Spring Cloud Eureka Client

Spring Cloud Eureka Client

使用Eureka Client

在pom中包含jar

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

注册

指定注册中心的位置

1
2
3
4
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

注册认证

HTTP Basic认证已经自动添加到eureka客户端中, 更复杂的需求,参考文档实现

1
http://user:password@localhost:8761/eureka

源码分析

在阅读源码的时候,我们根据它本身提供的功能去关注它实现的过程,以及代码是怎么被触发的

  1. 入口

    通过观察spring-cloud-netflix-eureka-client包中的META-INF/spring.factories可以看到里面配置了一组EnableAutoConfiguration, 我们重点关注EurekaClientAutoConfiguration

  2. EurekaClientAutoConfiguration

    在这个对象中,主要是根据条件创建了一组spring bean, 包括

    • EurekaClientConfigBean
    • ManagementMetadataProvider
    • EurekaInstanceConfigBean
    • DiscoveryClient
    • EurekaServiceRegistry
    • EurekaClient
  3. EurekaClient

    继续跟踪代码,观察EurekaClient的初始化过程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
       public class CloudEurekaClient extends DiscoveryClient {
    ...
    public CloudEurekaClient(ApplicationInfoManager applicationInfoManager,
    EurekaClientConfig config, AbstractDiscoveryClientOptionalArgs<?> args,
    ApplicationEventPublisher publisher) {
    super(applicationInfoManager, config, args);
    this.applicationInfoManager = applicationInfoManager;
    this.publisher = publisher;
    this.eurekaTransportField = ReflectionUtils.findField(DiscoveryClient.class,
    "eurekaTransport");
    ReflectionUtils.makeAccessible(this.eurekaTransportField);
    }
    ...
    }

    这里会调用父级的构造方法去执行注册、心跳、缓存刷新等任务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
        DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, AbstractDiscoveryClientOptionalArgs args,
    Provider<BackupRegistry> backupRegistryProvider) {
    ...
    // default size of 2 - 1 each for heartbeat and cacheRefresh
    scheduler = Executors.newScheduledThreadPool(2,
    new ThreadFactoryBuilder()
    .setNameFormat("DiscoveryClient-%d")
    .setDaemon(true)
    .build());

    heartbeatExecutor = new ThreadPoolExecutor(
    1, clientConfig.getHeartbeatExecutorThreadPoolSize(), 0, TimeUnit.SECONDS,
    new SynchronousQueue<Runnable>(),
    new ThreadFactoryBuilder()
    .setNameFormat("DiscoveryClient-HeartbeatExecutor-%d")
    .setDaemon(true)
    .build()
    ); // use direct handoff

    cacheRefreshExecutor = new ThreadPoolExecutor(
    1, clientConfig.getCacheRefreshExecutorThreadPoolSize(), 0, TimeUnit.SECONDS,
    new SynchronousQueue<Runnable>(),
    new ThreadFactoryBuilder()
    .setNameFormat("DiscoveryClient-CacheRefreshExecutor-%d")
    .setDaemon(true)
    .build()
    ); // use direct handoff
    ...
    }

    到这里我们可以发现,spring cloud eureka client将这些任务都委托给了Netflix的Eureka组建去执行

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×