当前位置:网站首页>[microservices 7] in depth analysis of bestavailablerule source code of ribbon load balancing strategy
[microservices 7] in depth analysis of bestavailablerule source code of ribbon load balancing strategy
2022-06-21 21:23:00 【Bald loves fitness】
List of articles
One 、 Preface
In front of Ribbon Related articles :
- 【 Cloud native & Micro service I 】SpringCloud And Ribbon Detailed cases of load balancing ( Integrate Eureka、Ribbon)
- 【 Cloud native & Microservice II 】SpringCloud And Ribbon Custom load balancing policies ( contain Ribbon The core API)
- 【 Cloud native & Microservice III 】SpringCloud And Ribbon This is how to achieve load balancing ( Source analysis @LoadBalanced principle )
- 【 Cloud native & Micro service 4 】SpringCloud And Ribbon and Erueka All the details of integration are here ( Source analysis )
- 【 Micro service five 】Ribbon How to implement the random load balancing algorithm
- 【 Micro service six 】Ribbon Polling of load balancing policy (RoundRobinRule)、 retry (RetryRule)
We talked about the following questions :
- Why give RestTemplate Class with @LoadBalanced Annotations can be used Ribbon Load balancing of ?
- SpringCloud How is it integrated Ribbon Of ?
- Ribbon How to effect RestTemplate Upper ?
- How to get Ribbon Of ILoadBalancer?
- ZoneAwareLoadBalancer( Belong to ribbon) How to communicate with eureka Integrate , adopt eureka client Get the corresponding registry ?
- ZoneAwareLoadBalancer How to continue from Eureka Get the latest registry information in ?
- How to load balancer
ILoadBalancerfrom Eureka Client AcquiredList<Server>Choose one of them Server?- Ribbon How to send network HTTP request ?
- Ribbon How to use IPing The mechanism dynamically checks whether the service instance is alive ?
- Ribbon Random of load balancing policy (
RandomRule)、 polling (RoundRobinRule)、 retry (RetryRule) Realization way ;
This article continues to discuss Best available rules (BestAvailableRule) How is it realized ?
Two 、BestAvailableRule
BestAvailableRule Will examine one by one Server, If Server By tripped 了 , Then skip ; Finally, choose the one with the least concurrent requests Server.
1、 Load rule
We know Ribbon The load balancing algorithm is embodied in IRule Of choose(Object key) In the method , So look BestAvailableRule Of choose(Object key) Method :

The detailed code notes are as follows :
public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule {
// Maintain some status information of the service instance
private LoadBalancerStats loadBalancerStats;
@Override
public Server choose(Object key) {
// If the service instance status information is empty , The parent class's choose() Method , use RoundRobin Algorithm
if (loadBalancerStats == null) {
return super.choose(key);
}
// Get all service instances
List<Server> serverList = getLoadBalancer().getAllServers();
// Minimum number of concurrent connections
int minimalConcurrentConnections = Integer.MAX_VALUE;
// current time
long currentTime = System.currentTimeMillis();
Server chosen = null;
// Traverse each instance
for (Server server: serverList) {
ServerStats serverStats = loadBalancerStats.getSingleServerStat(server);
// If the service instance is tripped 了 , Skip the current service instance directly
if (!serverStats.isCircuitBreakerTripped(currentTime)) {
// Get the concurrent number of instances ( If and only if The time interval between the current time and the last valid change in the number of connections is within the specified range ( Default 10 minute ))
int concurrentConnections = serverStats.getActiveRequestsCount(currentTime);
// Find the service instance with the smallest concurrent connection
if (concurrentConnections < minimalConcurrentConnections) {
minimalConcurrentConnections = concurrentConnections;
chosen = server;
}
}
}
// After traversing all service instances , Not found yet server, Then call the choose() Method , use RoundRobin Algorithm selection .
if (chosen == null) {
return super.choose(key);
} else {
return chosen;
}
}
@Override
public void setLoadBalancer(ILoadBalancer lb) {
super.setLoadBalancer(lb);
if (lb instanceof AbstractLoadBalancer) {
loadBalancerStats = ((AbstractLoadBalancer) lb).getLoadBalancerStats();
}
}
}
The core logic of the method :
- First, judge if the service instance status information is empty , The parent class's choose() Method , use RoundRobin Algorithm .
- otherwise : from BestAvailableRule Of ILoadBalancer Get all instances of the service in , Record current time ;
- Traverse each instance of the service , Get instance of
ServerStats, If the instance is tripped 了 , Skip the current service instance directly ; otherwise , Get the concurrent number of instances ( Here if and only if The time interval between the current time and the last valid change in the number of connections is within the specified range ( Default 10 minute )), IfIf it exceeds the time range, it returns 0. After the loop ends, the first instance with the smallest number of concurrency is returned .- Last , After traversing all service instances , I haven't got Server, Then call its parent class's choose() Method , Use RoundRobin The algorithm selects an instance .
Let's move on to a few details : How to judge whether a service instance is tripped? How to obtain the concurrent number of service instances ?
2、 How to judge whether a service instance is tripped?

