当前位置:网站首页>Ribbon execution logic source code analysis
Ribbon execution logic source code analysis
2022-07-25 22:41:00 【caicongyang】
1.org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration
take LoadBalancerInterceptor Injection into RestTemplate in
@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
adopt loadBalanceClient True execution execute Method
@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
from nocas/eureka According to the serviceId obtain Server Relevant information
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);
}
}from 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));
}The above code new obtain 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 yes ServerList Implementation class
Configure in 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;
}
}
边栏推荐
- 【集训DAY12】树!树!树!【贪心】【最小生成树】
- Solve several common problems
- 面向领域模型编程
- Explore the use of self increasing and self decreasing operators
- Dom and events
- [PMP learning notes] Chapter 1 Introduction to PMP System
- The new media operation strategy (taking xiaohongshu as an example) helps you quickly master the creative method of popular models
- ML-Numpy
- Today, learn about the use of lists, hyperlinks, image tags, and audio and video
- Platform architecture construction
猜你喜欢

【集训DAY12】树!树!树!【贪心】【最小生成树】

编译器引论
![[training Day11] Calc [mathematics]](/img/a7/cbb33f0c241e1bad90a282bba990d1.png)
[training Day11] Calc [mathematics]

Arcgis10.2 configuring postgresql9.2 standard tutorial

ML-Numpy

1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮

(1) DDL, DML, DQL, DCL and common data types

Vodak software: Smart City solution

Qt的TQTreeWidget控件

Basic principle of torque motor control
随机推荐
Pyspark data analysis basis: pyspark.sql.sparksession class method explanation and operation + code display
Share two music playing addresses
JSON object
[database learning] redis parser & single thread & Model
ECMA 262 12 Lexical Grammer
ML-Numpy
【数据库学习】Redis 解析器&&单线程&&模型
PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示
Solve several common problems
Kibana~后台启动Kibana之后无法找到进程号
721. Account consolidation ●●, and collection
Interpretation of English terms
【集训DAY15】好名字【hash】
Mitsubishi FX PLC free port RS command realizes Modbus Communication
LabVIEW develops PCI-1680U dual port can card
Compile and decompile
Qt的TQTreeWidget控件
IPv4地址已经完全耗尽,互联网还能正常运转,NAT是最大功臣!
分割金条的代价
To light up all the positions in the string that need to be lit, at least a few lights are needed