Ribbon load balancing
- load balancing strategy
- lazy loading
Ribbon-load balancing strategy
Ribbon's load balancing rules are defined by an interface called IRule, and each sub-interface is a rule:
The floor | ||||
---|---|---|---|---|
AbstractLoadBalancerRule | ||||
RetryRule | ClientConfigEnabledRoundRobinRule | RoundRobinRule | RandomRule | |
BestAvailableRule | PredicateBaseRule | WeightedResponseTimeRule | ||
AvailabilityFilteringRule | ZoneAvoidanceRule |
Built-in load balancing rule class | Rule description |
---|---|
RoundRobinRule | Simply poll the list of services to select a server. It is the default load balancing rule of Ribbon. |
AvailabilityFilteringRule | Ignore the following two servers: (1) By default, if this server fails to connect 3 times, this server will be set to "short circuit" status. The short-circuit state will last for 30 seconds, and if the connection fails again, the duration of the short-circuit will increase collectively. (2) Servers with too high concurrency. If the number of concurrent connections to a server is too high, clients configured with the AvailabilityFilteringRule rule will also ignore it. The upper limit of the number of concurrent connections can be configured with the client's , , and ActiveConnectionsLimit properties. |
WeightedResponseTimeRule | Assign a weight value to each server. The longer the server response time, the less weight this server has. This rule will randomly select a server, and 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. This Zone can be understood as a computer room, a rack, etc. Then poll multiple services in the Zone. |
BestAvailableRule | Ignore which short-circuited servers and choose servers with lower concurrency |
RandomRule | Randomly choose an available server |
RetryRule | Selection logic for the retry mechanism |
There are two ways to modify the load military rules by defining IRule:
1. Code method: In the OrderApplication class in order-service, define a new IRule:
This is global. After configuration, it is the same whether order-service calls user-service or other services in the future.
@Bean
public IRule randomRule() {
return new RandomRule();
}
2. Configuration file method: In the applicaiton.yml file of order-service, adding new configurations can also modify the rules:
This is only for a certain microservice, only for calling userservice.
userservice:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # 负载均衡规则
Ribbon hunger loading
Ribbon uses lazy loading by default, that is, the LoadBalanceClient is created only when it is accessed for the first time, and the request time will be very long. And hunger loading will be recorded when the project starts, reducing the time-consuming for the first visit, and enable borrow loading through the following configuration:
ribbon:
eager-load:
enabled: true #开启饥饿加载
clients: userservice # 指定对userservice这个服务饥饿加载
Summarize:
1. Ribbon load balancing rules
- The rule interface is IRule
- The default implementation is ZoneAvoidanceRule, select the service list according to the zone, and then poll
2. Custom load balancing method
- Code method: flexible configuration, but needs to be repackaged and released when modified
- Configuration method: Intuitive and convenient, no need to repackage and publish, but global configuration is not possible
3. Hungry loading
- Enable starvation loading
- Specify the microservice name for starvation loading