当前位置:网站首页>ribbon 执行逻辑源码解析
ribbon 执行逻辑源码解析
2022-07-25 22:39:00 【caicongyang】
1.org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration
将LoadBalancerInterceptor 注入到RestTemplate中
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingClass("org.springframework.retry.support.RetryTemplate")
static class LoadBalancerInterceptorConfig {
@Bean
public LoadBalancerInterceptor loadBalancerInterceptor(
LoadBalancerClient loadBalancerClient,
LoadBalancerRequestFactory requestFactory) {
return new LoadBalancerInterceptor(loadBalancerClient, requestFactory);
}
@Bean
@ConditionalOnMissingBean
public RestTemplateCustomizer restTemplateCustomizer(
final LoadBalancerInterceptor loadBalancerInterceptor) {
return restTemplate -> {
List<ClientHttpRequestInterceptor> list = new ArrayList<>(
restTemplate.getInterceptors());
list.add(loadBalancerInterceptor);
restTemplate.setInterceptors(list);
};
}
}2.org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor
通过loadBalanceClient 真正执行execute 方法
@Override
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body,
final ClientHttpRequestExecution execution) throws IOException {
final URI originalUri = request.getURI();
String serviceName = originalUri.getHost();
Assert.state(serviceName != null,
"Request URI does not contain a valid hostname: " + originalUri);
return this.loadBalancer.execute(serviceName,
this.requestFactory.createRequest(request, body, execution));
}3.org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient
从nocas/eureka中根据serviceId获取Server相关的信息
public <T> T execute(String serviceId, LoadBalancerRequest<T> request, Object hint) throws IOException {
ILoadBalancer loadBalancer = this.getLoadBalancer(serviceId);
Server server = this.getServer(loadBalancer, hint);
if (server == null) {
throw new IllegalStateException("No instances available for " + serviceId);
} else {
RibbonLoadBalancerClient.RibbonServer ribbonServer = new RibbonLoadBalancerClient.RibbonServer(serviceId, server, this.isSecure(server, serviceId), this.serverIntrospector(serviceId).getMetadata(server));
return this.execute(serviceId, (ServiceInstance)ribbonServer, (LoadBalancerRequest)request);
}
}从org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration
@Bean
@ConditionalOnMissingBean
public ILoadBalancer ribbonLoadBalancer(IClientConfig config, ServerList<Server> serverList, ServerListFilter<Server> serverListFilter, IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
return (ILoadBalancer)(this.propertiesFactory.isSet(ILoadBalancer.class, this.name) ? (ILoadBalancer)this.propertiesFactory.get(ILoadBalancer.class, config, this.name) : new ZoneAwareLoadBalancer(config, rule, ping, serverList, serverListFilter, serverListUpdater));
}上面的代码new 获取ZoneAwareLoadBalancer
com.netflix.loadbalancer.DynamicServerListLoadBalancer#updateListOfServers
@VisibleForTesting
public void updateListOfServers() {
List<T> servers = new ArrayList();
if (this.serverListImpl != null) {
servers = this.serverListImpl.getUpdatedListOfServers();
LOGGER.debug("List of Servers for {} obtained from Discovery client: {}", this.getIdentifier(), servers);
if (this.filter != null) {
servers = this.filter.getFilteredListOfServers((List)servers);
LOGGER.debug("Filtered List of Servers for {} obtained from Discovery client: {}", this.getIdentifier(), servers);
}
}
this.updateAllServerList((List)servers);
}serverListImpl 是 ServerList实现类
配置在org.springframework.cloud.netflix.ribbon.eureka.EurekaRibbonClientConfiguration
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config, Provider<EurekaClient> eurekaClientProvider) {
if (this.propertiesFactory.isSet(ServerList.class, this.serviceId)) {
return (ServerList)this.propertiesFactory.get(ServerList.class, config, this.serviceId);
} else {
DiscoveryEnabledNIWSServerList discoveryServerList = new DiscoveryEnabledNIWSServerList(config, eurekaClientProvider);
DomainExtractingServerList serverList = new DomainExtractingServerList(discoveryServerList, config, this.approximateZoneFromHostname);
return serverList;
}
}
边栏推荐
- 【集训DAY15】Boring【树形DP】
- Pyspark data analysis basis: pyspark.sql.sparksession class method explanation and operation + code display
- Wechat official account application development (I)
- Method of converting MAPGIS format to ArcGIS
- 分割金条的代价
- Dom and events
- 【集训DAY15】好名字【hash】
- Today, let's talk about the if branch statement of the selection structure
- 数据质量:数据治理的核心
- Document flow definition, box model related knowledge
猜你喜欢

力矩电机控制基本原理

MatrixCube揭秘102——300行实现的完整分布式存储系统MatrixKV

【集训DAY15】油漆道路【最小生成树】
![[training day13] Internet [concurrent search]](/img/c6/327095c3ed3a0d4e2b034ff164a7af.png)
[training day13] Internet [concurrent search]

自媒体人必备的4个资源工具,每一个都很实用

Pyspark data analysis basis: pyspark.sql.sparksession class method explanation and operation + code display

(1) Integrating two mapping frameworks of Dao

科大讯飞智能办公本Air电纸书阅读器,让我的工作生活更加健康

Build commercial projects based on ruoyi framework

Method of converting MAPGIS format to ArcGIS
随机推荐
Interpretation of the source code of all logging systems in XXL job (line by line source code interpretation)
Can generic types be used in array
对需求的内容进行jieba分词并按词频排序输出excel文档
If jimureport building block report is integrated according to the framework
软件测试 pytest pytest的命名规则 用例的前后置 conftest.py 定制allure报告 @pytest.mark.parametrize()装饰器作数据驱动
Arcgis10.2 configuring postgresql9.2 standard tutorial
【MySQL提权】UDF提权(附带大马)
PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示
Learning orientation today
Data quality: the core of data governance
【集训DAY15】油漆道路【最小生成树】
Scratch seamless butt bron filter
Matrixcube unveils the complete distributed storage system matrixkv implemented in 102-300 lines
【集训DAY11】Nescafe【贪心】
【PMP学习笔记】第1章 PMP体系引论
力矩电机控制基本原理
[training Day12] x equation [high precision] [mathematics]
【集训DAY13】Travel【暴力】【动态规划】
ML-Numpy
Explore the use of self increasing and self decreasing operators