RestTemplate adds @LoadBalanced annotations and uses SpringCloud's Ribbon to achieve automated load balancing

Problem phenomenon:

In the process of learning microservices today, a question was mentioned:

How to simply implement load balancing?


problem analysis:

Through learning everywhere, I learned some methods to achieve load balancing:

For example, you can register and inject the DiscoveryClient object , call the  getInstances("service name") method to obtain a collection of objects with the same service name, and then use the get(index) method to select the service object of the called interface based on the index.  Then pass  new Random().nextInt(instances.size()); Then you can randomly get the index of [0~collection length-1], so as to realize the load balancing of the random strategy!

The implementation process is a bit cumbersome, and I always feel that it is not optimized enough; so I learned another simpler, faster, and optimized method;

Inject the RestTemplate object through registration :

First add the   @LoadBalanced annotation when registering

The annotation can be used springcloud following the integration of Ribbon tool to achieve automated load balancing.

Ribbon has 7 main load balancing strategies:

/* 
   1. RandomRule: 
      randomly select a server. 
      [Randomly on the index, choose the server corresponding to the index] 
   2.BestAvailableRule: 
      Choose a server with the smallest concurrent request. 
      [Inspect the servers one by one, if the server is tripped, ignore it, and then select the server with the smallest ActiveRequestsCount] 
   3. AvailabilityFilteringRule: 
      filter out those backend servers marked as circuit tripped because of continuous connection failures, 
      and filter out those high concurrency The backend server (activeconnections exceeds the configured threshold). 
      [Using an AvailabilityPredicate to include the logic of filtering the server is actually checking the running status of each server recorded in the status] 
   4. WeightedResponseTimeRule: 
      Assign a weight according to the corresponding time, the longer the corresponding time, the smaller the weight, and the possibility of being selected The lower. 
      [A background thread periodically reads the evaluation response time from the status, and calculates a weight for each server. 
      The calculation of Weight is also relatively simple: responsetime minus the average responsetime of each server is the weight of the server.
      When it just started running and no statas is formed, use the roubine strategy to select the server. ] 
   5. RetryRule: 
      Retry mechanism on the selected load balancing strategy machine.
      [During a configuration period of time, when the server is unsuccessful, try to use subRule to select an available server all the time] 
   6. RoundRobinRule: 
      Polling to select the server in polling mode. 
      [Poll the index, select the server corresponding to the index] 
   7.ZoneAvoidanceRule: 
      Compound judgment of the performance of the area where the server is located and the availability of the server to select the server 
      [Use ZoneAvoidancePredicate and AvailabilityPredicate to judge whether to select a server, the 
      previous judgment judges the operation of a zone Whether the performance is available, remove the unavailable zone (all servers), and 
      AvailabilityPredicate is used to filter out servers with too many connections. ] 
 */

There are many ways to configure these policies, such as configuring them in a configuration file.


Solution:

Configure Ribbon's load balancing strategy in the configuration file:

The random strategy succeeds:

Guess you like

Origin blog.csdn.net/weixin_42585386/article/details/109215343