当前位置:网站首页>Ribbon核心源码解析
Ribbon核心源码解析
2022-06-28 09:56:00 【InfoQ】
spring.factories
- 在spring-cloud-common中,存在自动配置类
LoadBalancerAutoConfiguration
- 在eureka-client中,存在配置类
RibbonEurekaAutoConfiguration
- 在ribbon中,存在配置类
RibbonAutoConfiguration
RibbonEurekaAutoConfiguration
@AutoConfigureAfter
RibbonAutoConfiguration
调用流程
RestTemplate
@LoadBalanced
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
@GetMapping("/test")
public String test(String service){
String result=restTemplate.getForObject("http://eureka-hi/"+service,String.class);
System.out.println(result);
return result;
}
RestTemplate
@LoadBalanced
/**
* Annotation to mark a RestTemplate bean to be configured to use a LoadBalancerClient
* @author Spencer Gibb
*/
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Qualifier
public @interface LoadBalanced {
}
@LoadBalanced
RestTemplate
@LoadBalanced
LoadBalancerAutoConfiguration
LoadBalancerInterceptor
restTemplate
restTemplate
intercept
intercept
RibbonLoadBalancerClient
execute
ILoadBalancer
chooseServer
ILoadBalancer
execute
apply
LoadBalancerContext
reconstructURIWithServer
url
url
AbstractClientHttpRequest
execute
executeInternal
RestTemplate
HttpURLConnection
负载均衡过程
LoadBalancerClient
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/choose")
public String loadBalance(String serviceId){
ServiceInstance instance = loadBalancerClient.choose(serviceId);
System.out.println(instance.getHost()+" "+instance.getPort());
return "ok";
}
LoadBalancerClient
choose
LoadBalancerClient
RibbonLoadBalancerClient
choos
getServer
loadBalancer
ZoneAwareLoadBalancer
allServerList
chooseServer
zone
BaseLoadBalancer
chooseServer
IRule
IRule
ZoneAvoidanceRule
IRule
ZoneAvoidanceRule
choose
PredicateBasedRule
choose
AbstractServerPredicate
chooseRoundRobinAfterFiltering
choose
ILoadBalancer
核心组件ILoadBalancer
LoadBalancer
getServer
getLoadBalancer
ILoadBalancer
BeanFactoryUtil
getBean
getServer
ZoneAwareLoadBalancer
ILoadBalancer
public interface ILoadBalancer {
//往该ILoadBalancer中添加服务
public void addServers(List<Server> newServers);
//选择一个可以调用的实例,keyb不是服务名称,而是zone的id
public Server chooseServer(Object key);
//标记下线服务
public void markServerDown(Server server);
@Deprecated
public List<Server> getServerList(boolean availableOnly);
//获取可用服务列表
public List<Server> getReachableServers();
//获取所有服务列表
public List<Server> getAllServers();
}
ILoadBalancer
IRule
服务获取
RibbonEurekaAutoConfiguration
@Configuration
@EnableConfigurationProperties
@ConditionalOnRibbonAndEurekaEnabled
@AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = EurekaRibbonClientConfiguration.class)
public class RibbonEurekaAutoConfiguration {
}
EurekaRibbonClientConfiguration
ribbonServerList
DiscoveryEnabledNIWSServerList
DiscoveryEnabledNIWSServerList
ServerList
ServerList
public interface ServerList<T extends Server> {
public List<T> getInitialListOfServers();
public List<T> getUpdatedListOfServers();
}
DiscoveryEnabledNIWSServerList
obtainServersViaDiscovery
EurekaClient
EurekaClient
UP
serverList
serverList
DynamicServerListLoadBalancer
setServerList
BaseLoadBalancer
setServersList
BaseLoadBalancer
protected volatile List<Server> allServerList = Collections.synchronizedList(new ArrayList<Server>());
protected volatile List<Server> upServerList = Collections.synchronizedList(new ArrayList<Server>());
setServersList
serverList
allServerList
ZoneAwareLoadBalancer
BaseLoadBalancer
setupPingTask
ping
runPinger
SerialPingStrategy
pingServers
pingServers
NIWSDiscoveryPing
isAlive
NIWSDiscoveryPing
IPing
IPing
isAlive
public interface IPing {
public boolean isAlive(Server server);
}
NIWSDiscoveryPing
isAlive
serverList
UP
Pinger
runPingger
changedServers
newUpList
upServerList
changedServers
results
results
BaseLoadBalancer
setServersList
IPing
PollingServerListUpdater
start
BaseLoadBalancer
setServersList
IPing
- 更新列表
- ping机制
服务选取
IRule
public interface IRule{
public Server choose(Object key);
public void setLoadBalancer(ILoadBalancer lb);
public ILoadBalancer getLoadBalancer();
}
choose
IRule
ZoneAvoidanceRule
zone
IRule
choose
@Bean
IRule
IRule
IRule
ComponentScan
@RibbonClients
@RibbonClients({@RibbonClient(name="eureka-hi",configuration = HiRuleConfig.class),
@RibbonClient(name = "eureka-test",configuration = TestRuleConfig.class)})
public class ServiceFeignApplication {
……
}
总结
- 拦截请求,通过请求中的url地址,截取服务名称
- 通过
LoadBalancerClient
获取ILoadBalancer
- 使用Eureka获取服务列表
- 通过
IRule
负载均衡策略选择具体服务
ILoadBalancer
通过IPing
及定时更新机制来维护服务列表
- 重构该url地址,最终调用
HttpURLConnection
发起请求
边栏推荐
- Unity AssetBundle资源打包与资源加载
- ECS MySQL query is slow
- Application of X6 in data stack index management
- Stutter participle_ Principle of word breaker
- 读取pdf文字和excel写入操作
- fastposter v2.8.4 发布 电商海报生成器
- Solve the problem that the value of the action attribute of the form is null when transferring parameters
- I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
- TCP实战案例之即时通信、BS架构模拟
- Is it safe to open an account with the QR code of CICC securities? Tell me what you know
猜你喜欢
PMP Exam key summary VI - chart arrangement
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
JVM family (2) - garbage collection
错过金三银四,找工作4个月,面试15家,终于拿到3个offer,定级P7+
再见!IE浏览器,这条路由Edge替IE继续走下去
Adapter mode
卸载oracle报错
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
The boss asked me to write an app automation -- yaml file reading -- with the whole framework source code attached
Missed the golden three silver four, found a job for 4 months, interviewed 15 companies, and finally got 3 offers, ranking P7+
随机推荐
Django数据库操作以及问题解决
请教下, 我在本地idea运行flinkcdc的mysql到mysql全量同步,这个是在我本地ide
[Unity]内置渲染管线转URP
Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
引入 flink-sql-mysql-cdc-2.2.1 好多依赖冲突,有解决的吗?
【云驻共创】DWS告警服务DMS详细介绍和集群连接方式简介
错过金三银四,找工作4个月,面试15家,终于拿到3个offer,定级P7+
Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
Caffeine cache, the king of cache, has stronger performance than guava
再见!IE浏览器,这条路由Edge替IE继续走下去
ECS MySQL query is slow
Thread lifecycle
Why does istio use spirit for identity authentication?
MySQL基础知识点总结
Django database operation and problem solving
增强 Jupyter Notebook 的功能,这里有四个妙招
R语言使用car包中的avPlots函数创建变量添加图(Added-variable plots)、在图像交互中,在变量添加图中手动标识(添加)对于每一个预测变量影响较大的强影响点
ffmpeg录音录像
The introduction of flink-sql-mysql-cdc-2.2.1 has solved many dependency conflicts?
第三章 栈和队列