Eureka Client注册服务 启动流程

作者:神秘网友 发布时间:2020-09-15 05:12:43

Eureka Client注册服务 启动流程

Eureka Client注册服务 启动流程

启动过程:Eureka客户端在启动时也会装载很多配置类,我们通过spring-cloud-netflix-eureka-client-2.1.0.RELEASE.jar下的spring.factories?件可以看到加载的配置类

使用了Springboot 的自动配置 重点关注:EurekaClientAutoConfiguration
Eureka Client注册服务 启动流程
引?jar就会被?动装配,分析EurekaClientAutoConfiguration类头
@AutoConfigureBefore 必须先把配置的bean装配完,才会装配当前bean

重点关注:EurekaDiscoveryClientConfiguration

@Configuration
@EnableConfigurationProperties
@ConditionalOnClass(EurekaClientConfig.class)
@Import(DiscoveryClientOptionalArgsConfiguration.class)
@ConditionalOnBean(EurekaDiscoveryClientConfiguration.Marker.class)
@ConditionalOnProperty(value = "eureka.client.enabled", matchIfMissing = true)
@AutoConfigureBefore({ NoopDiscoveryClientAutoConfiguration.class,
		CommonsClientAutoConfiguration.class, ServiceRegistryAutoConfiguration.class })
@AutoConfigureAfter(name = {"org.springframework.cloud.autoconfigure.RefreshAutoConfiguration",
		"org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration",
		"org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration"})
public class EurekaClientAutoConfiguration {

如果项目中引入了EurekaClient相关jar,而不想作为客户端,可以设置eureka.client.enabled=false

 */
@Configuration
@EnableConfigurationProperties
@ConditionalOnClass(EurekaClientConfig.class)
@ConditionalOnProperty(value = "eureka.client.enabled", matchIfMissing = true)
public class EurekaDiscoveryClientConfiguration {

	class Marker {}
	
	//自动装配过程中其实Eureka会自动帮我们先注入一个marker
	//所以在启动类上也可以不用加上 @EnableEurekaClient 也是ok的
	@Bean
	public Marker eurekaDiscoverClientMarker() {
		return new Marker();
	}

回到主配置类EurekaClientAutoConfiguration
思考:EurekaClient启动过程要做什么事情??????
1)读取配置?件
2)启动时从EurekaServer获取服务实例信息
3)注册??到EurekaServer(addInstance)
4)开启?些定时任务(?跳续约,刷新本地服务缓存列表)

EurekaInstanceConfigBean 读取封装的配置信息并注入容器中

	@Bean
	@ConditionalOnMissingBean(value = EurekaInstanceConfig.class, search = SearchStrategy.CURRENT)
	public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils,
															 ManagementMetadataProvider managementMetadataProvider) {
		String hostname = getProperty("eureka.instance.hostname");
		boolean preferIpAddress = Boolean.parseBoolean(getProperty("eureka.instance.prefer-ip-address"));
		String ipAddress = getProperty("eureka.instance.ip-address");
		boolean isSecurePortEnabled = Boolean.parseBoolean(getProperty("eureka.instance.secure-port-enabled"));

		String serverContextPath = env.getProperty("server.context-path", "/");
		int serverPort = Integer.valueOf(env.getProperty("server.port", env.getProperty("port", "8080")));

		Integer managementPort = env.getProperty("management.server.port", Integer.class);// nullable. should be wrapped into optional
		String managementContextPath = env.getProperty("management.server.servlet.context-path");// nullable. should be wrapped into optional
		Integer jmxPort = env.getProperty("com.sun.management.jmxremote.port", Integer.class);//nullable
		EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils);

实例化EurekaClient对象,返回CloudEurekaClient

		@Bean(destroyMethod = "shutdown")
		@ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT)
		public EurekaClient eurekaClient(ApplicationInfoManager manager, EurekaClientConfig config) {
			return new CloudEurekaClient(manager, config, this.optionalArgs,
					this.context);
		}

super(applicationInfoManager, config, args) 构造函数 中调用父类

	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);
	}

观察?类DiscoveryClient()

Eureka Client注册服务 启动流程
在另外?个构造器中
Eureka Client注册服务 启动流程
Eureka Client注册服务 启动流程

com.netflix.discovery.DiscoveryClient#DiscoveryClient(com.netflix.appinfo.ApplicationInfoManager, com.netflix.discovery.EurekaClientConfig, com.netflix.discovery.AbstractDiscoveryClientOptionalArgs, javax.inject.Provider<com.netflix.discovery.BackupRegistry>)

        if (clientConfig.shouldRegisterWithEureka() && clientConfig.shouldEnforceRegistrationAtInit()) {
            try {
                if (!register() ) {
                    throw new IllegalStateException("Registration error at startup. Invalid server response.");
                }
            } catch (Throwable th) {
                logger.error("Registration error at startup: {}", th.getMessage());
                throw new IllegalStateException(th);
            }
        }

Eureka Client注册服务 启动流程
底层使?Jersey客户端进?远程请求。

nitScheduledTasks() 初始化定时任务

        if (clientConfig.shouldRegisterWithEureka() && clientConfig.shouldEnforceRegistrationAtInit()) {
            try {
                if (!register() ) {
                    throw new IllegalStateException("Registration error at startup. Invalid server response.");
                }
            } catch (Throwable th) {
                logger.error("Registration error at startup: {}", th.getMessage());
                throw new IllegalStateException(th);
            }
        }

        // finally, init the schedule tasks (e.g. cluster resolvers, heartbeat, instanceInfo replicator, fetch
        initScheduledTasks();

Eureka Client注册服务 启动流程
?跳续约定时任务
Eureka Client注册服务 启动流程
Eureka Client注册服务 启动流程

Eureka Client注册服务 启动流程相关教程

  1. ESP32 开发笔记(三)源码示例 17_WIFI_AP_TCP_Client 在AP模式下
  2. 我该如何学习spring源码以及解析bean定义的注册
  3. SpringCloud系列一:Springcloud介绍及注册中心搭建
  4. 《Spring Cloud Netflix》--服务注册和服务发现-Eureka的深入了
  5. 微信小程序之逻辑层(JS)注册页面(初始化数据+生命周期回调函数+
  6. Spring Could搭建Eureka实战(二)
  7. 微服务架构之Spring Cloud Eureka入门程序
  8. 记一次docker构建eureka应用