本章节将介绍怎样快速搭建一个 Eureka server 实例。首先启动 Eureka server 项目,然后启动 Eureka client 项目,Eureka client 项目将自动向 Eureka server 注册服务信息。我们先看看怎样创建和配置 Eureka server 实例。
Eureka server 是作为服务的注册与发现中心。它提供服务注册、提供注册表和同步状态,下面将介绍怎样利用 IDEA 搭建 Eureka server。详细步骤如下:
(1)打开“New Module”窗口,选择“Spring Initializr”向导创建项目,JDK 选择 1.8。如下图:
(2)如实填写 Maven 项目的 Group、Artifact、Type、Java Version、Version等信息,然后点击“Next”按钮进入下一步,如下图:
(3)选择我们需要引入的模块,该项目引入了“Spring Boot DevTools”、“Cloud Bootstrap”和“Eureka Server”模块依赖,然后点击“Next”按钮。如下图:
(4)填写模块名称和指定模块存放位置,最后点击“Finish”按钮完成项目创建。如下图:
(1)在 application.properties 文件中添加如下配置信息:
# 服务端口 server.port=8077 # 服务名称 spring.application.name=eureka-server # 服务地址 eureka.instance.hostname=localhost # 不向注册中心注册自己 eureka.client.register-with-eureka=false # 取消检索服务 eureka.client.fetch-registry=false # 开启注册中心的保护机制,默认是开启 eureka.server.enable-self-preservation=true # 设置保护机制的阈值,默认是0.85。 eureka.server.renewal-percent-threshold=0.5
(2)在 Eureka server 项目的启动类上面添加 @EnableEurekaServer 注解,代码如下:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaDemo1ServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaDemo1ServerApplication.class, args); } }
注意:在项目启动类上使用 @EnableEurekaServer,可以将项目作为 Spring Cloud 中的注册中心,即 Eureka server 服务端。
上面两个步骤已将项目配置完成,我们可以直接运行 EurekaDemo1ServerApplication 类来启动 Eureka server 项目,启动日志如下:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.2) 2021-02-04 22:29:23.776 INFO 3332 --- [ restartedMain] c.h.s.e.EurekaDemo1ServerApplication : Starting EurekaDemo1ServerApplication using Java 1.8.0_45 on MS-OYCYMLXUSLLD with PID 3332 (D:\learn\Spring\springcloud\springcloud_workspaces\springcloud_netflix_eureka\eureka_demo1\eureka_demo1_server\target\classes started by Administrator in D:\learn\Spring\springcloud\springcloud_workspaces\springcloud_netflix_eureka) 2021-02-04 22:29:23.782 INFO 3332 --- [ restartedMain] c.h.s.e.EurekaDemo1ServerApplication : No active profile set, falling back to default profiles: default 2021-02-04 22:29:23.981 INFO 3332 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2021-02-04 22:29:23.981 INFO 3332 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2021-02-04 22:29:25.932 INFO 3332 --- [ restartedMain] o.s.cloud.context.scope.GenericScope : BeanFactory id=cb86a24d-f668-33bf-a800-da5f46944e6a 2021-02-04 22:29:26.627 INFO 3332 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8077 (http) 2021-02-04 22:29:26.642 INFO 3332 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-02-04 22:29:26.643 INFO 3332 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41] 2021-02-04 22:29:26.869 INFO 3332 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-02-04 22:29:26.871 INFO 3332 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2886 ms 2021-02-04 22:29:27.823 INFO 3332 --- [ restartedMain] c.s.j.s.i.a.WebApplicationImpl : Initiating Jersey application, version 'Jersey: 1.19.4 05/24/2017 03:20 PM' 2021-02-04 22:29:27.944 INFO 3332 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using JSON encoding codec LegacyJacksonJson 2021-02-04 22:29:27.945 INFO 3332 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using JSON decoding codec LegacyJacksonJson 2021-02-04 22:29:28.152 INFO 3332 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using XML encoding codec XStreamXml 2021-02-04 22:29:28.152 INFO 3332 --- [ restartedMain] c.n.d.provider.DiscoveryJerseyProvider : Using XML decoding codec XStreamXml 2021-02-04 22:29:28.912 INFO 3332 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021-02-04 22:29:30.594 INFO 3332 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2021-02-04 22:29:31.786 INFO 3332 --- [ restartedMain] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses Jersey 2021-02-04 22:29:31.886 WARN 3332 --- [ restartedMain] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it to the classpath. 2021-02-04 22:29:31.907 INFO 3332 --- [ restartedMain] o.s.c.n.eureka.InstanceInfoFactory : Setting initial instance status as: STARTING 2021-02-04 22:29:31.953 INFO 3332 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Initializing Eureka in region us-east-1 2021-02-04 22:29:31.953 INFO 3332 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Client configured to neither register nor query for data. 2021-02-04 22:29:31.968 INFO 3332 --- [ restartedMain] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1612448971965 with initial instances count: 0 2021-02-04 22:29:32.079 INFO 3332 --- [ restartedMain] c.n.eureka.DefaultEurekaServerContext : Initializing ... 2021-02-04 22:29:32.085 WARN 3332 --- [ restartedMain] c.n.eureka.cluster.PeerEurekaNodes : The replica size seems to be empty. Check the route 53 DNS Registry 2021-02-04 22:29:32.174 INFO 3332 --- [ restartedMain] c.n.e.registry.AbstractInstanceRegistry : Finished initializing remote region registries. All known remote regions: [] 2021-02-04 22:29:32.176 INFO 3332 --- [ restartedMain] c.n.eureka.DefaultEurekaServerContext : Initialized 2021-02-04 22:29:32.190 INFO 3332 --- [ restartedMain] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator' 2021-02-04 22:29:32.233 INFO 3332 --- [ restartedMain] o.s.c.n.e.s.EurekaServiceRegistry : Registering application EUREKA-SERVER with eureka with status UP 2021-02-04 22:29:32.236 INFO 3332 --- [ Thread-37] o.s.c.n.e.server.EurekaServerBootstrap : Setting the eureka configuration.. 2021-02-04 22:29:32.258 INFO 3332 --- [ Thread-37] o.s.c.n.e.server.EurekaServerBootstrap : isAws returned false 2021-02-04 22:29:32.259 INFO 3332 --- [ Thread-37] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context 2021-02-04 22:29:32.259 INFO 3332 --- [ Thread-37] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node 2021-02-04 22:29:32.259 INFO 3332 --- [ Thread-37] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1 2021-02-04 22:29:32.259 INFO 3332 --- [ Thread-37] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP 2021-02-04 22:29:32.276 INFO 3332 --- [ Thread-37] e.s.EurekaServerInitializerConfiguration : Started Eureka Server 2021-02-04 22:29:32.280 INFO 3332 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8077 (http) with context path '' 2021-02-04 22:29:32.282 INFO 3332 --- [ restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8077 2021-02-04 22:29:33.550 INFO 3332 --- [ restartedMain] c.h.s.e.EurekaDemo1ServerApplication : Started EurekaDemo1ServerApplication in 11.87 seconds (JVM running for 16.179)
使用浏览器运行 http://localhost:8077,运行效果如下图:
到这里 Eureka server 项目创建完成,也能成功运行了。上图中“Instances currently registered with Eureka”中的表格中没有任何可用的服务实例。等我们下一个章节创建 Eureka client 实例后,该表格将会看到 Eureka client 服务。