Dubbo has a built-in client-based load balancing mechanism. Below are the currently supported load balancing algorithms. With the automatic service discovery mechanism mentioned earlier, the consumer will automatically use the Weighted Random LoadBalance
strategy for service calls.
If you want to adjust the load balancing algorithm, the following are built-in load balancing strategies in the Dubbo framework:
Algorithm | Features | Remarks | Configuration Value |
---|---|---|---|
Weighted Random LoadBalance | Weighted Random | Default algorithm, same default weight | random (default) |
RoundRobin LoadBalance | Weighted Round Robin | Based on Nginx’s smooth weighted round-robin algorithm, same default weight | roundrobin |
LeastActive LoadBalance | Least Active + Weighted Random | Based on the “to each according to their ability” concept | leastactive |
Shortest-Response LoadBalance | Shortest Response + Weighted Random | More focused on response speed | shortestresponse |
ConsistentHash LoadBalance | Consistent Hash | Deterministic input, deterministic provider, suitable for stateful requests | consistenthash |
P2C LoadBalance | Power of Two Choice | Randomly selects two nodes and then chooses the one with fewer connections. | p2c |
Adaptive LoadBalance | Adaptive Load Balancing | Based on P2C algorithm, chooses the node with the least load among the two | adaptive |
The default strategy of the Dubbo framework is random
weighted random load balancing. To adjust the strategy, simply set the corresponding loadbalance
value. For each load balancing strategy value, refer to the table at the top of this document.
Specify global configuration for all service calls:
Different load balancing strategies can be specified for each service.
Set on the provider side as the default value for the consumer:
Set on the consumer side, which has higher priority:
Load balancing strategies can also be specified at the method level.
In Spring Boot development mode, there are the following ways to configure method-level parameters:
JavaConfig
dubbo.properties
By default, the first parameter is used as the hash key. If you need to switch parameters, you can specify the hash.arguments
property.
Simply set loadbalance
to p2c
or adaptive
on the consumer or provider side. You can refer to Working Principle.