Microservices Architecture | 4.1 Load balancing based on Ribbon

Reference materials:SpringCloud Ribbon is based on SpringCloud Ribbon. SpringCloud Ribbon is based on SpringCloud Ribbon. SpringCloud Ribbon is based on SpringCloud AlibabaNetflix Ribbon is a client load balancing tool.Provide client software load balancing algorithm and service invocation;Spring Cloud Ribbon is a set of client load balancing tools based on Netflix Ribbon.Provide client software load balancing algorithm and service invocation;The Ribbon client component provides a comprehensive set of configuration options such as connection timeout and retry.Simply put, the Ribbon lists all the machines behind the Load Balancer in the configuration file. The Ribbon will automatically connect these machines based on certain rules (simple polling, random join, etc.).Custom load balancing algorithms can be easily implemented using the Ribbon.1.2 Three Levels of Interaction with the Ribbon Spring DiscoveryClient provides minimal access to the Ribbon and the registry services cached in the Ribbon.Spring DiscoveryClient of RestTemplate is enabled.Netflix Feign client;1.3 The Ribbon works in two steps. The first step is to select EurekaServer. It preferentially selects servers with less load in the same area.The second step is to select an address from the service registration list obtained from the server according to the user-specified policy.1.4 Service providers and consumer providers: Service providers register themselves in the registry for consumers to discover;In this example, there are multiple providers providing services to consumers;Consumers: Consumers use service discovery, find providers and invoke provider services;In this example, only one consumer selects one of the multiple providers to serve him;1.5 Ribbon Core component IRule selects a service from the service list based on a specific algorithm.Defines the load balancing mode.The load balancing modes are as follows: RoundRobinRule: polling.RandomRule:RetryRule: The system obtains services according to the RoundRobinRule policy. If the services fail to be obtained, the system tries again within a specified time to obtain available services.WeightedResponseTimeRule: an extension to RoundRobinRule, where faster instances have more weight and are more likely to be selected;BestAvailableRule: Filters out the service in the circuit breaker trip state due to multiple access failures, and selects the service with the least concurrency.AvailabilityFilteringRule: to filter out the first example, choose the instance of concurrent smaller again;ZoneAvoidanceRule: Default rule that selects a server for performance in the zone where the Server is located and for availability of the Server;An example of three levels of service consumer acquisition of providers 2.1 Introduction of the PM.xml dependency <dependency>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency>If you use Eureka as the registry, you don’t need to import this dependency because Eureka’s dependencies include the Ribbon dependency JAR package.2.2 Using Spring DiscoveryClient to find service instances 2.2.1 Add the @enableDiscoveryClient annotation on the main program class to indicate that it can be discovered by the registry.Sring Cloud is a trigger that enables applications to use DiscoveryClient and Ribbon libraries.2.2.2 Use DiscoveryClient to find information in the Client package of service consumers.This approach has the following problems: It does not leverage the Ribbon’s client-side load balancing, and the developer needs to define which service instance to invoke;The developer must construct a URL to invoke the service;Instantiate the ResTemplate class, which does not comply with the Spring IoC specification;In combination with this article 5. Implementation of Local Load Balancer (Consumer), client load balancing can be used, that is, developers define a local load balancer to achieve load balancing;In earlier versions of Spring Cloud, the RestTemplate class automatically supports the Ribbon by default.Since the Angel release of Spring Cloud, the RestTemplate in Spring Cloud no longer supports the Ribbon.Therefore, later versions must be explicitly annotated with the @loadBalanced annotation to use the Ribbon and RestTemplate together;*RestTemplate doesn’t have to be in the main program class;You can also create a new ApplicationContextConfig config class under the config package and place the RestTemplate in it:2.3.2 Use the RestTemplate in the Ribbon to invoke services in the client package of service consumers;By using the RestTemplate class, the Ribbon polls load-balancing requests across all service instances;2.4 Using Netflix Feign Client to call service Feign Related knowledge will be explained in the next part “4.2 Service Interface Call Based on Feign and OpenFeign”. Here, we only focus on the difference and comparison with the above two call provider services;2.4.1 Add annotation @enableFeignClients to main program class: enable Feign client.2.4.2 Defining the Feign interface for invoking service providers @feignClient (“provider-instance-name”)
// Identify the service as feign’s client
Public interface ProviderFeignClient {// Defines the path and action of the endpoint
method= RequestMethod.GET,
// Defines the parameters passed into the endpoint, which can be called by the client to trigger the organization service
Provider getProvider(@PathVariable(“providerId”)
String providerId);
} With the ProviderFeignClient class, developers simply need to auto-assemble and use it;3. Customize the load balancing algorithm using Java configuration classes (consumer service). Switch the default load balancing algorithm and the existing one is used (different from the local load balancer, which needs to be implemented by itself).Note: Custom configuration classes cannot be stored in the current and sub-packages scanned by @ComponentScan. Otherwise, the custom configuration classes are shared by all Ribbon clients.The @ComponentScan annotation is wrapped in the @SpringBootApplication annotation on the main bootstrap class.By default, it scans the package of the main startup class and its subpackages, so we need to go back to the upper directory and create a new myRule directory to store our customized load balancing configuration classes.@Configuration
public class MySelfRule {
public IRule myRule(){
return new RandomRule();// Defined as random
}3.2 Add annotation @RibbonClient(name = “provider-instance-name”,configuration= myselfrule. class) to the main bootstrap class: the custom load balancing algorithm is used.Name: Specifies the instance name of the service provider;Configuration: Specifies the load balancing class to be used.The Provider service uses the Ribbon configuration corresponding to MySelfRule.Again, the RestTemplate class needs to be declared with the @loadBalanced annotation;4. Configure a custom load balancing algorithm (consumer service) to switch the default load balancing algorithm. The default load balancing algorithm is still available (different from the local load balancer, which needs to be implemented by the local load balancer).4.1 Modifying the Bootstrap. yml Configuration File The effect of the above Java configuration class is equivalent to the following configuration file: 4.2 The main program class does not require the @RibbonClient annotation;Again, the RestTemplate class needs to be declared with the @loadBalanced annotation;5. Implementation of local load balancers (consumer) Local load balancers are different from custom load balancing algorithms;The load balancing algorithm of the former needs to be implemented manually, while the latter just switches to another off-the-shelf load balancing algorithm.5.1 Not Using RestTemplate the main program class does not need to declare the RestTemplate class with the @loadBalanced annotation.The @loadbalanced annotation can be deleted or commented out;5.2 Defining the Load Balancing Interface You can create a package to store the load balancing algorithm written by ourselves.public interface LoadBalancer{
ServiceInstance instances(List<ServiceInstance> serviceInstances);
}5.3 Implementing load balancing Interfaces You can write different load balancing algorithms based on service requirements. This section provides only one example.This example implements a simple atomic load balancing algorithm.5.4 Using a local load balancer in the Controller Interface is similar to 2.2 Using Spring DiscoveryClient to Find service Instances.The difference is that there is no load balancer in 2.2. Based on 2.2, developers define their own local load balancer instead of using the load balancer provided by the Ribbon.Therefore, 5.1 Not Using RestTemplate does not use the @loadBalanced annotation to display declarations on the RestTemplate class.Source: https://www.cnblogs.com/dlhjw/p/15845348.html

Leave a Reply

Your email address will not be published.