当前位置:网站首页>Ribbon核心源码解析
Ribbon核心源码解析
2022-06-28 09:56:00 【InfoQ】
spring.factories- 在spring-cloud-common中,存在自动配置类
LoadBalancerAutoConfiguration
- 在eureka-client中,存在配置类
RibbonEurekaAutoConfiguration
- 在ribbon中,存在配置类
RibbonAutoConfiguration
RibbonEurekaAutoConfiguration@AutoConfigureAfterRibbonAutoConfiguration调用流程
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 {
}
@LoadBalancedRestTemplate@LoadBalancedLoadBalancerAutoConfigurationLoadBalancerInterceptorrestTemplaterestTemplateinterceptinterceptRibbonLoadBalancerClientexecuteILoadBalancerchooseServerILoadBalancerexecuteapplyLoadBalancerContextreconstructURIWithServerurlurlAbstractClientHttpRequestexecuteexecuteInternalRestTemplateHttpURLConnection负载均衡过程
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";
}
LoadBalancerClientchooseLoadBalancerClientRibbonLoadBalancerClientchoosgetServerloadBalancerZoneAwareLoadBalancerallServerListchooseServerzoneBaseLoadBalancerchooseServerIRuleIRuleZoneAvoidanceRuleIRuleZoneAvoidanceRulechoosePredicateBasedRulechooseAbstractServerPredicatechooseRoundRobinAfterFilteringchooseILoadBalancer核心组件ILoadBalancer
LoadBalancergetServergetLoadBalancerILoadBalancerBeanFactoryUtilgetBeangetServerZoneAwareLoadBalancerILoadBalancerpublic 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();
}
ILoadBalancerIRule服务获取
RibbonEurekaAutoConfiguration@Configuration
@EnableConfigurationProperties
@ConditionalOnRibbonAndEurekaEnabled
@AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = EurekaRibbonClientConfiguration.class)
public class RibbonEurekaAutoConfiguration {
}
EurekaRibbonClientConfigurationribbonServerListDiscoveryEnabledNIWSServerListDiscoveryEnabledNIWSServerListServerListServerListpublic interface ServerList<T extends Server> {
public List<T> getInitialListOfServers();
public List<T> getUpdatedListOfServers();
}
DiscoveryEnabledNIWSServerListobtainServersViaDiscoveryEurekaClientEurekaClientUPserverListserverListDynamicServerListLoadBalancersetServerListBaseLoadBalancersetServersListBaseLoadBalancerprotected volatile List<Server> allServerList = Collections.synchronizedList(new ArrayList<Server>());
protected volatile List<Server> upServerList = Collections.synchronizedList(new ArrayList<Server>());
setServersListserverListallServerListZoneAwareLoadBalancerBaseLoadBalancersetupPingTaskpingrunPingerSerialPingStrategypingServerspingServersNIWSDiscoveryPingisAliveNIWSDiscoveryPingIPingIPingisAlivepublic interface IPing {
public boolean isAlive(Server server);
}
NIWSDiscoveryPingisAliveserverListUPPingerrunPinggerchangedServersnewUpListupServerListchangedServersresultsresultsBaseLoadBalancersetServersListIPingPollingServerListUpdaterstartBaseLoadBalancersetServersListIPing- 更新列表
- ping机制
服务选取
IRulepublic interface IRule{
public Server choose(Object key);
public void setLoadBalancer(ILoadBalancer lb);
public ILoadBalancer getLoadBalancer();
}
chooseIRuleZoneAvoidanceRulezoneIRulechoose@BeanIRuleIRuleIRuleComponentScan@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发起请求
边栏推荐
- 再见!IE浏览器,这条路由Edge替IE继续走下去
- Unity 从服务器加载AssetBundle资源写入本地内存,并将下载保存的AB资源从本地内存加载至场景
- Correct conversion between JSON data and list collection
- 接口自动化框架脚手架-利用反射机制实现接口统一发起端
- Caffeine cache, the king of cache, has stronger performance than guava
- Install using snap in opencloudos NET 6
- 栈的弹出压入序列<难度系数>
- Restful style
- Google开源依赖注入框架-Guice指南
- 优秀笔记软件盘点:好看且强大的可视化笔记软件、知识图谱工具Heptabase、氢图、Walling、Reflect、InfraNodus、TiddlyWiki
猜你喜欢

为什么 Istio 要使用 SPIRE 做身份认证?

Numpy array: join, flatten, and add dimensions

As shown in the figure, the SQL row is used to convert the original table of Figure 1. Figure 2 wants to convert it

Understand 12 convolution methods (including 1x1 convolution, transpose convolution and deep separable convolution)

Matplotlib attribute and annotation

Installing redis under Linux and windows (ultra detailed graphic tutorial)

Key summary V of PMP examination - execution process group

Dotnet uses crossgen2 to readytorun DLL to improve startup performance

代理模式(Proxy)

dotnet 使用 Crossgen2 对 DLL 进行 ReadyToRun 提升启动性能
随机推荐
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
Proxy mode (proxy)
Cisco * VRF (virtual route forwarding table)
读取pdf图片并识别内容
Installing redis under Linux and windows (ultra detailed graphic tutorial)
云服务器MYSQL查询速度慢
老板叫我写个APP自动化--Yaml文件读取--内附整个框架源码
==And eqauls()
To enhance the function of jupyter notebook, here are four tips
引入 flink-sql-mysql-cdc-2.2.1 好多依赖冲突,有解决的吗?
Unity AssetBundle资源打包与资源加载
通过PyTorch构建的LeNet-5网络对手写数字进行训练和识别
读取pdf文字和excel写入操作
栈的弹出压入序列<难度系数>
Discard Tkinter! Simple configuration to quickly generate cool GUI!
卸载oracle报错
代理模式(Proxy)
Read PDF image and identify content
Key summary VII of PMP examination - monitoring process group (1)
Generate token