springcloud client load balancing component - RIBBON

RIBBON

introduce

ribbon is a basic load balancing client in the framework of spring cloud. It implements the micro service invocation function. The code needs to rely on the RestTemplate object to intercept the http requests sent by its api, calculate the load balancing, and finally access the specific instance provider to obtain the function response.

It is also a netflix component. Like eureka, the ribbon has stopped working. At present, there are no better replacement measures on the market, and it is still used in a large area. It has powerful functions. springcloud originally wanted to launch a loadbalancer to replace the ribbon, but the function is still not perfect. Even if eureka, the service governance component, is replaced by Alibaba nacos, it also supports the load balancing of the ribbon

structure


Any program (web application) with ribbon component can grab the registration information and call the cluster instance of a micro service for load balancing.

Build a microservice invocation function

Introduce ribbon components and code, and then load balance all microservice providers calling hi service

pom file

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

applicaotion.properties

server.port=8094
#Configure a service name of the current ribbon calling project, but should this project web application have a service name
#Whether or not to register in the registry depends on the requirements. Whether or not someone calls it. If there is a caller, it will be registered, and if not, it will not be registered
#Suppose the current ribbon system has an interface called by others
spring.application.name=ribbon-service
server.servlet.context-path=/ribbon
#eureka.client.fetch-registry=false
#eureka.client.register-with-eureka=true
eureka.client.service-url.zone=http://localhost:8761/eureka

Startup class

Take the of the startup class as a configuration class and generate an object RestTemplate used in the ribbon

@SpringBootApplication
public class StarterRibbonBalance {
    public static void main(String[] args) {
        SpringApplication.run(StarterRibbonBalance.class,args);
    }
    //ribbon needs to add annotation ID to a RestTemplate object
    //Consider the startup class as a Configuration class
    @Bean
    @LoadBalanced//Tag function. Every time this object sends an http request, it must be intercepted by loadBalanced
    //Only with this annotation can we use the service call in the code, otherwise it must be the domain name or ip of the Internet
    //With this annotation, instead of using baidu.com 10.9.9.9, this method can only be called through the service name major serivce
    //hi-serivce
    public RestTemplate init(){
        return new RestTemplate();
    }
    //Custom load balancing calculator default roundrobin rule
    @Bean
    public IRule ruleInit(){
        //Polling, weight (weight based on response time) random load balancing
        return new RandomRule();
        //WeightedResponseTimeRule
        //RoundRobinRule
    }
}

Test case

The ruby project that provides access to 8094 actually uses the service hi instance provider for load balancing.

Develop test code

  • helloService
@Service
public class HelloService {
    @Autowired
    private RestTemplate restTemplate;
    public String sayHi(String name) {
        /*
         Find a way to send an http request from the business code to the hi service micro service
         */
        return restTemplate.getForObject("http://hi-service/hello?name={1}",String.class,name);
    }
}

  • helloController.java
/**
 * Load balancing call control layer
 */
@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    /**
     * Request address: / hello (due to the current project context path / ribbon / hello)
     * Request parameter: name
     * Return data: "RIBBON:" + string of load balancing 8091 / 8092 / 8093
     * For example, this load balancing visit to 8091 is finally seen from this response
     * RIBBON: hello eureka Wang Cuihua, i am from 8091
     */
    @RequestMapping("/hello")
    public String sayHi(String name){
        return "RIBBON:"+helloService.sayHi(name);
    }
}

Conclusion 1: in the code, although the request url address points to hi service, the final access must be the detailed address of a back-end instance (localhost:8091/8092)
Conclusion 2: if the restTemplate can replace the hi service with 8091 / 8092, it must have been intercepted or filtered. Load balancing algorithm: polling - other algorithms

ribbon interception principle

In the ribbon client, the configured RestTemplate object has an ID @ LoadBalanced. Once the load is balanced, all http requests of the RestTemplate object pass through an interceptor LoadBalancerInterceptor, which is the main object of load balancing calculation.
1. Resolve the string of domain name location from the request – hi service

2. Get all instances of this service from the information captured by Eureka client (map.get("Hi service"))

3. Call an IRule calculation rule (the default implementation class RoundRobinRule polling) to calculate the final node corresponding to load balancing from several nodes and get the server object (including the specific back-end provider to be accessed)

4. Replace the hi service in the request path with this node
http://hi-service/hello->http://localhost:8092/hello
5. The request is sent to the destination

IRule interface and implementation class

In the ribbon component of springboot, you can manually configure the IRule implementation class to replace the load balancing calculation logic. Polling is the default
As long as there is a bean object of type Irule in the container, the default polling is no longer used

//Custom load balancing calculator default roundrobin rule
    @Bean
    public IRule ruleInit(){
        //Polling, weight (weight based on response time) random load balancing
        return new RandomRule();
        //WeightedResponseTimeRule
        //RoundRobinRule

Changes in overall structure

Tags: Java Spring Spring Boot microservice

Posted on Thu, 07 Oct 2021 14:13:14 -0400 by a1amattyj