1.1 About load balancing
Load balancing is generally divided into server-side load balancing and client-side load balancing. The so-called server-side load balancing, such as Nginx and F5, after the request reaches the server, these load balancers will route the request to the target server for processing according to a certain algorithm. .
The so-called client load balancing, such as the Ribbon we want to talk about, the service consumer client will have a server address list, the caller selects a server to access through a certain load balancing algorithm before the request, load balancing The execution of the algorithm is performed on the request client.
Ribbon is a load balancer released by Netflix. Eureka is generally used with Ribbon. Ribbon uses the service information read from Eureka, and when calling the service provided by the service provider, it will load according to a certain algorithm.
1..2 Ribbon Advanced Application
There is no need to introduce additional Jar coordinates, because we have introduced eureka-client in the service consumer, which will introduce Ribbon-related Jar
The code is used as follows, just add the corresponding annotation on the RestTemplate
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
Modify two service providers, one port 8080 and one port 8083, to provide an interface to return the port number of the current instance, which is convenient for observing the load situation
@RestController
@RequestMapping("/demo")
public class DemoController {
@Value("${server.port}")
private Integer port;
@RequestMapping("/server/port")
public Integer demo(){
return port;
}
}
Write a service consumer and configure the service name directly to have the ribbon complete load balancing
@RestController
@RequestMapping("/consumer")
public class RibbonTestController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/check/port")
public Integer findResumeOpenState2() {
Integer port = restTemplate.getForObject("http://ZQLDEMOPROVIDER/demo/server/port", Integer.class);
return port;
}
}
Start each service and test
The service has been registered with the registry
Visit http://localhost:8082/consumer/check/port test
1.3 Ribbon load balancing strategy
Ribbon has a variety of built-in load balancing strategies. The top-level interface responsible for complex balancing is com.netflflix.loadbalancer.IRule, and the class tree is as follows
load balancing strategy |
describe |
RoundRobinRule: polling Strategy |
By default, servers obtained more than 10 times are unavailable and will return an empty server |
RandomRule: random strategy |
If the randomly obtained server is null or unavailable, it will while non-stop loop selection |
RetryRule: retry strategy |
Retry in a loop within a certain time limit. default inheritance RoundRobinRule also supports custom injection, RetryRule will check the elected server after each selection Line judgment, whether it is null, whether it is alive, and within 500ms The internal meeting will continue to select and judge. But RoundRobinRule fails The strategy is more than 10 times, the RandomRule is not expired when The concept of time, as long as the serverList is not linked. |
BestAvailableRule:最⼩ Connection strategy |
Traverse the serverList and select the available one with the smallest number of connections a server. There is a LoadBalancerStats in this algorithm The member variable of will store the running status and connection status of all servers Take the number. If the selected server is null, it will call RoundRobinRule is reselected. |
AvailabilityFilteringRule: Available filtering strategies |
The polling strategy is extended, and a default polling method will be selected first server, and then judge whether the server is available after timeout, the current If the number of connections exceeds the limit, it will return after success. |
ZoneAvoidanceRule:区 domain trade-off policy (default policy) |
The polling strategy is extended to inherit 2 filters: ZoneAvoidancePredicate和 AvailabilityPredicate, except filter timeout and link count More servers will also filter out the zone areas that do not meet the requirements All nodes inside, AWS --ZONE in one area/computer room Polling in the service instance within the |
The load balancing strategy can be modified in the configuration file
#针对的被调⽤⽅微服务名称,不加就是全局⽣效
zql_demo_provider:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #负载策略调整