Netflix Eureka client 简单实例

本章节将介绍怎样快速搭建一个 Eureka client 实例。启动 Eureka client 项目将自动向 Eureka server 注册服务信息。我们先看看怎样创建和配置 Eureka client 实例。

Eureka client 实例

Eureka client 程序启动时,Eureka 客户端向服务注册中心注册自身提供的服务,并周期性的发送心跳来更新它的服务租约。同时,他也能从服务端查询当前注册的服务信息并把它们缓存到本地并周期行的刷新服务状态。

Eureka client 是一个 Java 客户端,用于简化与 Eureka Server 的交互。Eureka Client 会拉取、更新和缓存 Eureka Server 中的信息。详细步骤如下:

创建 Eureka client 项目

(1)打开“New Module”窗口,选择“Spring Initializr”向导创建项目,JDK 选择 1.8。如下图:

(2)如实填写 Maven 项目的 Group、Artifact、Type、Java Version、Version等信息,然后点击“Next”按钮进入下一步,如下图:

(3)选择我们需要引入的模块,该项目引入了“Spring Boot DevTools”、“Cloud Bootstrap”和“Eureka Discovery Client”模块依赖,然后点击“Next”按钮。如下图:

(4)填写模块名称和指定模块存放位置,最后点击“Finish”按钮完成项目创建。如下图:

配置 Eureka client 项目

(1)在 application.properties 文件中添加如下配置信息:

# 服务端口
server.port=7001
# 服务名称
spring.application.name=user
# 服务地址
eureka.instance.hostname=localhost
# 注册中心路径,表示我们向这个注册中心注册服务,如果向多个注册中心注册,用“,”进行分隔
eureka.client.serviceUrl.defaultZone=http://localhost:8077/eureka
# 心跳间隔5s,默认30s。每一个服务配置后,心跳间隔和心跳超时时间会被保存在server端,不同服务的心跳频率可能不同,server端会根据保存的配置来分别探活
eureka.instance.lease-renewal-interval-in-seconds=5
# 心跳超时时间10s,默认90s。从client端最后一次发出心跳后,达到这个时间没有再次发出心跳,表示服务不可用,将它的实例从注册中心移除
eureka.instance.lease-expiration-duration-in-seconds=10

(2)在 Eureka client 项目的启动类上面添加 @EnableEurekaClient 注解,代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaDemo1ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaDemo1ClientApplication.class, args);
    }

}

上面代码中,@EnableEurekaClient 注解的作用将在项目启动时自动向 Eureka server 注册中心注册服务。

运行 Eureka client 项目

上面两个步骤已将项目配置完成,我们可以直接运行 EurekaDemo1ClientApplication 类来启动 Eureka client 项目,启动日志如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.2)

2021-02-04 22:45:39.839  INFO 7548 --- [  restartedMain] c.h.s.e.EurekaDemo1ClientApplication     : Starting EurekaDemo1ClientApplication using Java 1.8.0_45 on MS-OYCYMLXUSLLD with PID 7548 (D:\learn\Spring\springcloud\springcloud_workspaces\springcloud_netflix_eureka\eureka_demo1\eureka_demo1_client\target\classes started by Administrator in D:\learn\Spring\springcloud\springcloud_workspaces\springcloud_netflix_eureka)
2021-02-04 22:45:39.845  INFO 7548 --- [  restartedMain] c.h.s.e.EurekaDemo1ClientApplication     : No active profile set, falling back to default profiles: default
2021-02-04 22:45:39.958  INFO 7548 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-02-04 22:45:39.958  INFO 7548 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-02-04 22:45:41.409  INFO 7548 --- [  restartedMain] o.s.cloud.context.scope.GenericScope     : BeanFactory id=6eaecee8-c2b7-31d0-a52c-60e0eb9d0470
2021-02-04 22:45:42.072  INFO 7548 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 7001 (http)
2021-02-04 22:45:42.093  INFO 7548 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-02-04 22:45:42.093  INFO 7548 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-02-04 22:45:42.272  INFO 7548 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-02-04 22:45:42.272  INFO 7548 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2312 ms
2021-02-04 22:45:42.632  INFO 7548 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-02-04 22:45:42.935  WARN 7548 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : Unable to start LiveReload server
2021-02-04 22:45:44.434  INFO 7548 --- [  restartedMain] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate.
2021-02-04 22:45:46.025  WARN 7548 --- [  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:45:46.053  INFO 7548 --- [  restartedMain] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2021-02-04 22:45:46.103  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2021-02-04 22:45:46.112  INFO 7548 --- [  restartedMain] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2021-02-04 22:45:47.090  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2021-02-04 22:45:47.090  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2021-02-04 22:45:47.090  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2021-02-04 22:45:47.090  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Application is null : false
2021-02-04 22:45:47.090  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2021-02-04 22:45:47.091  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2021-02-04 22:45:47.091  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2021-02-04 22:45:47.442  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : The response status is 200
2021-02-04 22:45:47.445  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 5
2021-02-04 22:45:47.449  INFO 7548 --- [  restartedMain] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2021-02-04 22:45:47.458  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1612449947456 with initial instances count: 0
2021-02-04 22:45:47.460  INFO 7548 --- [  restartedMain] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application USER with eureka with status UP
2021-02-04 22:45:47.461  INFO 7548 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1612449947461, current=UP, previous=STARTING]
2021-02-04 22:45:47.463  INFO 7548 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER/MS-OYCYMLXUSLLD:user:7001: registering service...
2021-02-04 22:45:47.504  INFO 7548 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 7001 (http) with context path ''
2021-02-04 22:45:47.506  INFO 7548 --- [  restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 7001
2021-02-04 22:45:47.704  INFO 7548 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_USER/MS-OYCYMLXUSLLD:user:7001 - registration status: 204
2021-02-04 22:45:49.153  INFO 7548 --- [  restartedMain] c.h.s.e.EurekaDemo1ClientApplication     : Started EurekaDemo1ClientApplication in 11.337 seconds (JVM running for 16.428)
2021-02-04 22:46:17.455  INFO 7548 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2021-02-04 22:46:17.455  INFO 7548 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2021-02-04 22:46:17.455  INFO 7548 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2021-02-04 22:46:17.455  INFO 7548 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application is null : false
2021-02-04 22:46:17.455  INFO 7548 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2021-02-04 22:46:17.455  INFO 7548 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application version is -1: false
2021-02-04 22:46:17.456  INFO 7548 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2021-02-04 22:46:17.465  INFO 7548 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The response status is 200
2021-02-04 22:50:47.109  INFO 7548 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

使用浏览器运行 http://localhost:8077,查看 Eureka server 上面是否已经存在 Eureka client 服务,如下图:

上图中,“MS-OYCYMLXUSLLD:user:7001”就是我们的 Eureka client 服务。在后续章节中将介绍怎样调用我们注册到 Eureka server 注册中心的服务。

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号