EurekaClientAutoConfiguration
2021-05-16 12:58:11 0 举报
AI智能生成
EurekaClient 自动化配置
作者其他创作
大纲/内容
源码
@EnableConfigurationProperties
源码
/**
* Convenient way to quickly register {@link ConfigurationProperties} annotated beans
* with Spring. Standard Spring Beans will also be scanned regardless of this value.
* @return {@link ConfigurationProperties} annotated beans to register
*/
Class<?>[] value() default {};
* Convenient way to quickly register {@link ConfigurationProperties} annotated beans
* with Spring. Standard Spring Beans will also be scanned regardless of this value.
* @return {@link ConfigurationProperties} annotated beans to register
*/
Class<?>[] value() default {};
@Import(EnableConfigurationPropertiesImportSelector.class)
源码
源码注释
@ConditionalOnClass(EurekaClientConfig.class)
@ConditionalOnClass是指当classpath下有某个类时才装配。@ConditionalOnMissingClass是指当classpath下没有某个类时才装配
因为 EurekaClientConfig类在 com.netflix.eureka:eureka-client 包下面,所以也就是说只有当有这个jar 依赖的时候才会起作用
@ConditionalOnBean(EurekaDiscoveryClientConfiguration.Marker.class)
@ConditionalOnBean是指当spring容器中有某个bean时才装配。@ConditionalOnMissingBean当spring容器中不存在某个bean时才装配
首先我们看Eureka-client 端
主启动类
@EnableEurekaClient
源码
@EnableDiscoveryClient
源码
@Import(EnableDiscoveryClientImportSelector.class)
源码
子主题
EurekaDiscoveryClientConfiguration
源码
@ConditionalOnProperty(value = "eureka.client.enabled", matchIfMissing = true)
@ConditionalOnProperty是指当某个配置项为某个值时才装配。@ConditionalOnMissingProperty是指当某个配置项不等于某个值时才装配
我们看这个如果没有配置的话默认就是 true
@AutoConfigureBefore({ NoopDiscoveryClientAutoConfiguration.class,
CommonsClientAutoConfiguration.class, ServiceRegistryAutoConfiguration.class })
CommonsClientAutoConfiguration.class, ServiceRegistryAutoConfiguration.class })
源码
源码说明
@AutoConfigureAfter(name = "org.springframework.cloud.autoconfigure.RefreshAutoConfiguration")
源码
源码说明
@Autowired
private ConfigurableEnvironment env
private ConfigurableEnvironment env
什么时候初始化的?
SpringBoot main 方法 run 源码中
@Autowired(required = false)
private HealthCheckHandler healthCheckHandler
private HealthCheckHandler healthCheckHandler
什么时候初始化的?
首先 这个对象不一定 会被创建( 从注解看 required= false 他也不是必须的)
首先如果我们如果想要初始化必须添加pom 依赖
首先它是一个接口,我们可以发现这个接口只有 两个实现类
EurekaHealthCheckHandler
HealthCheckCallbackToHandlerBridge
因为:HealthCheckCallbackToHandlerBridge 是废弃的,所以我们这个接口的对象一定注入的是EurekaHealthCheckHandler的实例
我们发现 EurekaDiscoveryClientConfiguration这个配置类中创建了我们要找的Bean 对象
源码
核心方法:
我们可以发现只有当我们在配置文件中配置了:eureka.client.healthcheck.enabled 对象才会创建
源码
public EurekaClientConfigBean eurekaClientConfigBean()
核心代码
注解说只有IOC 容器没有EurekaClientConfig bean 对象的时候才会创建当前bean
@ImplementedBy(DefaultEurekaClientConfig.class)
@ImplementedBy注解的作用是指定默认接口的实现类
DefaultEurekaClientConfig
源码
@Singleton
源码
源码说明
@ProvidedBy(DefaultEurekaClientConfigProvider.class)
源码
源码说明
指向提供者
DefaultEurekaClientConfigProvider
源码
源码说明
源码
说明:客户端的配置bean对象,我们可以通过这个类看到很多的客户端的默认配置
public EurekaInstanceConfigBean eurekaInstanceConfigBean(InetUtils inetUtils)
核心代码
源码
该Bean 是Eureka 客户端的实例bean
InetUtils
我们可以根据这个配置我们客户端指定的网卡等
public DiscoveryClient discoveryClient(EurekaInstanceConfig config,
EurekaClient client)
EurekaClient client)
核心代码
对象源码
我们可以通过这个Bean 获取服务实例列表等
示例
public EurekaServiceRegistry eurekaServiceRegistry()
核心代码
源码
服务注册,服务注销 ,设置状态,获取状态
public EurekaRegistration eurekaRegistration(EurekaClient eurekaClient, CloudEurekaInstanceConfig instanceConfig, ApplicationInfoManager applicationInfoManager)
核心方法
类源码
作用就是我们可以通过这个Bean 拿到
EurekaClient
CloudEurekaInstanceConfig
ApplicationInfoManager
HealthCheckHandler
public EurekaAutoServiceRegistration eurekaAutoServiceRegistration(ApplicationContext context, EurekaServiceRegistry registry, EurekaRegistration registration)
方法
核心方法 start()
源码
public class EurekaAutoServiceRegistration implements AutoServiceRegistration, SmartLifecycle, Ordered
SmartLifecycle
SmartLifecycle 是一个接口。当Spring容器加载所有bean并完成初始化之后,会接着回调实现该接口的类中对应的方法(start()方法)
测试代码示例
经过测试发现,start 方法在InitializingBean 方法的 afterPropertiesSet() 方法之后执行
总结: 就是在IOC 容器启动完成之后,注册 监控检查,设置状态,发布事件
public MutableDiscoveryClientOptionalArgs discoveryClientOptionalArgs()
代码
@Configuration
@ConditionalOnMissingRefreshScope
protected static class EurekaClientConfiguration
@ConditionalOnMissingRefreshScope
protected static class EurekaClientConfiguration
代码
CloudEurekaClient
核心代码
public CloudEurekaClient(ApplicationInfoManager applicationInfoManager,
EurekaClientConfig config,
DiscoveryClientOptionalArgs args,
ApplicationEventPublisher publisher) {
super(applicationInfoManager, config, args);
this.applicationInfoManager = applicationInfoManager;
this.publisher = publisher;
this.eurekaTransportField = ReflectionUtils.findField(DiscoveryClient.class, "eurekaTransport");
ReflectionUtils.makeAccessible(this.eurekaTransportField);
}
EurekaClientConfig config,
DiscoveryClientOptionalArgs args,
ApplicationEventPublisher publisher) {
super(applicationInfoManager, config, args);
this.applicationInfoManager = applicationInfoManager;
this.publisher = publisher;
this.eurekaTransportField = ReflectionUtils.findField(DiscoveryClient.class, "eurekaTransport");
ReflectionUtils.makeAccessible(this.eurekaTransportField);
}
super(applicationInfoManager, config, args);
DiscoveryClient
源码
/**
* @deprecated use the version that take {@link com.netflix.discovery.AbstractDiscoveryClientOptionalArgs} instead
*/
@Deprecated
public DiscoveryClient(ApplicationInfoManager applicationInfoManager, final EurekaClientConfig config, DiscoveryClientOptionalArgs args) {
this(applicationInfoManager, config, (AbstractDiscoveryClientOptionalArgs) args);
}
* @deprecated use the version that take {@link com.netflix.discovery.AbstractDiscoveryClientOptionalArgs} instead
*/
@Deprecated
public DiscoveryClient(ApplicationInfoManager applicationInfoManager, final EurekaClientConfig config, DiscoveryClientOptionalArgs args) {
this(applicationInfoManager, config, (AbstractDiscoveryClientOptionalArgs) args);
}
this(applicationInfoManager, config, (AbstractDiscoveryClientOptionalArgs) args);
public DiscoveryClient(ApplicationInfoManager applicationInfoManager, final EurekaClientConfig config, AbstractDiscoveryClientOptionalArgs args) {
this(applicationInfoManager, config, args, new Provider<BackupRegistry>() {
private volatile BackupRegistry backupRegistryInstance;
@Override
public synchronized BackupRegistry get() {
if (backupRegistryInstance == null) {
String backupRegistryClassName = config.getBackupRegistryImpl();
if (null != backupRegistryClassName) {
try {
backupRegistryInstance = (BackupRegistry) Class.forName(backupRegistryClassName).newInstance();
logger.info("Enabled backup registry of type " + backupRegistryInstance.getClass());
} catch (InstantiationException e) {
logger.error("Error instantiating BackupRegistry.", e);
} catch (IllegalAccessException e) {
logger.error("Error instantiating BackupRegistry.", e);
} catch (ClassNotFoundException e) {
logger.error("Error instantiating BackupRegistry.", e);
}
}
if (backupRegistryInstance == null) {
logger.warn("Using default backup registry implementation which does not do anything.");
backupRegistryInstance = new NotImplementedRegistryImpl();
}
}
return backupRegistryInstance;
}
});
}
this(applicationInfoManager, config, args, new Provider<BackupRegistry>() {
private volatile BackupRegistry backupRegistryInstance;
@Override
public synchronized BackupRegistry get() {
if (backupRegistryInstance == null) {
String backupRegistryClassName = config.getBackupRegistryImpl();
if (null != backupRegistryClassName) {
try {
backupRegistryInstance = (BackupRegistry) Class.forName(backupRegistryClassName).newInstance();
logger.info("Enabled backup registry of type " + backupRegistryInstance.getClass());
} catch (InstantiationException e) {
logger.error("Error instantiating BackupRegistry.", e);
} catch (IllegalAccessException e) {
logger.error("Error instantiating BackupRegistry.", e);
} catch (ClassNotFoundException e) {
logger.error("Error instantiating BackupRegistry.", e);
}
}
if (backupRegistryInstance == null) {
logger.warn("Using default backup registry implementation which does not do anything.");
backupRegistryInstance = new NotImplementedRegistryImpl();
}
}
return backupRegistryInstance;
}
});
}
核心源码
源码
@Configuration
@ConditionalOnRefreshScope
protected static class RefreshableEurekaClientConfiguration
@ConditionalOnRefreshScope
protected static class RefreshableEurekaClientConfiguration
代码
@interface ConditionalOnMissingRefreshScope
代码
就是在当前类定义一个条件刷新注解,上面EurekaClientConfiguration 配置是否会起作用会判断这个条件是否满足
@interface ConditionalOnRefreshScope
代码
就是在当前类定义一个条件刷新注解,上面RefreshableEurekaClientConfiguration 配置是否会起作用会判断这个条件是否满足
@ConditionalOnClass(RefreshScope.class)
@RefreshScope 注解
源码
这个注解的作用是什么?
实现配置、实例热加载
实现原理:
印象笔记-Spring-Spring Cloud @RefreshScope 原理是什么?
OnMissingRefreshScopeCondition
@interface ConditionalOnMissingRefreshScope 注解的条件
代码
@interface ConditionalOnMissingRefreshScope 注解的条件
EurekaClientAutoConfiguration 是 springboot 启动的时候 org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#selectImports 方法加载的
@Import(EnableAutoConfigurationImportSelector.class)
@Deprecated
public class EnableAutoConfigurationImportSelector
extends AutoConfigurationImportSelector
public class EnableAutoConfigurationImportSelector
extends AutoConfigurationImportSelector
AutoConfigurationImportSelector
核心代码
0 条评论
下一页