“This is the 18th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”
The Ribbon is an open-source software tool for client load balancing developed by Netflix. It supports communication between clients in a cluster, helps control the behavior of HTTP and TCP clients, and provides many load balancing algorithms, such as polling and randomization, as well as customized algorithms.
In Spring Cloud’s microservices, Ribbon serves as a load balancer for service consumers in two ways, one with RestTemplate and the other with Feign. Feign already integrates the Ribbon by default, and we’ll cover more about Feign in Chapter 4.
The Ribbon contains many sub-modules, many of which are not used in production environments. The following are the sub-modules of the Ribbon currently used in production:
● Ribbon core: includes the definition of load balancing interface, client interface, built-in load balancing implementation AND other apis.
● Ribbon – Eureka: Provides the EUreka client load balancing API.
● Ribbon – HttpClient: Encapsulates Apache’s HttpClient. This module provides a REST client with load balancing functions.
Spring Cloud also allows developers to declare additional configuration (on RibbonClientConfiguration) – @ RibbonClient to achieve full control for the client. Such as:
@Configuration
@RibbonClient(name = "foo", configuration = FooConfiguration.class)
public class TestConfiguration {}Copy the code
In this case, the customer has comprised in RibbonClientConfiguration components and components of FooConfiguration (the latter usually cover the former).
Warning FooConfiguration must have @Configuration, but note that it is not in the @ComponentScan of the main application context, otherwise it will be shared by all @RibbonClients (meaning override all client defaults). If developers use @ComponentScan (or @SpringBootApplication), they must take steps to avoid being overridden (== for example by putting it in a separate, non-overlapping package ==, or by specifying the package to be scanned with @ComponentScan.
Introduce POM dependencies
<! - introduction of ribbon - >
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6. RELEASE</version>
</dependency>
<! - introduce erueka -- -- >
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6. RELEASE</version>
</dependency>
Copy the code
Configuration erueka
# configuration had been
eureka:
client:
register-with-eureka: false # Do not register yourself with Eureka
service-url: Get service address
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
Copy the code
Start the service on the main startup class
@SpringBootApplication
@EnableEurekaClient // Eureka client
@RibbonClient(name = "SPRINGCLOUD-PROVIDER-DEPT",configuration = MyRule.class )
public class DeptConsumer {
public static void main(String[] args) { SpringApplication.run(DeptConsumer.class,args); }}Copy the code
Creating a Configuration Class
Note that the package stored in the configuration class cannot be at the same level as the boot class 在MyRule
Class for springBoot configuration class declaration
@Configuration // Indicates that it is a basic configuration class
public class MyRule {
@Bean // Put the component into the IOC container
public IRule myrule(a){
return newMyRandomRule(); }}Copy the code
Class MyRule to implement load balancing. The custom class should inherit AbstractLoadBalancerRule from its parent.