Spring Cloud load balancing and service invocation (Ribbon)

Table of Contents

Ribbon

Introduction

load balancing

Introduction

Load balancing method

Server-side load balancing

working principle

Features

Client load balancing

working principle

Features

Compared

accomplish

Load balancing strategy

Switch load balancing strategy

Customized load balancing strategy

Timeouts and retries

Single service configuration

Global configuration

service call

Example


Ribbon

Introduction

Ribbon is an open source component released by Netflix. Its main function is to provide client-side load balancing algorithms and service calls; through it, we can convert service-oriented REST template requests into client-side load balancing service calls. Calls between microservices, API gateway request forwarding, etc. are actually implemented through Ribbon.

Load Balancing

Introduction

In any system, load balancing is a very important content that must be implemented. It is one of the important means for the system to handle high concurrency, relieve network pressure and expand server capacity.

Load balancing, simply put, is to distribute user requests equally to multiple servers to achieve the purpose of expanding server bandwidth, enhancing data processing capabilities, increasing throughput, and improving network availability and flexibility.

Load balancing method

There are two common load balancing methods: server load balancing and client load balancing.

Server-side load balancing
Working Principle

Server-side load balancing is to establish an independent load balancing server between the client and the server. The server can be either a hardware device (such as F5) or software (such as Nginx). This load balancing server maintains a list of available servers, and then deletes failed server nodes through the heartbeat mechanism to ensure that all service nodes in the list can be accessed normally.

When the client sends a request, the request will not be sent directly to the server for processing, but will all be handed over to the load balancing server. The load balancing server will use the available information maintained by it according to a certain algorithm (such as polling, random, etc.) Select a server in the service list and forward it.

Features

1. Need to establish an independent load balancing server

2. Load balancing is performed after the client sends a request, so the client does not know which server provides the service.

3. The list of available servers is stored on the load balancing server

Client Load Balancing

Compared with server-side load balancing, client-side service balancing is a relatively niche concept.

How it works

Client load balancing encapsulates the load balancing logic on the client in the form of code, that is, the load balancer is located on the client. The client obtains a list of available services provided by the server through the service registration center (such as Eureka Server). After having the service list, the load balancer will select a server instance through the load balancing algorithm before accessing it before the client sends the request to achieve the purpose of load balancing;

Client load balancing also requires a heartbeat mechanism to maintain the validity of the server list. This process needs to be completed in conjunction with the service registration center.

Features

1. The load balancer is located on the client side, and there is no need to build a separate load balancing server.

2. Load balancing is performed before the client sends a request, so the client clearly knows which server provides the service.

3. The client maintains a list of available services, and this list is obtained from the service registration center

Ribbon is a client load balancer based on HTTP and TCP. When we use Ribbon and Eureka together, Ribbon will obtain the server list from the Eureka Server (service registration center), and then forward the requests through the load balancing policy. Allocate it to multiple service providers to achieve load balancing

Compare

Implementation

Ribbon is a client-side load balancer that can be used with Eureka to easily achieve client-side load balancing. Ribbon will first obtain the server list from Eureka Server (service registration center), and then allocate the request to multiple servers through the load balancing strategy to achieve load balancing.

Load Balancing Policy
Serial number Implementation class Load balancing strategy
1 RoundRobinRule Polling strategy, that is, selecting service instances in a certain order
2 RandomRule Randomly select a service instance
3 RetryRule Acquire services according to the polling strategy. If the service instance obtained is null or has expired, it will be retried continuously within the specified time. If the service instance is still not obtained after the specified time, it will return null
4 WeightedResponseTimeRule Calculate the weight of all service instances based on the average response time. The shorter the response time, the higher the weight of the service instance, and the greater the probability of being selected
5 BestAvailableRule First filter the service instances with point failures or failures, and then select the service instances with the smallest concurrency
6 AvailabilityFilteringRule First filter out faulty or failed service instances, and then select service instances with smaller concurrency
7 ZoneAvoidanceRule Default load balancing strategy, comprehensively judging the performance of the service area and the availability of the service to select the service instance
Switch load balancing strategy

Ribbon uses polling strategy to select service instances by default. We can also switch the load balancing strategy according to our own needs. We only need to inject other implementation classes of IRule into the container in the configuration class of the service consumer (client).

Add the following code in the configuration class ConfigBean to switch the load balancing policy to RandomRule (random)

@Bean
public IRule myRule() {
    // RandomRule is the random strategy
    return new RandomRule();
}
Customized load balancing strategy

1. Create a class called MyRandomRule

package net.biancheng.myrule;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;

/**
* Customized Ribbon load balancing strategy
*/
public class MyRandomRule extends AbstractLoadBalancerRule {
    private int total = 0; // The total number of times it has been called. Currently, each unit is required to be called 5 times.
    private int currentIndex = 0; // The machine number currently providing services

