当前位置:网站首页>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发起请求
边栏推荐
- Dear leaders, ask me if MySQL does not support early_ Offset mode? Unsupported star
- 满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
- 接口自动化框架脚手架-利用反射机制实现接口统一发起端
- Stutter participle_ Principle of word breaker
- 各位大佬,问下Mysql不支持EARLIEST_OFFSET模式吗?Unsupported star
- Cisco * VRF(虚拟路由转发表)
- JVM family (2) - garbage collection
- 代理模式(Proxy)
- 适配器模式(Adapter)
- Thread lifecycle
猜你喜欢

Ideal interface automation project

再见!IE浏览器,这条路由Edge替IE继续走下去

函数的分文件编写

【OpenCV 例程200篇】213. 绘制圆形

Unity AssetBundle asset packaging and asset loading

PyGame game: "Changsha version" millionaire started, dare you ask? (multiple game source codes attached)

使用 ABAP 操作 Excel 的几种方法
![[200 opencv routines] 213 Draw circle](/img/8d/a771ea7008f84ae3a3cf41507448ec.png)
[200 opencv routines] 213 Draw circle

What is the best way to learn machine learning

用 Compose 实现个空调,为你的夏日带去清凉
随机推荐
flink cep 跳过策略 AfterMatchSkipStrategy.skipPastLastEvent() 匹配过的不再匹配 碧坑指南
ffmpeg录音录像
Unity AssetBundle资源打包与资源加载
Regular verification of mobile phone number and email [easy to understand]
Methods for creating multithreads ---1 creating subclasses of thread class and multithreading principle
Proxy mode (proxy)
六月集训(第28天) —— 动态规划
Correct conversion between JSON data and list collection
Flip CEP skip policy aftermatchskipstrategy Skippastlastevent() matched no longer matches the Bikeng Guide
代理模式(Proxy)
Xiaomi's payment company was fined 120000 yuan, involving the illegal opening of payment accounts, etc.: Lei Jun is the legal representative, and the products include MIUI wallet app
Sqlcmd database connection error
Matplotlib attribute and annotation
通过PyTorch构建的LeNet-5网络对手写数字进行训练和识别
[NLP] this year's college entrance examination English AI score is 134. The research of Fudan Wuda alumni is interesting
接口自动化框架脚手架-参数化工具的实现
HDI blind hole design, have you noticed this detail?
The R language uses the avplots function in the car package to create added variable plots. In image interaction, manually identify (add) strong influence points that have a great impact on each predi
PMP examination key summary VIII - monitoring process group (2)
Is it safe to open an account with the QR code of CICC securities? Tell me what you know