Load balancing

更新时间:
复制 MD 格式

This topic describes the load balancing algorithms that SOFARPC supports and how to configure them.

Load balancing algorithms

SOFARPC supports the following five algorithms:

Type

Name

Description

random

Random algorithm

The default load balancing algorithm.

localPref

Local preference algorithm

Prioritizes local services. If no local service is available, it uses the random algorithm.

roundRobin

Round-robin algorithm

Performs round-robin at the method level. The polling for each method is independent and does not affect others.

consistentHash

Consistent hash algorithm

Creates a one-to-one mapping between service requests and the servers that handle them.

weightRoundRobin

Weighted round-robin algorithm

Performs round-robin polling on nodes based on their weights. This algorithm has poor performance and is not recommended.

Set load balancing

To use a specific load balancing algorithm, configure it as follows:

  • XML method

    If you reference a service using XML, you can set the load balancing algorithm by configuring the loadBalancer attribute of the sofa:global-attrs tag. The following example shows how to set the algorithm to `roundRobin`:

    <sofa:reference interface="com.example.demo.SampleService" id="sampleService">
        <sofa:binding.bolt>
            <sofa:global-attrs loadBalancer="roundRobin"/>
        </sofa:binding.bolt>
    </sofa:reference>
  • API method in a Spring environment

    If you use the API in a Spring or Spring Boot environment, you can set the load balancing algorithm by calling the setLoadBalancer method of BoltBindingParam. The following example shows how to set the algorithm to `roundRobin`:

    BoltBindingParam boltBindingParam =new BoltBindingParam();
    boltBindingParam.setLoadBalancer("roundRobin");
  • API method in a non-Spring environment

    If you use the bare SOFARPC API in a non-Spring environment, you can set the load balancing algorithm by calling the setLoadBalancer method of ConsumerConfig. The following example shows how to set the algorithm to `random`:

    ConsumerConfig consumerConfig =new ConsumerConfig();
    consumerConfig.setLoadbalancer("random");