当前位置:网站首页>In depth exploration of ribbon load balancing
In depth exploration of ribbon load balancing
2022-07-26 17:16:00 【Liu Chu, Ge Nian】
List of articles
1. Ribbon Load balancing
Study Eruaka When , We added @LoadBalanced annotation , Load balancing function can be realized , What's the principle ?
1.1 Load balancing principle
SpringCloud The bottom layer is actually using a program called Ribbon The components of , To realize the load balancing function .

Then our request is clearly http://userservice/user/1, How did it become http://localhost:8081 What about ?
1.2 Source tracking
Why do we just input service Name can be accessed ? We need to get ip And port .
Obviously someone helped us according to service name , Get the ip And port . It is LoadBalancerInterceptor, This class will be in the right RestTemplate Request to intercept , And then from Eureka According to the service id Get a list of services , Then we use the load balancing algorithm to get the real service address information , Replace service id.
We are right. LoadBalancerInterceptor Source code tracking :
1)LoadBalancerInterceptor

You can see... Here intercept Method , Intercepted the user's HttpRequest request , Then I did a few things :
request.getURI(): Get request uri, In this case http://user-service/user/8originalUri.getHost(): obtain uri Hostname of the path , It's actually service id,user-servicethis.loadBalancer.execute(): Processing services id, And user requests .
there this.loadBalancer yes LoadBalancerClient type , Let's keep up with execute.
1)LoadBalancerClient
find LoadBalancerClient Interface , Continue to follow in execute Method , Find the subclass that implements the interface :

The code looks like this :
- getLoadBalancer(serviceId): According to the service id obtain ILoadBalancer, and ILoadBalancer Will take the service id Go to eureka Get the list of services in and save it .
- getServer(loadBalancer): Use the built-in load balancing algorithm , Select a... From the list of services . In this case , You can see the capture 8082 Service of port
After release , Visit and track again , We found that 8081:

Sure enough, load balancing is realized .
3) Load balancing strategy IRule
In the code just now , You can see that getting the service through a getServer Method to do load balancing :

Let's keep up with :

Keep track of source code chooseServer Method , Found such a piece of code :

Let's look at this rule Who is it? :

there rule The default is a RoundRobinRule, See the introduction of class :

That's what polling means .
Come here , We know the whole process of load balancing .
4) summary
SpringCloudRibbon The bottom layer of the uses an interceptor , Intercepted RestTemplate Requests made , Changed the address . Summarize with a picture :
The basic process is as follows :
- Intercept our RestTemplate request http://userservice/user/1
- RibbonLoadBalancerClient Will request from url Get the service name from , That is to say user-service
- DynamicServerListLoadBalancer according to user-service To eureka Pull the service list
- eureka Returns a list of ,localhost:8081、localhost:8082
- IRule Leverage built-in load balancing rules , Select one from the list , for example localhost:8081
- RibbonLoadBalancerClient Modify request address , use localhost:8081 replace userservice, obtain http://localhost:8081/user/1, Make a real request
1.3 Load balancing strategy
1.3.1. Load balancing strategy
The rules of load balancing are defined in IRule Interface , and IRule There are many different implementation classes :
The meaning of different rules is as follows :
| Built in load balancing rule class | Rule description |
|---|---|
| RoundRobinRule | Simply poll the list of services to select the server . It is Ribbon Default load balancing rules . |
| AvailabilityFilteringRule | Ignore the following two servers : (1) By default , This server if 3 Secondary connection failed , This server will be set to “ A short circuit ” state . The short circuit condition will continue 30 second , If the connection fails again , The duration of the short circuit will increase geometrically . (2) Servers with too many concurrent . If a server has too many concurrent connections , Configured with AvailabilityFilteringRule The client side of the rule will also ignore it . Maximum number of concurrent connections , It can be done by ..ActiveConnectionsLimit Property configuration . |
| WeightedResponseTimeRule | Give each server a weight value . The longer the server response time , The less weight this server has . This rule randomly selects servers , This weight value will affect the choice of the server . |
| ZoneAvoidanceRule | Select servers based on servers available in the region . Use Zone Classify servers , This Zone It can be understood as a computer room 、 A rack, etc . And then on Zone Polling multiple services in the . |
| BestAvailableRule | Ignore those short circuited servers , And choose a server with a lower number of concurrent . |
| RandomRule | Randomly select an available server . |
| RetryRule | The choice logic of retry mechanism |
The default implementation is ZoneAvoidanceRule, Is a polling scheme
1.3.2. Custom load balancing policies
By defining IRule The implementation can modify the load balancing rules , There are two ways :
Code mode : stay order-service Medium OrderApplication Class , Define a new IRule:
@Bean public IRule randomRule(){ return new RandomRule(); }Configuration file mode : stay order-service Of application.yml In file , Adding a new configuration can also modify the rules :
userservice: # Configure load balancing rules for a microservice , Here is userservice service ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # Load balancing rules
Be careful , Generally, the default load balancing rules are used , No modification .
1.4 Hunger is loaded
Ribbon The default is lazy loading , That is, the first time you visit, you will create LoadBalanceClient, The request will take a long time .
First request :1s
It's very fast to ask again :

Hunger loading will be created when the project starts , Reduce the time-consuming of the first visit , Enable hungry loading through the following configuration :
ribbon:
eager-load:
enabled: true
clients: userservice
边栏推荐
- 浅谈云原生边缘计算框架演进
- Tcpdump命令详解
- [Luogu p8063] shortest paths (graph theory)
- There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
- The first case in Guangdong! A company in Guangzhou was punished by the police for failing to fulfill its obligation of data security protection
- 正则表达式
- [ctfshow-web]反序列化
- E-week finance | postal Huiwanjia bank opened; Wechat public call to stop four types of financial marketing publicity
- 抓包与发流软件与网络诊断
- Marxan model, reserve optimization and protection vacancy selection technology, application in invest ecosystem
猜你喜欢

"Green is better than blue". Why is TPC the last white lotus to earn interest with money

【开发教程8】疯壳·开源蓝牙心率防水运动手环-三轴计步伐

In May, 2022, video user insight: user use time increased, and the platform achieved initial results in cost reduction and efficiency increase

How does win11 reinstall the system?

How emqx 5.0 under the new architecture of mria+rlog realizes 100million mqtt connections

Use verdaccio to build your own NPM private library

Can TCP and UDP use the same port?

UPC 2022 summer personal training game 07 (part)

【开发教程7】疯壳·开源蓝牙心率防水运动手环-电容触摸

2022软件测试技能 Postman+newman+jenkins 持续集成 实战教程
随机推荐
The principle of reliable transmission in TCP protocol
Win11怎么重新安装系统?
2022 software testing skills postman+newman+jenkins continuous integration practical tutorial
Analysis of the advantages of eolink and JMeter interface testing
Using MySQL master-slave replication delay to save erroneously deleted data
ES:Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes
How can win11 system be reinstalled with one click?
Realizing DDD based on ABP -- related concepts of DDD
PXE efficient batch network installation
操作系统迁移实战之在openEuler上部署MySQL数据库
Marketing guide | several common micro blog marketing methods
6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
It turns out that cappuccino information security association does this. Let's have a look.
Win11自动删除文件设置方法
regular expression
The user experience center of Analysys Qianfan bank was established to help upgrade the user experience of the banking industry
Chapter 1 Overview - Section 1 - 1.3 composition of the Internet
Small application of C language using structure to simulate election
[basic course of flight control development 1] crazy shell · open source formation UAV GPIO (LED flight information light and signal light control)
Definition and relationship of derivative, differential, partial derivative, total derivative, directional derivative and gradient