Table of contents
1. Introduction to Load Balance
Three, Ribbon configuration steps
1.1 Create a commodity (product-service) service provider cluster
1.1.1 Create a product (product-service) service
1.1.2 Create a commodity (product-service) service provider cluster
1.2 Create a project order (order-service) service
1.2.1 Create an order (order-service) service
2 Add the related dependencies of Ribbon
3 Modify the OrderController of the order-service service
Method 1: Use the annotation @LoadBalaneced
The following is to understand the content
Four Ribbon's IRule components and load balancing strategy
2. Case: load balancing - random strategy
3. Load balancing random strategy test
1. Custom defined load balancing class
3. Test custom load balancing with 3.6 for testing
1. Introduction to Load Balance
LB, or Load Balance , is an application often used in microservices or distributed clusters.
Simply put, load balancing is to distribute user requests evenly to multiple services, so as to achieve HA of the system.
Common load balancing hardware: F5, software: ( for server: Nginx, for client: Ribbon, Feign )
2. Ribbon
To put it simply, Ribbon , the load balancing software for clients , is an open source project released by Netflix. Its main function is to provide client software load balancing algorithms and connect Netflix's middle-tier services together. The Ribbon client component provides a series of comprehensive configuration items such as: connection timeout, retry, etc. Simply put, it is to list all the machines behind Load Balancer (LB for short) in the configuration file , and Ribbon will automatically help you connect these machines based on certain rules (such as simple polling, random connection, etc.) . We can also easily implement custom load balancing algorithms using Ribbon.
Spring Cloud Ribbon is a set of client load balancing tools based on Netflix Ribbon . It is a client-side load balancer based on HTTP and TCP . It can configure the RibbonServerList in the client to set the server list to poll for access to achieve load balancing.
3. Ribbon configuration steps
1. Create a project
1.1 Create a commodity (product-service) service provider cluster
1.1.1 Create a product (product-service) service
To create a product (product-service) service, refer to the product (product-service) service and perfect product service in Chapter 3. Combining the two is a complete product (product-service) service. For details, see: Chapter 3 Spring Cloud Components and Getting Started Cases - Programmer Sought
1.1.2 Create a commodity (product-service) service provider cluster
- method one
In order to build multiple product (product-service) services, copy the created product (product-service) service twice, that is, three product (product-service) services, the service name is unified as: product-service, the project names are: productServer9001, productServer9002, productServer9003, and the port numbers are respectively set to: 9001, 9002, 9003, which are configured in application.yml as follows:
- way two
Use the following method: Duplicate multiple projects without creating a project.
1.2 Create a project order (order-service) service
Create order-service and product-service services.
1.2.1 Create an order (order-service) service
Create an order (order-service) service, see the order (order-service) service and improve the order service in Chapter 3. Combining the two is a complete order (order-service) service. See:
https://blog.csdn.net/qq_41946216/article/details/127276089?spm=1001.2014.3001.5502
2 Add the related dependencies of Ribbon
Modify the pom file of the order (order-service) service, and add the related dependencies of Ribbon .
<?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>springcloudbase</artifactId>
<groupId>com.hwadee.springcloud2022</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.hwadee.springcloud2022</groupId>
<artifactId>orderServer9000</artifactId>
<dependencies>
<!--web dependency-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Manage public api -->
<dependency>
<groupId>com.hwadee.springcloud2022</groupId>
<artifactId>springcloud-api</artifactId>
</dependency>
<!-- Ribbon related start-->
<!--Tell the microservice registration center that I am the client-side Ribbon of the microservice and need to integrate with eureak-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- Ribbon related end-->
<!--Introduce the client component of eureka service-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</project>
3 Modify the OrderController of the order-service service
Problem Description:
In the OrderController of the consumer-side service, that is, the order-service service, the original remote calls are as follows:
@RestController
@RequestMapping("/order")
public class OrderController { // The order service needs to call the ip address and port number of the remote commodity service private static final String REST_URL_PREFIX = "http://localhost:9001"; /** *Use * Using restTemplate to access the restfu1 interface is very simple and rude. * (url, requestMap, ResponseBean.class) These three parameters respectively represent the * REST request address, request parameters, and the object type that HTTP response conversion is converted into. **/ @Autowired private RestTemplate restTemplate; @RequestMapping("/buy/{id}") public Product buy(@PathVariable Long id) { System.out.println("Enter the buy() id of OrderController: "+id); return restTemplate.
}
The localhost and port number of REST_URL_PREFIX = "http://localhost:9001" in the above code are hard-coded. If there are multiple remote services (product (product) services) to be accessed in the future, the ip address and port number need to be manually modified frequently. Therefore, the ip of multiple remote services is uniformly named as the service name, such as product-service, so that REST_URL_PREFIX = "http://localhost:9001" can be changed to REST_URL_PREFIX = "http:// PRODUCT-SERVICE " , because @LoadBalanced searches for the ip and port number of the corresponding service based on the microservice name PRODUCT-SERVICE, otherwise it cannot be accessed.
Modify the OrderController of the order (order-service) service as follows:
import com.hwadee.springboot.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/order")
public class OrderController {
private static final String REST_URL_PREFIX = "http://PRODUCT-SERVICE";
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/buy/{id}")
public Product buy(@PathVariable Long id) {
System.out.println("进入OrderController的buy() id:"+id);
return restTemplate.getForObject(REST_URL_PREFIX + "/product/buy/"+id,Product.class);
}
}
4 Configure load balancing
There are two ways to configure Ribbon load balancing. There are annotation forms and configuration file forms. Later, Feign will talk about the configuration file form.
Method 1: Use the annotation @LoadBalanced
@LoadBalanced annotation on RestTemplate. Modify the configbean of the order service and add the annotation @LoadBalanced , because it is a load balancing distribution for the remote call of the client service (order (order-service) service). @LoadBalanced is to find the ip and port number of the corresponding service according to the microservice name PROVIDER-SERVICE . The ip and port number foundare obtained after load balancing. code show as below:
@Configuration//此类相当于spring中的容器对bean的管理生成。
public class ConfigBean {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
5 for testing
- First start the registry service: eurekaServer main startup class EurekaServerApplication
- Second, start the order service: order-service main startup class OrderServerApplication
- Start the commodity service productServer9001: the main startup class ProductServerApplication of product-servie
- Start the commodity service productServer9002: the main startup class ProductServerApplication of product-servie
- Start the commodity service productServer9003: the main startup class ProductServerApplication of product-servie
Access the order service multiple times: http://localhost:9000/order/buy/1 View the returned ip and port information
The following is to understand the content
Four R ibbon 's IRule component and load balancing strategy
1. Load balancing strategy
IRule: According to a specific algorithm , select a service to be accessed from the service list . Robin defaults to seven strategies.
RoundRobinRule
polling
RandomRule random AvailbilityFilteringRule First filter out:
1. Services with circuit breakers tripped due to multiple access failures
2. Services whose number of concurrent connections exceeds the threshold
Secondly, access the remaining service list according to the polling strategy
WeightedResponseTimeRule Response time weighting:
1. Calculate the weight of all services based on the average response time,
The faster the response time, the higher the service weight, the higher the probability of being selected. If the statistical information is insufficient at the beginning, the RoundRobinRule polling strategy will be used. When the statistical information is sufficient, it will switch to WeightedResponseTimeRule
RetryRule: Retry First obtain the service according to the strategy of RoundRobinRule. If the service fails to obtain, it will retry within the specified time to obtain the available service.
BestAvailableRule: Best 1. First filter out services that are in a circuit breaker trip state due to multiple access failures
2. Next, choose a service with the smallest amount of concurrency
ZoneAvoidanceRule The default rule is to judge the performance of the area where the server is located and the availability of the server to select the server (the principle of proximity, select the server and service in the same area. If there is no area concept, select according to linear polling)
2. Case: load balancing - random strategy
3. Load balancing random strategy test
Test as in: 3.6 Perform the same test.
Five custom load balancing
Pay attention to details:
The official document clearly gives a warning:
this custom configuration class cannot be placed under the current package and sub-packages scanned by @ComponentScan, otherwise our custom configuration class will be shared by all Ribbon clients, which means that we cannot achieve the purpose of special customization.
1. Custom defined load balancing class
Define the load balancing class, and the return value type must be IRule type. But be careful: the custom load balancing class cannot be in the same parent directory as the app, that is, the directory of the custom load balancing class is at the same level as the root directory where the app is located, and the package path is as shown in the figure below.
code show as below:
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MySelfRule {
@Bean
public IRule myRule() {
return new RandomRule();// Ribbon默认是轮询,我自定义为随机
}
}
2. Use custom load balancing
Modify the order-service main startup class OrderServerApplication, add the annotation @RibbonClient (name="PRODUCT-SERVICE", configuration=MySelfRule.class, specify a custom configuration class, and load our custom Ribbon configuration class when starting the microservice, so that the custom configuration class MySelfRule takes effect.
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;
import rule.MySelfRule;
@SpringBootApplication
@EnableEurekaClient// 启动 eureka 客户端
@RibbonClient (name="PRODUCT-SERVICE" , configuration= MySelfRule.class)
public class OrderServerApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServerApplication.class, args);
}
}
3. Test custom load balancing with 3.6 for testing
Test as in: 3.6 Perform the same test.
Chapter 3: Spring Cloud Components and Getting Started Cases
Chapter 5: Detailed explanation of Feign load balancing