    public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            return null;
        }
        Server server = null;

        while (server == null) {
            if (Thread.interrupted()) {
                return null;
            }
            //Get a list of all valid service instances
            List<Server> upList = lb.getReachableServers();
            //Get the list of all service instances
            List<Server> allList = lb.getAllServers();

            //If there is no service instance, return null
            int serverCount = allList.size();
            if (serverCount == 0) {
                return null;
            }
            //Similar to the random strategy, but each service instance will only call other service instances after calling it 3 times.
            if (total < 3) {
                server = upList.get(currentIndex);
                total + + ;
            } else {
                total = 0;
                currentIndex + + ;
                if (currentIndex >= upList.size()) {
                    currentIndex = 0;
                }
            }
            if (server == null) {
                Thread.yield();
                continue;
            }
            if (server.isAlive()) {
                return (server);
            }
            server = null;
            Thread.yield();
        }
        return server;
    }

    @Override
    public Server choose(Object key) {
        return choose(getLoadBalancer(), key);
    }

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {
        // TODO Auto-generated method stub
    }
}

2. Create a configuration class named MySelfRibbonRuleConfig and inject our customized load balancing strategy implementation class into the container

/**
* Customize the configuration class of Ribbon load balancing strategy
* This custom Ribbon load balancing policy configuration class cannot be under the net.biancheng.c package and its sub-packages
* Otherwise, all Ribbon clients will adopt this strategy and cannot achieve the purpose of special customization.
*/
@Configuration
public class MySelfRibbonRuleConfig {
    @Bean
    public IRule myRule() {
        //Customize Ribbon load balancing strategy
        return new MyRandomRule(); //Customize, randomly select a microservice and execute it five times
    }
}

3. Modify the startup class and use the @RibbonClient annotation on this class to make our customized load balancing strategy take effect.

@SpringBootApplication
@EnableEurekaClient
//Customized Ribbon load balancing strategy uses RibbonClient annotation on the main startup class. When the microservice starts, our customized Ribbon configuration class can be automatically loaded, so that the configuration takes effect.
// name is the name of the microservice that needs to be customized with a load balancing strategy (application name)
// configuration is the configuration class of the customized load balancing strategy,
// And the official document clearly states that this configuration class cannot be in the package or its sub-package under the ComponentScan annotation (the SpringBootApplication annotation contains this annotation), that is, the custom load balancing configuration class cannot be in the net.biancheng.c package and His son takes care of it
@RibbonClient(name = "MICROSERVICECLOUDPROVIDERDEPT", configuration = MySelfRibbonRuleConfig.class)
public class MicroServiceCloudConsumerDept80Application {
    public static void main(String[] args) {
        SpringApplication.run(MicroServiceCloudConsumerDept80Application.class, args);
    }
}

Timeout and Retry

Using HTTP to initiate a request will inevitably cause problems. Starting from version F, Ribbon’s retry mechanism is enabled by default. You need to add the configuration of the timeout and retry strategy;

Single service configuration
RIBBON-SERVICE-B:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
    ConnectTimeout: 3000
    ReadTimeout: 60000
    MaxAutoRetries: 3 #Number of retries for the first requested service
    MaxAutoRetriesNextServer: 1 #Maximum number of next services to retry (excluding the first service)
    OkToRetryOnAllOperations: true
Global Configuration
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
  MaxAutoRetries: 3 #Number of retries for the first requested service
  MaxAutoRetriesNextServer: 1 #Maximum number of next services to retry (excluding the first service)
  OkToRetryOnAllOperations: true

Service call

Ribbon can be used with RestTemplate (Rest template) to implement calls between microservices

RestTemplate is a request framework in the Spring family for consuming third-party REST services. RestTemplate implements the encapsulation of HTTP requests and provides a set of templated service calling methods. Through it, Spring applications can easily access various types of HTTP requests.

Example

1.Introduce dependencies

 <!--Spring Cloud Ribbon dependency-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

2. Create a request and call the service provided by the server

@RestController
public class DeptController_Consumer {
    //private static final String REST_URL_PROVIDER_PREFIX = "http://localhost:8001/"; This method is to call the server directly and does not use Spring Cloud at all.

    //Programming for microservices, that is, obtaining the calling address through the name of the microservice
    private static final String REST_URL_PROVIDER_PREFIX = "http://MICROSERVICECLOUDPROVIDERDEPT"; // Use the service registered in the Spring Cloud Eureka service registration center, that is, application.name

    @Autowired
    private RestTemplate restTemplate; //RestTemplate is a simple and convenient access to restful service template class. It is a client template tool set provided by Spring for accessing Rest services. It provides a variety of convenient methods to access remote HTTP services.

    //Get specified department information
    @RequestMapping(value = "/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(REST_URL_PROVIDER_PREFIX + "/dept/get/" + id, Dept.class);
    }
    //Get the department list
    @RequestMapping(value = "/consumer/dept/list")
    public List<Dept> list() {
        return restTemplate.getForObject(REST_URL_PROVIDER_PREFIX + "/dept/list", List.class);
    }
}

3.Access

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138,179 people are learning the system