Micro Service Ribbon

Time:2020-9-19

Spring cloud ribbon is a client load balancing tool based on HTTP and TCP. It is implemented based on Netflix ribbon. Through the encapsulation of spring cloud ribbon, it is very simple to use client load balancing call in microservice architecture. Ribbon is a relatively complex module in spring cloud family, which directly affects the quality and performance of service scheduling.

1、 Client load balancing

Load balancing is one of the important means to improve system availability, relieve network pressure and expand processing capacity. Load balancing is divided into:

  • Server load balancing is divided into hardware load balancing (such as F5) and software load balancing (such as nginx)
  • Client load balancing (e.g. ribbon)

(1) Server load balancing: maintain a list of available servers, eliminate the failed server nodes through heartbeat detection, and ensure that all the server nodes in the list can be accessed normally. When the client sends a request to the load balancing device, the device takes a server address from the list of available servers according to some algorithm (polling, weighting, etc.), and then forwards it.

(2) Client load balancing: the biggest difference from server load balancing is the location of the service list. In the client-side load balancing, all the client nodes maintain the list of servers they want to access, and these server-side lists come from the service registry.

2、 Integration of feign and feign

In the scenario of combining feign with feign, the final request will be forwarded to the format of http: / / & lt; service name > / < relative path to Service >, and then through the loadbalancer feign client,
Extract the service ID < service name >, and then find the load balancer feignloadbalancer of the corresponding service in the context according to the service name. Load balancer is responsible for selecting the most suitable service instance according to the statistical information of existing service instances.

3、 Principle

(1) Ribbon provides uniform server selection function through Iload balancer interface, which will select appropriate server to return to users according to different load balancing policies.

public interface ILoadBalancer {
    void addServers(List<Server> var1);

    Server chooseServer(Object var1);

    void markServerDown(Server var1);

    /** @deprecated */
    @Deprecated
    List<Server> getServerList(boolean var1);

    List<Server> getReachableServers();

    List<Server> getAllServers();
}

(2) IRule is the abstraction of load balancing policy. Iloadbalancer returns to server by calling the choose() method of iRule. Its core method is as follows:

public interface IRule {
    Server choose(Object var1);

    void setLoadBalancer(ILoadBalancer var1);

    ILoadBalancer getLoadBalancer();
}

The implementation classes of iRule are as follows:

  • Best available rule skips the failed server and selects the server with the lowest concurrent requests among the remaining servers
  • Clientconfignenabledroundrobin rule polling
  • Round robin rule polling
  • Random rule random selection
  • Retryrule can be retryable policy, other policies can be retried, default polling retry
  • Weighted response time rule is weighted according to the response time. The shorter the response time, the greater the weight
  • Availability filtering rule removes servers that are broken due to continuous links, read failures, or links exceeding the maximum limit. Polling is performed in the remaining read servers

(3) Iping is used to detect whether the server is available. The implementation class of iloadbalancer maintains a timer every 10 seconds( this.pingIntervalSeconds =10) check the available status of the server once

public interface IPing {
    boolean isAlive(Server var1);
}

4、 Summary

  • Ribbon is the core module in spring cloud framework, which is responsible for service load calling. Ribbon can be used independently from spring cloud.
  • Ribbon is a load balancing framework for clients. Each client independently maintains its own call information statistics and is isolated from each other. Ribbon’s load balancing performance is not completely consistent on each machine
  • Ribbon is the most complex part of the whole component framework. In order to ensure the high availability of services, there are many detailed parameter controls in the control process

Recommended Today

On the theoretical basis of SRE

What is SRE? When I first got into contact with SRE, many people thought that it was a post with full stack capability in Google and could solve many problems independently. After in-depth exploration, it is found that SRE can solve many problems, but there are too many problems. It is difficult for a post […]