Spring Cloud 2.2.2 源码之十七Ribbon执行原理三
基本流程图

DynamicServerListLoadBalancer的restOfInit
enableAndInitLearnNewServersFeature开启服务列表更新的定时任务
开启更新定时器:


开启定时器,延迟10秒,间隔30秒,然后执行updateListOfServers方法。


updateListOfServers服务列表更新
定时器开好了,会先进行一次服务列表更新。

就是上篇说的DomainExtractingServerList的getUpdatedListOfServers方法。

DiscoveryEnabledNIWSServerList的obtainServersViaDiscovery
最终到这里,其实就是获取EurekaClient,然后调用他的方法获取服务列表,然后封装成DiscoveryEnabledServer对象,放入serverList集合返回。
private List<DiscoveryEnabledServer> obtainServersViaDiscovery() {
List<DiscoveryEnabledServer> serverList = new ArrayList<DiscoveryEnabledServer>();
...
//获取客户端
EurekaClient eurekaClient = eurekaClientProvider.get();
if (vipAddresses!=null){
for (String vipAddress : vipAddresses.split(",")) {
// if targetRegion is null, it will be interpreted as the same region of client
List<InstanceInfo> listOfInstanceInfo = eurekaClient.getInstancesByVipAddress(vipAddress, isSecure, targetRegion);
for (InstanceInfo ii : listOfInstanceInfo) {
if (ii.getStatus().equals(InstanceStatus.UP)) {
...
DiscoveryEnabledServer des = createServer(ii, isSecure, shouldUseIpAddr);
serverList.add(des);
}
}
if (serverList.size()>0 && prioritizeVipAddressBasedServers){
break; // if the current vipAddress has servers, we dont use subsequent vipAddress based servers
}
}
}
return serverList;
}
最后获取到的集合还要去更新负载均衡器里的集合,也就是BaseLoadBalancer的allServerList,这个就不说了,自己看下好了。
这样ZoneAwareLoadBalancer初始化干的事基本说完了,开启Ping定时任务,和刷新服务列表任务,刷刷一次服务列表,设置负载均衡规则等设置。
好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。
本文深入解析SpringCloud中Ribbon的执行原理,重点讲解DynamicServerListLoadBalancer的基本流程,包括服务列表更新的定时任务和DiscoveryEnabledNIWSServerList的obtainServersViaDiscovery方法。通过定时任务每10秒启动,每30秒执行一次服务列表更新。
2725

被折叠的 条评论
为什么被折叠?



