Updated on 2024-11-26 GMT+08:00

Load Balancing

Load balancing functions on the client and is an indispensable key component of a high-concurrency and high-availability system. It aims to evenly distribute network traffic to multiple servers to improve the overall response speed and availability of the system.

  • Load balancing of Java chassis is used for microservice consumers. The load balancing module must be integrated into microservice applications and the loadbalance processing chain must be enabled.
    Configuration example:
    servicecomb:
      handler:
        chain:
          Consumer:
            default: loadbalance
      loadbalance:   
        strategy:     
          name: RoundRobin #The polling mode is enabled.

    Add the following dependency to the POM file:

     <dependency>
       <groupId>org.apache.servicecomb</groupId>
       <artifactId>handler-loadbalance</artifactId>
       <version>${project.version}</version>
    </dependency>
  • Spring Cloud Huawei load balancing is based on Ribbon in Spring Cloud and works on the client. The principles are as follows: When a random rule is used, the client randomly accesses an instance in the downstream microservice instance. When a polling rule is used, the client cyclically selects a server in the downstream microservice instance in sequence.

    Configuration example:

    servicecomb:
      loadbalance:
        userLoginAction: | 
          rule: Random #The random mode is enabled. The default mode is RoundRobin.

Example

The Spring Cloud Huawei load balancing demo is used as an example to test the load effect.

This demo contains four microservices: consumer, provider-one, provider-two, and provider-three. Start the four microservices and access http://${consumer-ip}:8090/load-balance?id=111.

  • When rule is set to RoundRobin and the default framework mode is used, the results are returned in regular sequence based on the version number, as shown in the following figure.
      provider id-----> 11111 port ----->8091 ,version----->0.0.1
      provider id-----> 11111 port ----->8092 ,version----->0.0.2
      provider id-----> 11111 port ----->8093 ,version----->0.0.3
      provider id-----> 11111 port ----->8091 ,version----->0.0.1
      provider id-----> 11111 port ----->8092 ,version----->0.0.2
      provider id-----> 11111 port ----->8093 ,version----->0.0.3
  • When rule is set to Random, the results are returned irregularly, as shown in the following figure.
      provider id-----> 11111 port ----->8091 ,version----->0.0.1
      provider id-----> 11111 port ----->8092 ,version----->0.0.2
      provider id-----> 11111 port ----->8091 ,version----->0.0.1
      provider id-----> 11111 port ----->8093 ,version----->0.0.3
      provider id-----> 11111 port ----->8093 ,version----->0.0.3
      provider id-----> 11111 port ----->8092 ,version----->0.0.2