Spring Cloud - switch Ribbon's load balancing mode

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 convert the service-oriented REST template request to the client-side load balanced service call automatically. Although Spring Cloud Ribbon is just a tool framework, it does not need to be deployed independently like service registry, configuration center and API gateway, but it exists in almost every microservice and infrastructure built by Spring Cloud. Because the call between microservices and the request forwarding of API gateway are actually implemented through the Ribbon, including Feign, which we will introduce later. It is also a Ribbon based implementation tool. Therefore, the understanding and use of Spring Cloud Ribbon is very important for us to use Spring Cloud to build microservices.

Ribbon provides us with many load balancing algorithms, such as loop, random, etc. we can also define the algorithm ourselves. Which mode does ribbon use by default? Let's test it. Here I start three service providers, one service consumer, as shown in the following figure

Test class

@SpringBootTest
@RunWith(SpringRunner.class)
public class LoadBalanceTest {

    @Autowired
    private RibbonLoadBalancerClient client;

    @Test
    public void test(){
        for (int i = 0; i < 50; i++) {
            ServiceInstance instance = client.choose("service-provider");
            System.out.println(instance.getHost()+":"+instance.getPort());
        }
    }
}

Output result

LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890

So how to switch its load balancing mode

Just configure the following section in the configuration file (take yml for example):

{Service provider name}:
  ribbon:
    NFLoadBalancerRuleClassName: {IRule Implementation class}

For example, my configuration is as follows:

service-provider:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

The optional patterns are as follows. These are the implementation classes of IRule

What is the effect of switching to random mode? Run the test class again after modifying the configuration

LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8890
LAPTOP-LFAOC6JB:8888
LAPTOP-LFAOC6JB:8889
LAPTOP-LFAOC6JB:8889


As you can see, the results of the operation become random

Tags: Programming Spring REST

Posted on Mon, 02 Dec 2019 02:21:26 -0800 by daniel a