What is Ribbon
Ribbon is a customer service load balancing tool based on HTTP and TCP, which is implemented based on Netflix Ribbon.
It is not deployed independently like Spring Cloud service registry, configuration center and API gateway, but it exists in almost every Spring Cloud microservice. Including the declarative service call provided by Feign is also based on the Ribbon implementation.
Ribbon provides many load balancing algorithms by default, such as polling, random, and so on. Even include custom load balancing algorithm.
What problems did the Ribbon solve
Ribbon provides a load balancing solution for microservices.
Difference of different load balancing schemes
At present, the mainstream load balancing schemes in the industry can be divided into two categories:
- Centralized load balancing (server load balancing), that is, independent load balancing facilities (such as hardware, such as F5, or software, such as nginx) are used between the consumer and the provider, which is responsible for forwarding access requests to the provider through a certain strategy;
- In process load balancing (client load balancing) integrates the load balancing logic into the consumer. The consumer obtains the available addresses from the service registry, and then selects an appropriate provider from these addresses. The Ribbon belongs to the latter. It is just a class library, integrated into the consumer process, through which the consumer obtains the provider address.
Centralized load balancing
In process load balancing
Ribbon load balancing strategy
Polling policy (default)
Policy corresponding class name: roundrobin rule
Implementation principle: polling strategy means that one provider is taken down in sequence every time, for example, there are five providers in total, the first provider is taken for the first time, the second provider is taken for the second time, the third provider is taken for the third time, and so on.
Weight polling strategy
Policy corresponding class name: WeightedResponseTimeRule
Implementation principle:
- A weight is assigned according to the response time of each provider. The longer the response time is, the smaller the weight is, and the less likely it is to be selected.
- Principle: start with polling strategy and start a timer to collect the average response time of each provider every 30 seconds. When the information is enough, attach a weight to each provider, and select the provider randomly according to the weight. The provider with higher weight will be selected with high probability.
Stochastic strategy
Policy corresponding class name: RandomRule
Implementation principle: select one randomly from the provider list.
Minimum concurrent number policy
Policy corresponding class name: BestAvailableRule
Implementation principle: select the provider with the minimum concurrent number in the request, unless the provider is in fusing.
Retry strategy
Policy corresponding class name: RetryRule
Implementation principle: in fact, it is the enhanced version of polling policy. When the polling policy service is unavailable, it will not be processed. When the retry policy service is unavailable, it will try other nodes in the cluster again.
Availability sensitive policy
Policy corresponding class name: availabilityfiltering rule
Implementation principle: provider with poor filtering performance
- The first is to filter out provider s in Eureka that have failed to connect all the time.
- Second, filter out the provider s with high concurrency (busy).
Regional sensitivity strategy
Policy corresponding class name: zoneaavoidancerule
Implementation principle:
- The availability is evaluated in one area. For the whole area that is not available, select the available provider from the remaining areas.
- If one or more instances in this ip area are not reachable or the response is slow, the right to select other IPS in this ip area will be reduced Heavy.
Introduction to Ribbon
The default load balancing policy for clustered services in Ribbon is polling.
Create project
Use the Eureka demo project when learning Eureka to create the subproject service-provider02 in this project
Add dependency
pom.xml
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>service-provider02</artifactId> <version>1.0-SNAPSHOT</version> <!-- Inherit parent dependency --> <parent> <groupId>com.example</groupId> <artifactId>eureka-demo</artifactId> <version>1.0-SNAPSHOT</version> </parent> <!-- Project dependence --> <dependencies> <!-- netflix eureka client rely on --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- spring boot web rely on --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- lombok rely on --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <!-- spring boot actuator rely on --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- spring boot test rely on --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
configuration file
application.yml
server: port: 7071 # port spring: application: name: service-provider # Application name (same under cluster) # Configure Eureka Server registry eureka: instance: prefer-ip-address: true # Use ip address to register instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port client: service-url: # Set service registry address defaultZone: http://root:123456@localhost:8761/eureka/,http://root:123456@localhost:8762/eureka/ # Measurement monitoring and health examination management: endpoints: web: exposure: include: shutdown # Turn on shutdown endpoint access endpoint: shutdown: enabled: true # Turn on shutdown to realize elegant service stop
Service and startup
Copy and paste a copy of all the codes to server-provider02, and modify the name of the startup class.
ServiceProvider02Application.java
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ServiceProvider02Application { public static void main(String[] args) { SpringApplication.run(ServiceProvider02Application.class, args); } }
In order to see the effect of load balancing more intuitively, we print the service address to the console in the service consumer project.
Registry Center
Run the complete Eureka environment, visit: http://localhost:8761/ You can see that there are already two service providers.
Consumer service
Multiple visits: http://localhost:9090/order/1 You can see that the polling policy is used by default.
Ribbon load balancing policy settings
Overall situation
Inject the load balancing policy object into the startup class or configuration class. This policy is used by all service requests.
@Bean public RandomRule randomRule() { return new RandomRule(); }
Multiple visits: http://localhost:9090/order/1 The results are as follows:
local
Modify the load balancing policy of the specified service in the configuration file. Format: service application name.ribbon.NFLoadBalancerRuleClassName
# Load balancing strategy # Service provider is the name of the called service service-provider: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Multiple visits: http://localhost:9090/order/1 The results are as follows:
Ribbon point to point direct connection
Point to point direct connection refers to bypassing the registry and directly connecting service providers to obtain services, which is commonly used in the test phase.
Add dependency
When introducing Ribbon into pom files, it should be noted that if there is Eureka dependency in pom, it is necessary to remove Eureka dependency.
<!-- netflix ribbon rely on --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
configuration file
Close Eureka in the configuration file and add the directly connected service address. If load balancing policy is not set, polling policy is used by default.
# Load balancing strategy # Service provider is the name of the called service service-provider: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # Specify specific Provider service list, multiple separated by commas listOfServers: http://localhost:7070,http://localhost:7071 # Close Eureka to realize the point-to-point direct connection of Ribbon ribbon: eureka: enabled: false # false: off, true: on
Visit
Close the Eureka registry. The service provider will report a connection exception because it cannot connect to the registry. But the service can be consumed normally, so the current use of point-to-point method to call.
Multiple visits: http://localhost:9090/order/1 The results are as follows:
At this point, all knowledge points of Ribbon load balancing are finished.
You can go through classification See more about Spring Cloud Article.
This paper adopts Intellectual sharing "signature - non-commercial use - no deduction 4.0 international" License Agreement.
This chapter is over here. If you like it, please like it and forward it.
A kind of Pay attention to Mr. halloward and learn more IT technology~