Detailed explanation of spring cloud load balancing

Detailed explanation of spring cloud load balancing

1. Introduction

1.1 what is load balancing (in-process LB)

Load balancing, that is, load balancing, is a computer technology used to distribute load among multiple computers (computer clusters), network connections, CPU s, disk drives or other resources, so as to optimize resource use, maximize throughput, minimize response time and avoid overload.

1.2 why do I need load balancing

In distributed microservice architecture, multiple service providers often register in multiple registries. When consumers need to use services, deciding which service provider where the registry is located to provide services is load balancing. (that is, in-process LB, note: the scope of this article is limited to in-process LB)

2. At present, the load is very low

Most of the load balancing components used in the spring cloud H version are ribbon and OpenFegin. After the spring cloud update, it was officially announced that after the spring cloud 2020 version, spring cloud eliminated all Netflix components except Eureka server and Eureka client. Spring also gave an alternative scheme to use spring cloud Loadbalancer, but at present, Ribbon and OpenFegin are still the mainstream load balancing solutions. Therefore, this article will introduce the two components of the loader, which may be followed by the introduction of the new Loadbalancer.

3.Ribbon

3.1 what is ribbon

Spring Cloud Ribbon is a set of client-side load balancing tools based on Netflix Ribbon.

In short, Ribbon is an open source project released by Netflix. Its main function is to provide software load balancing algorithms and service calls on the client. The Ribbon client component provides a series of complete configuration items, such as connection timeout, Retry, etc. To put it simply, list all the machines behind the Load Balancer (LB) in the configuration file. The Ribbon will automatically help you connect these machines based on certain rules (such as simple polling, random connection, etc.). It is easy for us to use the Ribbon to implement a custom load balancing algorithm.

Summary: load balancing + RestTemplate call

3.2 load balancing demonstration

3.2. 1. Working principle analysis

Ribbon works in two steps
The first step is to select Eureka server, which gives priority to servers with less load in the same region
The second step is to select an address from the service registration list obtained from the server according to the policy specified by the user.
Among them, Ribbon provides a variety of strategies: such as polling, random and weighting according to response time.

3.2. 2 use

1. Introduce dependency and Eureka coordinates (spring cloud starter Netflix Eureka client comes with spring cloud starter ribbon reference)

      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2.application.yml

eureka:
  client:
    #Indicates whether to register yourself with Eurekaserver. The default value is true.
    register-with-eureka: true
    #Whether to retrieve the existing registration information from EurekaServer. The default value is true. Single node doesn't matter. The cluster must be set to true to use load balancing with ribbon
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
  instance:
    instance-id: 0rder80

3. Configure RestTemplate

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();


    }
}

4. Service call, call the service registered in Eureka registry

public static final String PAYMENT_URL = "http://CLOUD-PRIVDER-SERVICE";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){

        return restTemplate.postForObject(PAYMENT_URL+"/payment/create", payment, CommonResult.class);
    }

5. Open Eureka client discovery service

@SpringBootApplication
@EnableEurekaClient

public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }
}

test

3.3 core component IRule

Select a service to access from the service list according to a specific algorithm

Different load balancing algorithms can be implemented by injecting different implementation classes of this interface into the container

3.3. 1 how to replace?

1. Note: the official clearly states that this configuration class cannot be placed under the package scanned by @ ComponentScan. Since the spring boot boot class annotation contains this annotation, the configuration class cannot be defined under the package where the spring boot class is located

2. Configuration class definition

@Configuration
public class MySelfRule {
    @Bean
    public IRule returnRule(){
        return new RandomRule();
    }
}

3. Start the configuration class

@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "CLOUD-PRIVDER-SERVICE",configuration = com.ljy.myrule.MySelfRule.class)
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }
}

3.3. 2. Local load balancer

4.OpenFeign

4.1 what is openfeign

Feign is a declarative Web Service client. Using feign makes it easier to write Web Service clients.
It is used by defining a service interface and then adding annotations to it. Feign also supports pluggable encoders and decoders. Spring Cloud encapsulates feign to support Spring MVC standard annotations and HttpMessageConverters. Feign can be used in combination with Eureka and Ribbon to support load balancing

4.2 what can feign do

Feign aims to make it easier to write Java Http clients.
When Ribbon+RestTemplate is used earlier, a set of template calling methods is formed by encapsulating http requests with RestTemplate. However, in the actual development, because there may be more than one invocation of service dependencies, and often one interface will be invoked in multiple places, some client classes are usually encapsulated for each micro service to wrap the invocation of these dependent services. Therefore, Feign made further encapsulation on this basis to help us define and implement the definition of dependent service interfaces. Under the implementation of Feign, we only need to create an interface and configure it by annotation (previously, the Dao interface was marked with Mapper annotation, but now it is a micro service interface marked with Feign annotation), so as to complete the interface binding to the service provider, which simplifies the development of automatically encapsulating the service call client when using the Spring cloud Ribbon.

Feign integrates Ribbon
The Ribbon is used to maintain the service list information of Payment, and the load balancing of the client is realized through polling. Unlike Ribbon, feign only needs to define the service binding interface and implements the service invocation gracefully and simply in a declarative way

4.3 difference from feign

FeginOpenFegin
Feign is a lightweight RESTful HTTP service client in the Spring Cloud component
Feign has a built-in Ribbon, which is used for client load balancing to call the services of the service registry. Feign is used by using feign's annotation to define an interface. By calling this interface, you can call the service of the service registry
OpenFeign is a Spring Cloud that supports spring MVC annotations based on Feign, such as @ RequesMapping. OpenFeign's @ FeignClient can parse the interface under the @ RequestMapping annotation of spring MVC, generate implementation classes through dynamic proxy, implement load balancing in the classes and call other services.

org.springframework.cloud
spring-cloud-starter-feign

org.springframework.cloud
spring-cloud-starter-openfeign

4.4 use of openfeign

OpenFeign is very simple to use. After importing the relevant dependencies, you can directly define the interface

pom

   <dependencies>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- Introduce self defined api General package, you can use Payment payment Entity -->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--General foundation general configuration-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

application.yml

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
 

service interface

@Service
@FeignClient(value = "CLOUD-PRIVDER-SERVICE")
public interface PayMentService {

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getByI(@PathVariable("id") Long id);


}

When the controller layer calls the method of this interface, it will call the address of the corresponding service provider,

be careful:

  1. OpenFeign comes with load balancing function

  2. OpenFeign will wait for one second by default for receiving and processing requests. After that, an error will be reported
    MentService {

    @GetMapping("/payment/get/{id}")
    public CommonResult getByI(@PathVariable("id") Long id);

}

stay controller When the layer calls the method of this interface, it will call the address of the corresponding service provider,

**be careful:**

1. **OpenFeign Self contained load balancing function**
2. **OpenFeign The default waiting time for receiving request processing is one second. After that, an error will be reported**

Keywords: Java Spring Spring Cloud ribbon

Added by dhiren22 on Thu, 16 Dec 2021 19:45:27 +0200