Spring cloud - load balancing Ribbon(Greenwich version)

Catalogue

1. Load balancing

2. Introduction to Ribbon

3. IRULE 7 default algorithm

4. Project practice

4.1 preparations

4.2. Create a Ribbon service

1. Load balancing

Load balancing simply means that users' requests are evenly distributed to multiple services, so as to achieve high availability (HA) of the system. Common load balancing include software Nginx, LVS, hardware F5, etc. Accordingly, load balancing is provided to us in middleware, such as dubbo and spring cloud. The load balancing algorithm of spring cloud can be customized. Two common load balancing methods are as follows.

  • Centralized LB

That is, an independent LB facility (which can be hardware, such as F5, or software, such as nginx) is used between the service consumer and the service provider. The facility is responsible for forwarding the access request to the service provider through some policy.

  • In process LB

Integrate LB logic into the consumer. The consumer knows which addresses are available from the service registry, and then selects a suitable server from these addresses. Ribbon belongs to in-process lb, which is just a class library integrated into the consumer process, through which the consumer obtains the address of the service provider.

What is the difference between Ribbon local load balancing and Nginx server-side load balancing?

1, Ribbon local load balancing. When calling the micro service interface, it will obtain the information service list in the registry and cache it locally to the jvm, so as to realize the RPC remote service call technology locally.  

2, Nginx implements LB on the server side. All requests from the client will be handed over to nginx, and then nginx forwards the requests. That is, LB is implemented by the server.

2. Introduction to 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 for clients and connect Netflix's middle tier services together. Ribbon client component provides a series of perfect configuration items, such as connection timeout, Retry, etc. 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.). We can also easily use ribbon to implement custom load balancing algorithms.

Ribbon architecture (1)

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.

Summary: Ribbon is actually a client component of soft load balancing. It can be used in combination with other clients requiring requests. The combination with eureka is just one example.

3. IRULE 7 default algorithm

IRule: select a service to be accessed from the server list according to the specific algorithm. The default algorithm of Ribbon is polling algorithm;

Seven load balancing algorithms in Ribbon:

  1. Roundrobin rule: polling;
  2. RandomRule: random;
  3. Availability filtering rule: it will first filter out the services in the state of circuit breaker due to multiple access failures and the services with concurrent connections exceeding the threshold, and then access the remaining service list according to the polling policy;
  4. WeightedResponseTimeRule: calculate the weight of all services according to the average response time. The faster the response time, the greater the weight of the service and the greater the probability of being selected. If the statistical information is insufficient at the beginning of startup, use the round robin rule strategy. When the statistical information is sufficient, it will switch to WeightedResponseTimeRule;
  5. RetryRule: first obtain the service according to the round robin rule (polling) policy. If the service acquisition fails, retry within the specified time to obtain the available service;
  6. BestAvailableRule: first filter out the services in the circuit breaker tripping state due to multiple access faults, and then select a service with the least concurrency;
  7. ZoneAvoidanceRule: judge the performance of the region where the Server is located and the availability of the Server, and select the Server;

4. Project practice

4.1 preparations

Continue to use the spring cloud project in the previous section and start Eureka server with port 8761; Start Eureka client twice, with ports 8001 and 8002 respectively. SpringBoot setting local project multi port boot

4.2. Create a Ribbon service

1. Spring cloud project in maven main project( Springwiure1 - registered version of the service )Next, create a new module: Service Ribbon, POM file import dependency.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringCloud-Project</artifactId>
        <groupId>com.boyue</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>service-feign</artifactId>
    <!--Feign Default integration Ribbon,And Eureka Combined with, the effect of load balancing is realized by default-->
    <dependencies>
        <!--Web Start dependence spring-boot-starter-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Eureka Client start dependency spring-cloud-starter-netflix-eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <!--Feign Start dependence spring-cloud-starter-openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
</project>

Code prompt:

Spring cloud is updated quickly;

Spring cloud starter Eureka server is dependent on versions before 1.5;

Spring cloud starter Netflix Eureka server is a dependency of the latest version (recommended).

 

