Teach you how to build a SpringCloud project (8) Integrate Ribbon load balancer

What are microservices? A series will be seen at a glance!

1. Teach you how to build a SpringCloud project (1) Detailed explanation with pictures and texts, fool-like operation

2. Teach you how to build a SpringCloud project (2) Producers and consumers

3. Teach you how to build a SpringCloud project (3) Integrate the Eureka service registration center

4. Teach you how to build the SpringCloud project (4) Eureka cluster version construction

5. Teach you how to build the SpringCloud project (5) Build the producer cluster version

6. Teach you how to build a SpringCloud project (6) Eureka realizes service discovery

7. Teach you how to build a SpringCloud project (7) Integrate the Consul service registration center

8. Teach you how to build a SpringCloud project (8) Integrated Ribbon load balancer

9. Teach you how to build a SpringCloud project (9) Integrate OpenFeign service interface calls

10. Teach you how to build a SpringCloud project (10) Integrate Hystrix service downgrade

11. Teach you to build a SpringCloud project (11) Integrating Hystrix's service fuse

12. Teach you how to build a SpringCloud project (12) Integrate Hystrix's graphical Dashboard real-time monitoring

13. Teach you how to build a SpringCloud project (13) Integrate a new generation of Gateway

14. Teach you how to build a SpringCloud project (14) Integrated Config Distributed Configuration Center

15. Teach you how to build a SpringCloud project (15) Integrated Bus message bus

16. Teach you how to build a SpringCloud project (16) Integrated Stream message driver

17. Teach you how to build a SpringCloud project (17) Integrating Sleuth distributed link tracking

Continue to update, welcome to like and follow!

1. Introduction to Ribbon

Ribbon is an open source project released by Neflix. It was later encapsulated in Spring Cloud by the Spring Cloud development team. It allows us to easily convert service-oriented REST template requests into client-side load-balancing service calls. The function is to provide the client software load balancing algorithm and service call. Ribbon is a client load balancing tool based on HTTP and CP. Although Spring Cloud Ribbon is just a tool framework, it does not require independent deployment like service registry, configuration center, and API gateway, but it exists in almost every Spring Cloud In the microservices and infrastructure built. Because calls between microservices, API gateway request forwarding, etc. are actually implemented through Ribbon, including Feign, which we will introduce later, which is also a tool based on Ribbon. Therefore, the understanding and use of Spring Cloud Ribbon is very important for us to use Spring Cloud to build microservices. The Ribbon client provides a series of comprehensive configuration items, such as connection timeout, retry, etc. Simply put, all machines behind Load Balancer (LB for short) are listed in the configuration file, and Ribbon will automatically help you connect to these machines based on certain rules (such as simple polling, random, etc.). We are very container using Ribbon to implement a custom load balancing algorithm.

What is Load Balancer?

Simply put, it is to distribute the user's request to multiple services equally, so as to achieve the HA (high availability) of the system. Common load balancing software includes: Nginx, LVS, hardware F5, etc.

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

Nginx is the load balancer of the server. All requests from the client will be handed over to Nginx, and then Nginx will forward the request, that is, the load balancer is implemented by the server.
Ribbon local load balancing, when invoking the service interface, will obtain the registration information service list from the registration center and buffer it locally in the JVM, so as to realize the RPC remote service invocation technology locally.

What are the two types of LB load balancing?

Centralized LB, that is, there is an independent LB facility between the service consumer and provider (it can be hardware, such as F5, or software, such as Nginx), and the facility is responsible for forwarding access requests through a certain strategy to the provider of the service.

The in-process LB integrates the LB logic with the consumer. The consumer learns which addresses are available from the registration center, and then selects a suitable server from these addresses. Ribbon is an in-process LB. It is just a class library integrated in the consumer process, and the consumer obtains the address of the service provider through it.

In the previous article, we realized the round-robin assignment balance of consumers accessing producer clusters. For details, please click to view. Ribbon is to realize the load balancing of the server and the call of RestTemplate. It is a client component of soft load balancing, which can be used in combination with other clients that require requests, and the combination with Eureka is just one example.

2. Code example

Since the spring-cloud-starter-netflix-eureka-client comes with the spring-cloud-starter-ribbon reference, there is no need to introduce the ribbon package, as shown below:

insert image description here

The architecture diagram of the Ribbon is as follows:

insert image description here

Ribbon is divided into two steps when working:

The first step is to choose EurekaServer, which gives priority to servers with less load in the same area.
In the second step, according to the policy specified by the user, select an address in the service registration list obtained from the Server.
Among them, ribbon provides a variety of strategies, such as polling, random, weighting according to time response time, etc.

Ribbon's core component IRule

The knot diagram of the IRule algorithm:
insert image description here

IRule's seven load algorithms

insert image description here

To change the load algorithm from our polling method to a random algorithm, we need to customize a configuration class. Note: this custom class cannot be placed under the current package scanned by @ComponentScan or under the package, otherwise we customize this The configuration class will be shared by all Ribbon clients, and the purpose of special customization cannot be achieved. As shown below:

insert image description here

Create a new MyselfRule configuration class and change it to random, as shown in the figure below:

package com.buba.springclould.myrule;
 
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//自定义负载均衡路由规则类
@Configuration
public class MyselfRule {
    
    
    @Bean
    public IRule myRule() {
    
    
        // 定义为随机
        return new RoundRobinRule();
    }   
}

Add the @RibbonClient annotation to the main startup class and configure it, as shown in the figure below:

package com.buba.springclould.order;
 
import com.buba.springclould.myrule.MyselfRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
 
@SpringBootApplication
@EnableEurekaClient
//name为生产者服务的服务名称  configuration为配置类的类名
@RibbonClient(name = "mcroservice-payment",configuration = MyselfRule.class)
public class OrderMain {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(OrderMain.class,args);
    }
}

Then start eureka7001 and 7002 now, then start the server service 8001 and 8002, start the producer service 80, access the path http://localhost/consumer/payment/get/1, and switch back and forth between the original polling 8001 service and 8002 service , becomes 8001 service and 8002 service random access.

Ribbon load balancing principle:

insert image description here

Simply integrate Ribbon's load algorithm and you're done. so easy!
insert image description here

In the next article, learn to call the OpenFeign service interface, keep paying attention and like it. We continue to update.

Guess you like

Origin blog.csdn.net/weixin_39570655/article/details/131810185