探究 Eureka 在 Spring Boot 中的配置注入与统一管理机制(下)——第二节

今天我们来探讨下已经加载到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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值