Netflix Eureka 入门示例
说明
- 脱离SpringCloud独立部署实践Eureka
- 包含部署一个Eureka Server,一个服务提供者,一个服务消费者
- 不包含代码讲解,用于体验原生的Eureka使用流程
- 对于打算阅读Eureka源码有前置帮助
前置需求
- 安装gradle,我的是6.6.1
- 安装tomcat,我的是8
源码下载
-
git clone https://github.com/Netflix/eureka.git
源码修改
修改eureka-server配置
- 修改路径eureka-server/src/main/resources/eureka-client.properties
- 新增配置:eureka.shouldFetchRegistry=false
- 不拉取注册信息
- 新增配置:eureka.registration.enabled=false
- 不注册到eureka
构建项目
-
cd eureka gradle -x test clean build
部署Eureka-Server
-
cp ./eureka-server/build/libs/eureka-server-XXX-SNAPSHOT.war $TOMCAT_HOME/webapps/eureka.war
- $TOMCAT_HOME替换为自己的tomcat安装目录
- 注意:一定要重命名为eureka.war!
- 启动tomcat,等待一会儿
- 访问WebUI,就能看到丑丑的页面了: http://localhost:8080/eureka
服务注册和发现测试
-
启动服务提供者 gradle :eureka-examples:runExampleService
-
实际运行的是:ExampleEurekaService.java
-
服务启动后会挂起,等待服务消费者连接,看到以下日志表示启动成功:Service started and ready to process requests..
-
Service started and ready to process requests.. [DiscoveryClient-InstanceInfoReplicator-0] INFO com.netflix.discovery.DiscoveryClient - DiscoveryClient_SAMPLEREGISTERINGSERVICE/xxxMacBook-Pro.local - registration status: 204 <===========--> 90% EXECUTING [27s] > :eureka-examples:runExampleService
-
此时查看WebUI就能看到注册的服务提供者了
-
-
启动服务消费者 gradle :eureka-examples:runExampleClient
-
实际运行的是:ExampleEurekaClient.java
-
以下为消费成功日志
-
Found an instance of example service to talk to from eureka: sampleservice.mydomain.net:8001 healthCheckUrl: http://xxxMacBook-Pro.local:8001/healthcheck override: UNKNOWN Connected to server. Sending a sample request: FOO Tue Feb 23 16:50:41 CST 2021 Waiting for server response.. Received response from server: BAR Tue Feb 23 16:50:41 CST 2021 Exiting the client. Demo over.. [main] INFO com.netflix.discovery.DiscoveryClient - Shutting down DiscoveryClient ... [main] INFO com.netflix.discovery.DiscoveryClient - Completed shut down of DiscoveryClient Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings BUILD SUCCESSFUL in 14s 6 actionable tasks: 3 executed, 3 up-to-date
-
-
同时提供者会打印后续日志
-
Client got connected... processing request from the client Received a request from the example client: FOO Tue Feb 23 16:56:58 CST 2021 Sending the response to the client: BAR Tue Feb 23 16:56:58 CST 2021 Simulating service doing work by sleeping for 5 seconds... Shutting down server. Demo over.
-
消费完成后,提供者会在5s后退出
-