Logic is embodied in ServerStats Of isCircuitBreakerTripped(long currentTime) In the method :
public boolean isCircuitBreakerTripped(long currentTime) {
// Get the time-out point of the circuit breaker
long circuitBreakerTimeout = getCircuitBreakerTimeout();
// If the circuit breaker times out <= 0, Then return directly false.
if (circuitBreakerTimeout <= 0) {
return false;
}
// If the circuit breaker times out > current time , Then return to true, Indicates that the service instance is tripped 了 ; Otherwise return to false
return circuitBreakerTimeout > currentTime;
}
Method core logic :
- Judge whether the timeout time point of the circuit breaker is greater than the current time , If it is greater than , Indicates that the current service instance is tripped 了 , Will no longer be chosen ; No , Normal choice .
3、 How to obtain the concurrent number of service instances ?

Logic is embodied in ServerStats Of getActiveRequestsCount(long currentTime) In the method :
public int getActiveRequestsCount(long currentTime) {
// Get the current number of concurrent connections of the instance
int count = activeRequestsCount.get();
// The number of connections is 0, Then return directly 0
if (count == 0) {
return 0;
// If the time interval between the current time and the last valid change in the number of connections is not within the specified range ( Default 10 minute ), The number of concurrent connections is set to 0, And back to 0
} else if (currentTime - lastActiveRequestsCountChangeTimestamp > activeRequestsCountTimeout.get() * 1000 || count < 0) {
activeRequestsCount.set(0);
return 0;
} else {
// The number of concurrent connections returned in a normal scenario
return count;
}
}
AtomicInteger activeRequestsCount = new AtomicInteger(0);
private static final DynamicIntProperty activeRequestsCountTimeout =
DynamicPropertyFactory.getInstance().getIntProperty("niws.loadbalancer.serverStats.activeRequestsCount.effectiveWindowSeconds", 60 * 10);
The key point is how to maintain the concurrency number of instances ? Now I will continue to look at .
4、 Maintenance of instance concurrency :
1) Increase the number of concurrent instances
At the beginning of the execution of a Rest The request will be passed ServerStats#incrementActiveRequestsCount() Method to add a new connection number (activeRequestsCount);
Though it is new One RibbonStatsRecorder The number of concurrent instances added during , however RibbonStatsRecorder Combined inside ServerStats originate Ribbo The context of RibbonLoadBalancerContext, So every time new RibbonStatsRecorder when ,ServerStats Data is shared ;

2) Reduce the number of concurrent instances
When Rest After the request to call the external service is completed , Will pass ServerStats#decrementActiveRequestsCount() Method to reduce the number of connections (activeRequestsCount):
RibbonStatsRecorder#recordStats(Object entity) The method is as follows :

3、 ... and 、 Subsequent articles
In the next article, we will continue to analyze Ribbon Load balancing strategy WeightedResponseTimeRule.
边栏推荐
- #夏日挑战赛# 用OpenHarmony eTS 实现一个Huawei app标准布局
- C语言回调函数到底是怎么回事?
- evaluating expression ‘ew.sqlSegment != null and ew.sqlSegment != ‘‘ and ew. mybaties plus问题
- Leecode70 climbing stairs
- ctfshow 105-127
- Golang learning notes - pointer
- Lvs+keepalived high availability cluster deployment
- pc 电商平台----search模块
- Dedecms dream weaving background system adds its own column menu
- K - Clairewd’s message HDU - 4300 (EXKMP)
猜你喜欢

基于 PCA 的人脸识别系统及人脸姿态分析

最详细整理STL之vector基础

11、 Beautify the interface

欢迎使用Markdown编辑器

【owt】p2p Signaling Server 运行
![[Patents and papers-19]: Notice on electronic information application of Nanjing City, Jiangsu Province in 2022 (medium and advanced)](/img/a5/47c1b189cfd4bee6edb68c266a3816.png)
[Patents and papers-19]: Notice on electronic information application of Nanjing City, Jiangsu Province in 2022 (medium and advanced)

总结了嵌入式开发中几种常见的设备通信协议

Several common device communication protocols in embedded development are summarized

集群二---LVS负载均衡群集DR模式

Cluster 2 - LVS load balancing cluster Dr mode
随机推荐
Cluster I - LVS Load Balancing Cluster Nat Mode and LVS Load Balancing Field Deployment
evaluating expression ‘ew. sqlSegment != null and ew. sqlSegment != ‘‘ and ew. Mybats plus problems
Mysql database - Database Foundation
Golang learning notes - pointer
Principle and application of user mode hot patch
ADUM1401ARWZ-RL 亚德诺 数字信号隔离模块
期货开户平台哪家好?安全正规的期货公司有哪些?
C语言回调函数到底是怎么回事?
数据可视化工具软件
JVM的类加载过程
MySQL数据库---数据库基础
Book list given by Wu Jun to college students
4.3寸触摸屏智能网络中控主机有哪些应用
Mysql database - index
[parallel and distributed computing] 10B_ MapReduce GFS Implementation
Kubernetes-23:详解如何将CPU Manager做到游刃有余
libtorch显存管理示例
Common cases of hybrid cloud exercises
RDKit | 分子所具有的自由基电子数、价电子数
How functions are declared