Ribbon Load Balancing
1.1 About Load Balancing
Load balancing is generally divided into server load balancing and client load balancing
The so-called server-side load balancer, such as Nginx and F5, routes the request to the target server according to certain algorithms after it arrives at the server.
Client load balancing, for example, takes the Ribbon as an example. The service consumer client has a list of server addresses. The caller selects a server to access through a load balancing algorithm before making a request.
Ribbon is a load balancer released by Netflflix. Eureka is used with the Ribbon. The Ribbon reads service information from Eureka and loads the service provided by the service provider based on certain algorithms.
1.2 Ribbon Advanced Applications
There is no need to introduce additional Jar coordinates, because in the service consumer we introduced Eureka-client, which brings in Ribbon related jars
Use the following in the code to add annotations to the RestTemplate
@Bean
// Ribbon load balancing
@LoadBalanced
public RestTemplate getRestTemplate(a) {
return new RestTemplate();
}
Copy the code
1.3 Ribbon Load Balancing Policy
Ribbon built in a variety of load balancing strategy, responsible for internal complex balanced top interface for com.net flflix. The loadbalancer. IRule, class tree as follows
Load Balancing Policy
- RoundRobinRule: indicates a polling policy. By default, the server obtained for more than 10 times is unavailable and an empty server is returned
Example Modify a load balancing policy
Lougou-service-resume: ribbon: ribbon: ribbon: ribbon: ribbon: ribbon: ribbon: ribbon: ribbon: ribbon NFLoadBalancerRuleClassName:com.net flix. Loadbalancer. # RandomRule load strategy adjustmentCopy the code
1.4 Ribbon core source code analysis
How Ribbon works
Important: The Ribbon adds an interceptor to the restTemplate
Consider: What the Ribbon is doing: When we go to http://lagou-service-resume/resume/openstate/, The ribbon should retrieve the service instance list based on the service name lagou-service-resume, obtain an instance Server from the instance list based on certain load balancing policies, and finally request access through the RestTemplate
The Ribbon details the structure of the Ribbon (involving the description of the underlying components/classes)
The core is the LoadBalancer, surrounded by IRule, IPing, and so on
-
IRule: is the load balancing policy object at instance selection time
-
IPing: sends heartbeat checks to the service to determine whether the service is available
-
ServerListFilter: Filters the list of incoming service instances according to some rules
-
ServerListUpdater: Defines a set of operations to update a list of services
1.4.1 @loadBalanced
We add an @loadBalanced annotation to the RestTemplate instance to achieve load balancing. Amazingly, we’ll look at the operation behind this annotation.
1.4.1.1 Check the @loadBalanced annotation. Where is this annotation recognized?
Normal RestTemplate objects can be handled using LoadBalancerClient with the @LoadBalanced annotation
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Qualifier
public @interface LoadBalanced {
}
Copy the code
1.4.1.2 LoadBalancerClient class (implementation class RibbonLoadBalancerClient, inactive)
Public interface LoadBalancerClient extends ServiceInstanceChooser {// Execute a request from a service <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException; <T> T execute(String serviceId, ServiceInstance ServiceInstance, LoadBalancerRequest<T> request) throws IOException; // Constructuri (ServiceInstance instance, URI original); // Constructuri (ServiceInstance instance, URI original); }Copy the code