Article directory
Introduction to Ribbon
Spring Cloud Ribbon is a client-side load balancing tool based on HTTP and TCP, which is implemented based on Netflix Ribbon. Through the encapsulation of Spring Cloud, we can easily automatically convert service-oriented REST template requests into client-side load balancing service calls.
Ribbon principle
1. The service consumer initiates a request
2. LoadBalancerInterceptor intercepts the request
3. RibbonLoadBanlancerClient obtains the id of the service provider according to the uri in the request
4. Pulls the service list from the eureka server through the DynamicServerListLoadBalancer according to the id 5. Passes
Ribbon load balancing rules choose a service
Ribbon load balancing strategy
Ribbon's load balancing rules are defined through the IRule interface, and each implementation class is a rule
Built-in load balancing rule class | describe |
---|---|
RoundRobinRule | Simply poll the service list to select a server. It is Ribbon's default load balancing rule. |
AvailabilityFilteringRule | Ignore the following two servers |
WeightedResponseTimeRule | Assign a weight value to each server. The longer the server response time, the lower the weight of this server. This weight value will affect the server selection. |
ZoneAvoidanceRule | Server selection is based on the servers available in the region. Use Zone to classify servers, and then poll multiple services within the Zone. |
BestAvailableRule | Ignore those short-circuiting servers and choose servers with lower concurrency counts. |
RandomRule | An available server is randomly selected. |
RetryRule | Selection logic for retry mechanism |
Modify Ribbon Load Balancing Policy
the first way
Define the implementation class of IRule in the configuration class
@Bean
public IRule randomRule(){
return new RandomRule();
}
the second way
Configure in the configuration file application.yml file
userservice:
ribbon:
#负载均衡规则
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
starvation loading
Ribbon uses lazy loading by default, that is, the LoadBalanceClient will be created when it is accessed for the first time, and the request time will be very long. The starvation load will be created when the project starts, reducing the time-consuming of the first access
userservice:
ribbon:
eager-load:
#开启饥饿加载
enabled: true
#指定对多个服务饥饿加载
clients:
- userservice
- yyyyservice
- xxxxservice