今天我们来探讨下已经加载到Spring容器中的EurekaDiscoveryClientConfiguration自动配置类是如何进行初始化的。
EurekaDiscoveryClientConfiguration
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass(EurekaClientConfig.class)
@ConditionalOnProperty(value = "eureka.client.enabled", matchIfMissing = true)
@ConditionalOnDiscoveryEnabled
@AutoConfigureBefore({ NoopDiscoveryClientAutoConfiguration.class,
CommonsClientAutoConfiguration.class, ServiceRegistryAutoConfiguration.class })
@AutoConfigureAfter(name = {
"org.springframework.cloud.netflix.eureka.config.DiscoveryClientOptionalArgsConfiguration",
"org.springframework.cloud.autoconfigure.RefreshAutoConfiguration",
"org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration",
"org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration" })
public class EurekaClientAutoConfiguration {
……
}
该配置类负责 Eureka 发现客户端的具体配置,确保客户端能够根据配置正确地注册和发现服务。
初始化时创建的Bean有两个,分别是discoveryClient和eurekaHealthCheckHandler。
discoveryClient用于与 Eureka 服务器进行交互,实现服务发现功能。
@ConfigurationProperties("spring.cloud.loadbalancer.eureka")
public class EurekaLoadBalancerProperties {
/**
* Used to determine whether we should try to get the `zone` value from host name.
*/
private boolean approximateZoneFromHostname = false;
public boolean isApproximateZoneFromHostname() {
return approximateZoneFromHostname;
}
public void setApproximateZoneFromHostname(boolean approximateZoneFromHostname) {
this.approximateZoneFromHostname = approximateZoneFromHostname;
}
}
eurekaHealthCheckHandler是EurekaHealthCheckHandlerConfiguration
内部类中的bean。主要用来和Eureka服务端保持心跳,确保Eureka客户端可用。
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "eureka.client.healthcheck.enabled",
matchIfMissing = false)
protected static class EurekaHealthCheckHandlerConfiguration {
@Autowired(required = false)
private StatusAggregator statusAggregator = new SimpleStatusAggregator();
@Bean
@ConditionalOnMissingBean(HealthCheckHandler.class)
public EurekaHealthCheckHandler eurekaHealthCheckHandler() {
return new EurekaHealthCheckHandler(this.statusAggregator);
}
}
这两个bean的创建条件都是Spring容器中不存在时才会创建。
最后,我们再来看下EurekaClientConfigurationRefresher静态内部类。
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RefreshScopeRefreshedEvent.class)
protected static class EurekaClientConfigurationRefresher
implements ApplicationListener<RefreshScopeRefreshedEvent> {
@Autowired(required = false)
private EurekaClient eurekaClient;
@Autowired(required = false)
private EurekaAutoServiceRegistration autoRegistration;
public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
// This will force the creation of the EurkaClient bean if not already created
// to make sure the client will be reregistered after a refresh event
if (eurekaClient != null) {
eurekaClient.getApplications();
}
if (autoRegistration != null) {
// register in case meta data changed
this.autoRegistration.stop();
this.autoRegistration.start();
}
}
- 生效条件:类路径中存在
RefreshScopeRefreshedEvent
类。 - 作用:在 Spring 应用中监听
RefreshScopeRefreshedEvent
事件,确保Eureka客户端的配置在刷新时,能够正确地与Eureka服务端交互并更新服务的注册信息(即检查当前的Eureka客户端是否存在,存在的话获取Application,然后重启服务自动注册功能注册该Application)。