今天我们探讨已经加载到Spring容器中的EurekaClientAutoConfiguration自动配置类是如何进行初始化的。
该值得一提的是,本小节也是Eureka 在 Spring Boot 中的配置注入与统一管理机制系列的收官之节。
EurekaClientAutoConfiguration
@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 {
……
}
该配置类用在 Spring Cloud 环境中为 Eureka 客户端提供自动配置。它确保在满足特定条件时,被加载到spring容器中,然后初始化时会创建Eureka 客户端所需的各种bean,包括配置属性、服务注册、健康检查等,以实现与 Eureka 服务注册中心的交互。
1. eurekaFeature()
方法
@Bean
public HasFeatures eurekaFeature() {
return HasFeatures.namedFeature("Eureka Client", EurekaClient.class);
}
创建HasFeatures
类型的 Bean,用于表示application具有 Eureka 客户端的功能特征。
2.eurekaClientConfigBean(ConfigurableEnvironment env)
方法
@Bean
@ConditionalOnMissingBean(value = EurekaClientConfig.class,
search = SearchStrategy.CURRENT)
public EurekaClientConfigBean eurekaClientConfigBean(ConfigurableEnvironment env) {
return new EurekaClientConfigBean();
}
当类路径中不存在EurekaClientConfig
类型的 Bean,则创建。这个 Bean 用于配置 Eureka 客户端的通用属性。
3. serviceManagementMetadataProvider()
方法
@Bean
@ConditionalOnMissingBean
public ManagementMetadataProvider serviceManagementMetadataProvider() {
return new DefaultManagementMetadataProvider();
}
创建ManagementMetadataProvider类型的bean
。用于提供服务管理的元数据信息,如
- 健康检查 URL:http://192.168.1.188/health
- 服务主页:http://192.168.1.188/home
- 服务状态页:http://192.168.1.188/status
4. eurekaInstanceConfigBean(InetUtils inetUtils, ManagementMetadataProvider managementMetadataProvider)
方法
@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.servlet.context-path", "/");
int serverPort = Integer.parseInt(
env.getProperty("server.port", env.getProperty("port", "8080")));
Integer managementPort = env.getProperty("management.server.port", Integer.class);
String managementContextPath = env
.getProperty("management.server.servlet.context-path");
Integer jmxPort = env.getProperty("com.sun.management.jmxremote.port",
Integer.class);
……
}
创建EurekaInstanceConfigBean类型的bean,用于配置 Eureka 客户端实例的属性,包括从环境变量中读取各种配置信息并设置实例的状态页 URL、健康检查 URL 等信息。
5. eurekaServiceRegistry()方法
@Bean
public EurekaServiceRegistry eurekaServiceRegistry() {
return new EurekaServiceRegistry();
}
创建EurekaServiceRegistry类型的bean,用于服务注册相关的操作。
if (log.isInfoEnabled()) {
log.info("Unregistering application "
+ reg.getApplicationInfoManager().getInfo().getAppName()
}
6. eurekaAutoServiceRegistration(ApplicationContext context, EurekaServiceRegistry registry, EurekaRegistration registration)
方法
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
@ConditionalOnProperty(
value = "spring.cloud.service-registry.auto-registration.enabled",
matchIfMissing = true)
public EurekaAutoServiceRegistration eurekaAutoServiceRegistration(
ApplicationContext context, EurekaServiceRegistry registry,
EurekaRegistration registration) {
return new EurekaAutoServiceRegistration(context, registry, registration);
}
创建EurekaAutoServiceRegistration类型的bean,用于自动进行服务注册和管理。
7. EurekaClientConfiguration和RefreshableEurekaClientConfiguration
内部类
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingRefreshScope
protected static class EurekaClientConfiguration {
……
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnRefreshScope
protected static class RefreshableEurekaClientConfiguration {
……
}
这两个内部类的生效条件刚好是相反的。前者用于在没有刷新作用域的情况下配置 Eureka 客户端相关的 Bean,包括EurekaClient、ApplicationInfoManager和
EurekaRegistration。后者用于在存在刷新作用域的情况下为 Eureka 客户端提供可刷新的配置,当应用的配置发生变化时,这些 Bean 可以根据新的配置进行更新。
8. EurekaHealthIndicatorConfiguration内部类
当类路径中存在Health类时,配置生效,同时创建EurekaHealthIndicator类型的bean,用于提供Eureka客户端的健康检测,具体的实现思路可参考心跳机制大揭秘:Redis、数据库与 PING 命令的多面演绎https://blog.csdn.net/weixin_45593273/article/details/141611860?spm=1001.2014.3001.5501
至此,EurekaClientAutoConfiguration自动配置类完成了初始化。