This is the 11th day of my participation in Gwen Challenge

One, foreword

There are two main load schemes:

  1. Centralized load balancing: Load balancing between consumers and service providers using separate agents, either a hardware load balancer, such as F5, or software, such as Nginx.

    As shown in figure:

  1. Client load balancing: The Ribbon is a framework for clients to load their own requests.

    As shown in figure:


The biggest differences between centralized and client load balancers are as follows:

** Maintenance of service instance information. ** Centralized load balancing information is maintained centrally, such as Nginx, and is specified in the configuration file.

The client load balancing information is maintained locally on the client and can be configured manually, but the most common is a timed pull from the registry.


RibbonBelong to the second category:

The Ribbon is a cloud-tested IPC library that controls load balancing behavior for HTTP and TCP clients.

When the Ribbon is configured with a service provider address, the Ribbon automatically assists service consumers with requests based on a load-balancing algorithm. By default, the Ribbon provides many load balancing algorithms, such as polling and random algorithms.


FeginIntegrated by defaultRibbon

RibbonThe sub-modules are as follows:

  • ribbon-loadbalancer: Load balancer that can be used independently or with other modulesAPI
  • ribbon-eureka:RibbonIn combination withEurekaThe client’sAPITo provide dynamic service registration list information for load balancers
  • ribbon-core:RibbonThe core of theAPI





Second,RibbonThe major components

The brain map is as follows:

The functions of each component are as follows:

component role
LoadBalancer Defines a series of operational interfaces, such as selecting service instances.
IRule Algorithmic policies, which have a number of built-in algorithmic policies to serve the selection of service instances.
ServerList Is responsible for obtaining service instance information, either from the configuration file or from the registry.
ServerListFilter You can filter out some unwanted service instance information.
ServerListUpdater Responsible for updating locally cached service instance information.
IPing Check the availability of existing service instances to ensure that selected services are available.

How does the service instance information come from this process of selecting the service instance?

As shown in figure:

  1. Storage component

    This is supported by a storage component of the service instance, which is ServerList. Storage is classified into static and dynamic modes.

    Static storage requires fixed service instance information. Dynamic storage requires service instance information from the registry.

  2. Filtering component

    Once you have the service information, you may need to filter some of the information in some scenarios. In this case, you can use the ServerListFilter component to filter the information.

  3. Update the components

    The Ribbon stores a service instance in local memory so that it doesn’t have to go to the registry for information every time. The problem with this scenario is that when service instances are added or reduced, how do you update them locally?

    In this case, the ServerListUpdater component is used for service instance update operations.

  4. Whether the available

    Another problem is that the cached service instance information may no longer be available. In this case, a detection component is needed to check whether the service instance information is available. This component is IPing.

  5. choose

    The Ribbon selects the available instance information based on the specified algorithm. The IRule component provides a variety of algorithm strategies for selecting instance information.

Finally, the entrance to use, to choose an available service, how to choose? Who needs this service?

This is where ILoadBalancer comes in. ILoadBalancer defines interfaces for software load balancing operations, such as dynamically updating a list of services and selecting an available service from an existing list of servers based on a specified algorithm.





Three, simple use

The main application is SpringCloud

There are three ways to use the Ribbon:

  1. The Ribbon is open source by Netflix. If you are not using Spring Cloud, you can use the Ribbon in your project. In this case, you need to use the Ribbon’s native API.

  2. Ribbon + RestTemplate. When a project integrates Spring Cloud, you can use the Ribbon to provide load balancing services for the RestTemplate.

  3. Ribbon + Feign

(1) OriginalAPIuse

ILoadBalancer balancer = new BaseLoadBalancer(); 

List<Server> servers = new ArrayList<Server>();
servers.add(new Server("localhost".8080));
servers.add(new Server("localhost".8088));

balancer.addServers(servers); 
for(int i = 0; i < 10; i++) {
    Server server = balancer.chooseServer(null);
    System.out.println(server);
} 
Copy the code


(2)Ribbon + RestTemplate

A brief introduction to the implementation principle:

  • Through theRestTemplateTo add@LoadBalancedAdding interceptors
  • Interceptor throughRibbonSelecting a service instance
  • Then replace the service name in the request address withRibbonOf the selected service instanceIPAnd port
  1. pom.xmlThe configuration file
        <! 'eureka-client' in 'SpringCloud' -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.2. RELEASE</version>
        </dependency>
        <! -- Directly import 'ribbon' -->
        <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
		</dependency>
Copy the code
  1. RestTemplateThe use of
@Configuration
public class RestTemplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(a) {

        return newRestTemplate(); }}Copy the code