Customize the Ribbon load balancing strategy through configuration files in spring cloud

1. Load balancing strategy in Ribbon

1. Load balancing strategies supported in Ribbon

AvailabilityFilteringRule: Filter out those backend servers that are marked as circuit tripped because of constant connection failures, and filter out those backend servers with high concurrency (active connections exceed the configured threshold) | Use an AvailabilityPredicate to contain the logic of filtering servers, In fact, it is to check the running status of each server recorded in the status.

RandomRule: randomly select a server

BestAvailable: Select a server with the smallest concurrent requests, and examine the servers one by one. If the server is tripped, ignore it

RoundRobinRule: roundRobin mode polling selection, polling index, select the server corresponding to the index

WeightedResponseTimeRule: Assign a weight (weight) according to the response time. The longer the response time, the smaller the weight and the lower the possibility of being selected.

RetryRule: On-device retry mechanism for the selected load balancing strategy. When the server selection fails within a configuration period, it will always try to select an available server by using subRule.

ZoneAvoidanceRule: Combine the performance of the region where the server is located and the availability of the server to select the server

ResponseTimeWeightedRule: The function is the same as WeightedResponseTimeRule, and the two functions are the same. ResponseTimeWeightedRule was later renamed WeightedResponseTimeRule

2. Verification

1. Custom load balancing strategy

  1. # custom load balancing strategy  
    springboot -h2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule // Customize the use of random strategy, springboot-h2 is the service application name
     

2. Modify the calling code

copy code
package com.chhliu.springboot.restful.controller;  
  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.cloud.client.ServiceInstance;  
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RestController;  
import org.springframework.web.client.RestTemplate;  
  
import com.chhliu.springboot.restful.vo.User;  
  
@RestController  
public  class RestTemplateController {  
    @Autowired  
    private RestTemplate restTemplate;  
      
    @Autowired  
    private LoadBalancerClient loadBalancerClient;  
      
    @GetMapping("/template/{id}")  
    public User findById(@PathVariable Long id) {  
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("springboot-h2");  
        System.out.println("===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"  
                + serviceInstance.getPort()); // Print the information of the currently called service   
        User u = this .restTemplate.getForObject("http://springboot-h2/user/" + id, User.class ) ;  
        System.out.println(u);  
        return u;  
    }  
}  
copy code

 

3. Test

 

The service invocation relationship is as follows:

 

http://www.cnblogs.com/ilinuxer/p/6582784.html

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327099924&siteId=291194637