2. Add notes on the startup class: @ SpringBootApplication,@EnableDiscoveryClient, use @ LoadBalanced and configure the configuration information class of load balancing.

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbon {
    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbon.class,args);
    }
    /**
     * Inject a bean into the ioc of the program: resttemplate;
     * The @ LoadBalanced annotation indicates that this restreprimplate enables the load balancing function
     * Based on Ribbon
     * This method requires @ Configuration annotation, which has been included in @ SpringBootApplication
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }
}

Code prompt:

@Difference between EnableDiscoveryClient and @ EnableEurekaClient

Under a simple distinction, they are consistent in function: write it on the startup class and turn on the service registration and discovery function.

The difference is that when the registration centers are different, such as eureka, consul t and zookeeper, they are also used differently.

The EnableDiscoveryClient annotation in the common package determines which implementation to use through the classpath of the project, while the EnableEurekaClient annotation in the netflix package only uses eureka;

Therefore, using enablediscovery client is applicable to any registry. EnableEurekaClient serves eureka.

 

3. Configuration file application Yaml, where the service needs to be registered with Eureka Registration Center:

server:
  port: 8001

  #We can specify the name of the microservice. In subsequent calls,
  #  You only need to use this name to access the service
spring:
  application:
      name: SERVICE-RIBBON
      #When eureka is a cluster, you only need commas to separate it
eureka:
  instance:
    hostname: localhost
    # Modify the default description of Eureka
    # Let's point the instance url to the host ip + port number
    instance-id: http://${eureka.instance.hostname}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:7001/eureka/,http://localhost:7002/eureka/

4. Write a test class UserService and consume the "/ info" interface of Eureka client service through the restTemplate previously injected into the ioc container. Here, we directly replace the specific url address with the program name. In the ribbon, it will select the specific service instance according to the service name, and replace the service name with the specific url when requesting according to the service instance, The code is as follows:

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public RespBean restGetInfo() {
//        Consume the "/ info" interface of Eureka client service through the restTemplate previously injected into the ioc container,
//        Here, we directly use the program name instead of the specific url address,
//        In the ribbon, it will select the specific service instance according to the service name,
//        According to the service instance, the service name will be replaced with a specific url when requesting
        return restTemplate.getForObject("http://EUREKA-CLIENT/eurekaClient/user/eurekaClientInfo",RespBean.class);
//        return restTemplate.postForObject("http://EUREKA-CLIENT/eurekaClient/user/eurekaClientInfo","POST",RespBean.class);
    }
}

5. Write a controller and call the restGetInfo() method of HelloService in the controller. The code is as follows:

@RestController
@RequestMapping("/rest")
public class RestServiceController {
    @Autowired
    private UserService userService;
    @GetMapping("/restGetInfo")
    public RespBean restGetInfo(){
        return userService.restGetInfo();
    }
}

6. eurekaClientInfo service class in EUREKA-CLIENT service:

@RestController
@RequestMapping("/user")
public class UserController {
​
    @Autowired
    private DiscoveryClient client;

    @Value("${server.port}")
    private String port;
    @Value("${spring.application.name}")
    private String applicationName;
    @GetMapping("/eurekaClientInfo")
    public RespBean eurekaClientInfo(){
       String info="Name of the called service:"+applicationName+",Service port:"+port;
        return RespBean.ok("The service information is as follows:",info);
    }
}

7. In idea, a service (EUREKA-CLIENT) can be started simultaneously according to multiple ports. springboot sets the multi port boot of the local project. Access multiple times on the browser http://localhost:8001/rest/restGetInfo , the browser displays alternately:

This shows that when we call resttemplate getForObject(" http://EUREKA-CLIENT/eurekaClient/user/eurekaClientInfo ", respbean. Class) method, load balancing has been done and service instances of different ports have been accessed.

Scan code concerns WeChat official account:

 

Keywords: Java Maven Spring Boot Spring Cloud ribbon

Added by bettyatolive on Fri, 04 Mar 2022 06:42:59 +0200