Spring cloud client load balancing ribbon in-depth understanding
1、 Load balancing
load balancing (load balance): Based on the existing network structure, it provides a cheap, effective and transparent method to expand the bandwidth of network equipment and servers, increase throughput, strengthen network data processing capacity, and improve network flexibility and availability. It means to allocate it to multiple operating units for execution, such as web servers, FTP servers, and enterprise key applications Server and other key task servers, so as to complete the work tasks together.
1. Server side load balancing: the client requests to the load balancing server, and the load balancing server transfers the request to a server that really provides services according to its own algorithm. The server will respond to the data to the load balancing server, and the load balancing server finally returns the data to the customer service side. (Nginx)
2. Load balancing at the customer service side: load balancing based on the client side. In short, set a scheduling algorithm in the client program. When sending a request to the server, first execute the scheduling algorithm to calculate which server to send the request to, and then send the request to the server.
Based on the characteristics of client load balancing:
It is implemented by the internal program of the client without additional software and hardware investment of the load balancer.
The problem that the business server is unavailable needs to be solved inside the program, and the server failure has little transparency to the application.
The problem of pressure overload of business server needs to be solved inside the program.
2、 Ribbon realizes load balancing of client
We use spring boot to test.
POM file:
application. yml
Ribbon's load balancing strategy
1. Roundrobin rule (polling mode)
Public class roundrobin rule extensions abstractloadbalancerrule roundrobin polling select server to poll index and select the server corresponding to index. This policy is also the default policy of ribbon
SpringCloudRibbonApplication. java
2. Randomrule (random policy)
Public class randomrule extensions abstractloadbalancerrule randomly select a server on the index and randomly select the server corresponding to the index.
In the configuration file application YML join
In springcloudribbonapplication Add in Java
Results of 6 executions: @ h_ 404_ 99@
3. Bestavailablerule (concurrency)
Public class bestavailablerule extensions clientconfignenabledroundrobinrule select the server with the smallest concurrent request and check the server one by one. If the server is tripped, ignore it and select the server with the smallest activerequestscount
In the configuration file application YML join
NFLoadBalancerRuleClassName: com. netflix. loadbalancer. BestAvailableRule
In springcloudribbonapplication Add in Java
Results of 6 executions:
4. Availability filtering rule (server status)
Public class availabilityfiltering rule extensions predicatebasedrule filters out backend servers marked as circuit tripped because of persistent connection failures, And filter out those highly concurrent back-end servers (active connections exceed the configured threshold). Use an availability predict to include the logic of filtering servers. In fact, it is to check the running status of each server recorded in status
5. Weightedresponsetimerule (based on response time)
Public class weightedresponsetimerule extends roundrobin rule assigns a weight according to the response time. The longer the corresponding time, the smaller the weight, and the lower the possibility of being selected. A background thread periodically reads the evaluation response time from status and calculates a weight for each server. The calculation of weight is also relatively simple. The responsetime minus the average responsetime of each server is the weight of the server. When you start running and no statas are formed, use the roubine policy to select the server.
6. Retryrule (based on policy + retry)
Public class retryrule extends abstractloadbalancerrule the on-board retry mechanism for the selected load balancing policy@ H_ 404_ 99 @ in a configuration period, if the selection of server is unsuccessful, always try to select an available server by using the subrule method
7. Zoneavoidancerule (zone status + service status)
public class ZoneAvoidanceRule extends PredicateBasedRule @H_ 404_ 99 @ judge the performance of the region where the server is located and the availability of the server, and select server @ H_ 404_ 99 @ use zoneavoidancepredict and availabilitypredicte to determine whether to select a server. The previous judgment determines whether the running performance of a zone is available, and excludes unavailable zones (all servers). Availabilitypredicte is used to filter out servers with too many connections.
4. 5, 6 and 7 these strategies are used in the same way as the above methods, which are not demonstrated here
The above in-depth understanding of the spring cloud client load balancing ribbon is all that Xiaobian has shared with you. I hope it can give you a reference and support more programming tips.