Load Balancing Strategies and Configuration Details

Load balancing strategies and their usage methods supported by Dubbo

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:

AlgorithmFeaturesRemarksConfiguration Value
Weighted Random LoadBalanceWeighted RandomDefault algorithm, same default weightrandom (default)
RoundRobin LoadBalanceWeighted Round RobinBased on Nginx’s smooth weighted round-robin algorithm, same default weightroundrobin
LeastActive LoadBalanceLeast Active + Weighted RandomBased on the “to each according to their ability” conceptleastactive
Shortest-Response LoadBalanceShortest Response + Weighted RandomMore focused on response speedshortestresponse
ConsistentHash LoadBalanceConsistent HashDeterministic input, deterministic provider, suitable for stateful requestsconsistenthash
P2C LoadBalancePower of Two ChoiceRandomly selects two nodes and then chooses the one with fewer connections.p2c
Adaptive LoadBalanceAdaptive Load BalancingBased on P2C algorithm, chooses the node with the least load among the twoadaptive

Global Configuration

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:

  1. dubbo:
  2. consumer:
  3. loadbalance: roundrobin

Interface Level Configuration

Different load balancing strategies can be specified for each service.

Set on the provider side as the default value for the consumer:

  1. @DubboService(loadbalance = "roundrobin")
  2. public class DemoServiceImpl implements DemoService {}

Set on the consumer side, which has higher priority:

  1. @DubboReference(loadbalance = "roundrobin")
  2. private DemoService demoService;

Method Level Configuration

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

  1. @Configuration
  2. public class DubboConfiguration {
  3. @Bean
  4. public ServiceBean demoService() {
  5. MethodConfig method = new MethodConfig();
  6. method.setName("sayHello");
  7. method.setLoadbalance("roundrobin");
  8. ServiceBean service = new ServiceBean();
  9. service.setInterface(DemoService.class);
  10. service.setRef(new DemoServiceImpl());
  11. service.addMethod(method);
  12. return service;
  13. }
  14. }
  1. @Autowired
  2. private DemoService demoService;
  3. @Configuration
  4. public class DubboConfiguration {
  5. @Bean
  6. public ReferenceBean demoService() {
  7. MethodConfig method = new MethodConfig();
  8. method.setName("sayHello");
  9. method.setLoadbalance("roundrobin");
  10. ReferenceBean<DemoService> reference = new ReferenceBean<>();
  11. reference.setInterface(DemoService.class);
  12. reference.addMethod(method);
  13. return reference;
  14. }
  15. }

dubbo.properties

  1. dubbo.reference.org.apache.dubbo.samples.api.DemoService.sayHello.loadbalance=roundrobin

Consistent Hash Configuration

By default, the first parameter is used as the hash key. If you need to switch parameters, you can specify the hash.arguments property.

  1. ReferenceConfig<DemoService> referenceConfig = new ReferenceConfig<DemoService>();
  2. // ... init
  3. Map<String, String> parameters = new HashMap<String, String>();
  4. parameters.put("hash.arguments", "1");
  5. parameters.put("sayHello.hash.arguments", "0,1");
  6. referenceConfig.setParameters(parameters);
  7. referenceConfig.setLoadBalance("consistenthash");
  8. referenceConfig.get();

Adaptive Load Balancing Configuration

Simply set loadbalance to p2c or adaptive on the consumer or provider side. You can refer to Working Principle.

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)