Springcloud提供了两种负载均衡实现Ribbon和LoadBalancer
负载均衡自动配置类,属于spring-cloud-common包,不管是Ribbon还是LoadBalancer都使用了该配置类
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RestTemplate.class)
@ConditionalOnBean(LoadBalancerClient.class)
@EnableConfigurationProperties(LoadBalancerRetryProperties.class)
public class LoadBalancerAutoConfiguration {
用于创建SpringCloud子容器,在子容器中可以创建不同的bean,即可以给每个服务(需要调用的微服务)设置不同的容器,对应的配置类有默认配置类,也有全局配置类,也可以为单个bean定义配置。
RibbonAutoConfiguration:ribbo默认配置类,List< RibbonClientSpecification >
configurations是空的不用注册
RibbonNacosAutoConfiguration:nacos默认配置类,使用@RibbonClients注入全局默认配置类NacosRibbonClientConfiguration,需要注册
ribbon自动配置类,属于netflix.ribbon包
@Configuration
@Conditional(RibbonAutoConfiguration.RibbonClassesConditions.class)
@RibbonClients
@AutoConfigureAfter(
name = "org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration")
@AutoConfigureBefore({ LoadBalancerAutoConfiguration.class,
AsyncLoadBalancerAutoConfiguration.class })
@EnableConfigurationProperties({ RibbonEagerLoadProperties.class,
ServerIntrospectorProperties.class })
public class RibbonAutoConfiguration {
ribbon客户端配置类
@Import({ HttpClientConfiguration.class, OkHttpRibbonConfiguration.class,
RestClientRibbonConfiguration.class, HttpClientRibbonConfiguration.class })
public class RibbonClientConfiguration {
@Bean
@ConditionalOnMissingBean
public IClientConfig ribbonClientConfig() {
}
@Bean
@ConditionalOnMissingBean
public IRule ribbonRule(IClientConfig config) {
ZoneAvoidanceRule rule = new ZoneAvoidanceRule();
rule.initWithNiwsConfig(config);
return rule;
}
@Bean
@ConditionalOnMissingBean
@SuppressWarnings("unchecked")
public ServerList<Server> ribbonServerList(IClientConfig config) {
.....
return serverList;
}
@Bean
@ConditionalOnMissingBean
public ILoadBalancer ribbonLoadBalancer(IClientConfig config,
ServerList<Server> serverList, ServerListFilter<Server> serverListFilter,
IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
return new ZoneAwareLoadBalancer<>(config, rule, ping, serverList,
serverListFilter, serverListUpdater);
}
}
属于spring-cloud-loadbalancer包
自动配置类LoadBalancerAutoConfiguration(属于spring-cloud-common包)和
BlockingLoadBalancerClientAutoConfiguration
@Configuration
@LoadBalancerClients
@AutoConfigureAfter(LoadBalancerAutoConfiguration.class)
@AutoConfigureBefore({
org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration.class,
AsyncLoadBalancerAutoConfiguration.class })
public class BlockingLoadBalancerClientAutoConfiguration {
属于spring-cloud-loadbalancer包
@Configuration
@LoadBalancerClients
@AutoConfigureBefore({ ReactorLoadBalancerClientAutoConfiguration.class,
ReactiveLoadBalancerAutoConfiguration.class })
// @EnableCaching //TODO: how to enforce, or check conditions?
// @AutoConfigureBefore(CacheAutoConfiguration.class)
public class LoadBalancerAutoConfiguration {
@AutoConfigureBefore以下响应式配置前生效
spring-cloud-starter-alibaba-nacos-discovery依赖了spring-cloud-starter-netflix-ribbon,因此只需要引入Nacos服务发现依赖,就会自动实现ribbon功能
@RibbonClients(defaultConfiguration = NacosRibbonClientConfiguration.class)
public class RibbonNacosAutoConfiguration {
}
@ConditionalOnRibbonNacos
public class NacosRibbonClientConfiguration {
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config,
NacosDiscoveryProperties nacosDiscoveryProperties) {
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(config);
return serverList;
}
}
使用@RibbonClients导入了全局默认配置类NacosRibbonClientConfiguration,在该配置类中定义了ServerList服务实例NacosServerList,通过负载均衡查找服务实例时会调用NacosServerList#getServers方法,从Nacos客户端获取服务实例
注册中心Nacos为例
调用NamedContextFactory给order-server创建子容器,给容器注册配置类(自定义的该服务的配置类、全局默认配置类:注册RibbonNacosAutoConfiguration),注册客户端配置类RibbonClientConfiguration,添加环境变量服务名ribbon.client.name:order-server
从容器中获取ILoadBalancer的bean对象,默认是ZoneAwareLoadBalancer,创建该bean对象,注入规则ZoneAvoidanceRule、服务列表NacosServerList、服务配置DefaultClientConfigImpl等;构造对象时,通过NacosServerList根据服务名从注册中心客户端获取服务实例数据,将其缓存在本地