Spring Cloud系列(四):Eureka源码解析之客户端

  1、根据自动装配原理(详见:Spring Boot系列(二):Spring Boot自动装配原理解析),找到spring-cloud-netflix-eureka-client.jar的spring.factories,查看spring.factories如下:

Spring Cloud系列(四):Eureka源码解析之客户端

   2、进入EurekaClient的自动装配类EurekaClientAutoConfiguration:

Spring Cloud系列(四):Eureka源码解析之客户端

   3、@ConditionalOnBean(EurekaDiscoveryClientConfiguration.Marker.class)也就是说当容器中有EurekaDiscoveryClientCon figuration.Marker.class时,该配置类才起作用。接下来查看spring.factories,发现还有一个配置类EurekaDiscoveryClientConfigura tion,该配置类就刚刚好往容器中导入了EurekaDiscoveryClientConfiguration.Marker。

二、EurekaClient   1、EurekaClientAutoConfiguration配置类   1.1 导入的核心Bean

  ① EurekaClientConfigBean:初始化eurekaClient配置;

  ② EurekaInstanceConfigBean:初始化eureka实例配置;

  ③ EurekaDiscoveryClient:初始化eureka发现客户端;

  ④ EurekaServiceRegistry:初始化eureka服务注册;

  ⑤ EurekaAutoServiceRegistration:初始化eureka自动服务注册;

  ⑥ EurekaClient:初始化eureka客户端;

  1.2 初始化EurekaClient

  ① 首先看下类的继承图:

Spring Cloud系列(四):Eureka源码解析之客户端

   ② CloudEurekaClient构造方法:

  点进去会发现它调用父类的构造方法super(applicationInfoManager, config, args);最终来到如下方法:

@Inject DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, AbstractDiscoveryClientOptionalArgs args, Provider<BackupRegistry> backupRegistryProvider, EndpointRandomizer endpointRandomizer) { //省略...... this.applicationInfoManager = applicationInfoManager; //1、获取要注册的服务实例信息 InstanceInfo myInfo = applicationInfoManager.getInfo(); //省略...... instanceInfo = myInfo; //省略...... //2、定义一些Executor try { // default size of 2 - 1 each for heartbeat and cacheRefresh scheduler = Executors.newScheduledThreadPool(2, new ThreadFactoryBuilder() .setNameFormat("DiscoveryClient-%d") .setDaemon(true) .build()); //心跳Executor heartbeatExecutor = new ThreadPoolExecutor( 1, clientConfig.getHeartbeatExecutorThreadPoolSize(), 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryBuilder() .setNameFormat("DiscoveryClient-HeartbeatExecutor-%d") .setDaemon(true) .build() ); // use direct handoff //本地缓存刷新Executor cacheRefreshExecutor = new ThreadPoolExecutor( 1, clientConfig.getCacheRefreshExecutorThreadPoolSize(), 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryBuilder() .setNameFormat("DiscoveryClient-CacheRefreshExecutor-%d") .setDaemon(true) .build() ); // use direct handoff /** * 1、初始化定时拉取服务注册信息和服务续约任务 * 2、定义一个状态变化监听 * 3、初始化定时服务注册任务 */ initScheduledTasks(); //省略...... }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsfxyj.html