今天我们继续探讨已经加载到Spring容器中的RibbonEurekaAutoConfiguration自动配置类是如何进行初始化的。
RibbonEurekaAutoConfiguration
java
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnRibbonAndEurekaEnabled
@AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = EurekaRibbonClientConfiguration.class)
public class RibbonEurekaAutoConfiguration {
}
RibbonEurekaAutoConfiguration自动配置类注入到Spring容器之前,会为Ribbon客户端设置默认配置,同时在RibbonAutoConfiguration类被加载之后才会被注入到Spring容器中。
我们分别来看下RibbonAutoConfiguration和EurekaRibbonClientConfiguration
RibbonAutoConfiguration
java
@Configuration
@Conditional({RibbonClassesConditions.class})
@RibbonClients
@AutoConfigureAfter(
name = {"org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration"}
)
@AutoConfigureBefore({LoadBalancerAutoConfiguration.class, AsyncLoadBalancerAutoConfiguration.class})
@EnableConfigurationProperties({RibbonEagerLoadProperties.class, ServerIntrospectorProperties.class})
public class RibbonAutoConfiguration {
......
}
- 生效的条件:在EurekaClientAutoConfiguration类加载之后被加载,且在LoadBalancerAutoConfiguration和 AsyncLoadBalancerAutoConfiguration类加载之前加载,同时Ribbon 相关类存在时才生效。
- 日志
java
RibbonAutoConfiguration matched:
- AllNestedConditions 4 matched 0 did not; NestedCondition on RibbonAutoConfiguration.RibbonClassesConditions.RibbonPresent @ConditionalOnClass found required class 'com.netflix.ribbon.Ribbon'; NestedCondition on RibbonAutoConfiguration.RibbonClassesConditions.AsyncRestTemplatePresent @ConditionalOnClass found required class 'org.springframework.web.client.AsyncRestTemplate'; NestedCondition on RibbonAutoConfiguration.RibbonClassesConditions.RestTemplatePresent @ConditionalOnClass found required class 'org.springframework.web.client.RestTemplate'; NestedCondition on RibbonAutoConfiguration.RibbonClassesConditions.IClientPresent @ConditionalOnClass found required class 'com.netflix.client.IClient' (RibbonAutoConfiguration.RibbonClassesConditions)
RibbonAutoConfiguration#loadBalancerClient matched:
- @ConditionalOnMissingBean (types: org.springframework.cloud.client.loadbalancer.LoadBalancerClient; SearchStrategy: all) did not find any beans (OnBeanCondition)
RibbonAutoConfiguration#propertiesFactory matched:
- @ConditionalOnMissingBean (types: org.springframework.cloud.netflix.ribbon.PropertiesFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
RibbonAutoConfiguration#springClientFactory matched:
- @ConditionalOnMissingBean (types: org.springframework.cloud.netflix.ribbon.SpringClientFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
小结:RibbonAutoConfiguration会被注入到Spring容器中。
EurekaRibbonClientConfiguration
该配置类是为使用 Eureka 进行服务发现的 Ribbon 客户端提供自动配置。以实现负载均衡和服务发现的功能。
该配置类初始化的时候,会创建3个bean。
ribbonPing(IClientConfig config)
方法
java
@Bean
@ConditionalOnMissingBean
public IPing ribbonPing(IClientConfig config) {
if (this.propertiesFactory.isSet(IPing.class, serviceId)) {
return this.propertiesFactory.get(IPing.class, config, serviceId);
}
NIWSDiscoveryPing ping = new NIWSDiscoveryPing();
ping.initWithNiwsConfig(config);
return ping;
}
创建IPing
类型的 Bean,用于检查Ribbon 客户端所调用的后端服务实例的健康状态。
ribbonServerList(IClientConfig config, Provider<EurekaClient> eurekaClientProvider)
方法
java
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config,
Provider<EurekaClient> eurekaClientProvider) {
if (this.propertiesFactory.isSet(ServerList.class, serviceId)) {
return this.propertiesFactory.get(ServerList.class, config, serviceId);
}
DiscoveryEnabledNIWSServerList discoveryServerList = new DiscoveryEnabledNIWSServerList(
config, eurekaClientProvider);
DomainExtractingServerList serverList = new DomainExtractingServerList(
discoveryServerList, config, this.approximateZoneFromHostname);
return serverList;
}
创建ServerList<?>
类型的 Bean,用于获取可用的后端服务实例列表。
serverIntrospector()
方法
java
@Bean
public ServerIntrospector serverIntrospector() {
return new EurekaServerIntrospector();
}
创建ServerIntrospector
类型的 Bean,用于获取服务实例的详细信息。
在该EurekaRibbonClientConfiguration类被初始化完成之后,还会调用preprocess()
方法,进行一些预处理的操作。
至此,RibbonEurekaAutoConfiguration自动配置类完成了在Spring容器中的初始化,使得在分布式系统中,Ribbon 和 Eureka 能够协同工作,确保服务的高可用性和可靠性。