Ribbon源码分析
2022-01-05 21:18:06 1 举报
此图是我对Ribbon源码的理解,供大家学习交流
作者其他创作
大纲/内容
RibbonLoadBalancerClient
service-order:192.168.3.101:8010
DiscoveryEnabledNIWSServerList#getUpdatedListOfServers
RibbonClientConfiguration
调用http接口
BaseLoadBalancer#chooseServer
rule.choose(key)
setupPingTask()
@Bean
SpringClientFactory
每10s执行
LoadBalancerClient#execute
ServerListUpdater
loadBalancer.chooseServer(hint != null ? hint : \"default\")
返回服务实例列表
PingTask
判断allServerList中的Server是否存活,如果存活,作为负载均衡的候选Server,放入upServerList
mall-user
LoadBalancerClient
RestTemplate
service-user
实现
LoadBalancerAutoConfiguration
mall-order:192.168.3.102:8010
初始化父类
Eureka Server
service-order:192.168.3.102:8010
AbstractServerPredicate#incrementAndGetModulo
2.获取所有eureka实例列表
作为@Bean初始化的构造器参数
ILoadBalancer
SmartInitializingSingleton
NIWSDiscoveryPing
默认
2.负载均衡器loadBalancer根据负载均衡算法选择一个server
LoadBalancerInterceptor
RibbonAutoConfiguration
ribbon
将服务实例列表设置到父类的allServerList
allServerListservice-order:192.168.3.101:8010192.168.3.102:8010
1. 获取负载均衡器
轮询,从0开始,每次加1,然后对实例列表取模得到下标值
spring-cloud-netflix-ribbon-2.1.3.RELEASE.jar!/META-INF/spring.factories
DiscoveryEnabledNIWSServerList
PredicateBasedRule#choose
run()
定制RestTemplate,将LoadBalancerInterceptor封装到RestTemplate的拦截器集合中
从eurekaClient获取服务实例缓存
Eureka ClientConsumer
setServersList(ls)
DomainExtractingServerList
1.ribbon定时更新eureka实例列表
ZoneAvoidanceRule
拦截此方法,替换service-order为服务实例ip:port
缓存mall-order:192.168.3.101:8010192.168.3.102:8010
本地缓存
IRule
url = \"http://service-order/order/findOrderByUserId/\
思考:为什么只会自动装配@LoadBalanced的注解的restTemplate?原因: 匹配给定的限定符注解@QualifierQualifierAnnotationAutowireCandidateResolver#isAutowireCandidate>QualifierAnnotationAutowireCandidateResolver#checkQualifiers
DynamicServerListLoadBalancer
new Pinger(pingStrategy).runPinger()
从Spring容器根据类型获取loadBalancer
ZoneAwareLoadBalancer
Nacos ClientProvider
用RestTemplateCustomizer定制所有的restTemplate
从容器中获取loadBalancer
EurekaRibbonClientConfiguration
@AutoConfigureBefore
service-order:192.168.3.101:8010192.168.3.102:8010
BaseLoadBalancer#setServersList
用于标记配置的RestTemplate bean使用LoadBalancerClient
每隔30s自动更新eureka实例列表
PollingServerListUpdater
context.getBean(ILoadBalancer.class)
构造器入参
定时拉取订单服务列表
initWithConfig
设置server的定时Ping任务
Eureka ClientProvider
allServerListmall-order:192.168.3.101:8010192.168.3.102:8010
restOfInit(clientConfig)
RestTemplateCustomizer
Nacos Client Consumer
mall-order:192.168.3.101:8010
缓存service-order:192.168.3.101:8010192.168.3.102:8010
@LoadBalanced
注册
RestTemplate#doExecute
IPing
BaseLoadBalancer
updateListOfServers()
applications = this.localRegionApps.get()
Nacos Server
@RibbonClients
start
被@LoadBalanced注解的restTemplates(装了所有被@LoadBalanced注解的restTemplate)
enableAndInitLearnNewServersFeature()
serverListImpl.getUpdatedListOfServers()
LoadBalancerInterceptor#intercept
url = \"http://mall-order/order/findOrderByUserId/\
ping.isAlive(servers[i])
@ConditionalOnBean
serverListUpdater.start(updateAction)
updateAllServerList(servers)
mall-order 替换为192.168.3.102:8010
构造器入参,对应serverList
service-order 替换为192.168.3.102:8010
notifyServerStatusChangeListener(changedServers)
this.clientFactory.getLoadBalancer(serviceId)
isAlive = status.equals(InstanceStatus.UP)
拦截此方法,替换mall-order为服务实例ip:port
DiscoveryClient
RibbonEurekaAutoConfiguration
ILoadBalancer loadBalancer = getLoadBalancer(serviceId)
mall-order:192.168.3.101:8010192.168.3.102:8010
调用拦截器
构造器入参,对应serverListUpdater
0 条评论
下一页