What is the ribbon?
The Ribbon is an open source project by Netflix that provides a client-side software load-balancing algorithm that connects Netflix's middle-tier services. The Ribbon client component provides a complete set of configuration items such as connection timeouts, retries, and so on. Simply put, all the machines behind load Balancer (lb) are listed in the configuration file, and the Ribbon automatically helps you to connect the machines based on certain rules (such as simple polling, then connecting, and so on). It is also easy to implement a custom load balancing algorithm using the Ribbon.
LB Program Classification
The current mainstream LB scheme can be divided into two categories: a centralized lb, which is the use of a separate lb facility between the consumer and provider of the service (either hardware, such as F5, or software, such as Nginx), which is responsible for forwarding the access request through some policy to the provider of the service, and the other in-process lb, Integrating the LB logic into the consumer, the consumer learns from the service registry which addresses are available and then chooses a suitable server from those addresses themselves. The ribbon belongs to the latter, which is just a class library, integrated in the consumer process, where the consumer obtains the address of the service provider.
Three: The main components and workflow of the Ribbon
The core groups of the Ribbon (all interface types) have the following:
ServerList
Used to get the address list. It can be either static (providing a fixed set of addresses) or dynamic (the list of addresses is queried periodically from the registry).
Serverlistfilter
Used only when using dynamic serverlist to use a certain policy in the original list of services to remove a subset of addresses.
IRule
Select a final service address as the LB result. The selection strategy has polling, weighting based on response time, breaker (when Hystrix is available), etc.
The ribbon prefers to use serverlist to get a list of all available services at work, then serverlistfilter a portion of the address, and finally selects a server as the final result in the remaining address through Irule.
IV: Introduction to the main load balancing strategy provided by the Ribbon
1. Simple poll Load balancer (roundrobin)
The request is polled in turn to schedule the different servers, that is, each time the schedule executes i = (i + 1) mod n, and the first server is selected.
2. Random Load Balancing
Randomly select server with a status of up
3. Weighted response Time Load balancing (weightedresponsetime)
Assign a weight according to the corresponding time, the longer the corresponding time, the smaller the weight, the lower the likelihood of being selected.
4. Zone-aware polling load balancing (zoneavoidancerule)
Compound to determine the performance of the server area and the availability of the server Select server
Ribbon self-load balancing strategy comparison
| Policy Name |
Policy statement |
Policy description |
Implementation Notes |
| Bestavailablerule |
public class Bestavailablerule extends Clientconfigenabledroundrobinrule |
Select a minimum of concurrent requests for the server |
Examine the server individually, if the server is tripped, ignore it, and select the server activerequestscount the smallest |
| Availabilityfilteringrule |
public class Availabilityfilteringrule extends Predicatebasedrule |
Filter out back-end servers that are flagged as circuit tripped because they have failed to connect, and filter out those highly concurrent backend servers (active connections exceeds the configured threshold) |
The logic of using a availabilitypredicate to contain the filtering server is to check the status of each server that is logged in the status |
| Weightedresponsetimerule |
public class Weightedresponsetimerule extends Roundrobinrule |
Assign a weight according to the corresponding time, the longer the corresponding time, the smaller the weight, the lower the likelihood of being selected. |
A background thread periodically reads the evaluation response time from the status and computes a weight for each server. The calculation of weight is also relatively simple responsetime minus each server's own average responsetime is the weight of the server. Use Roubine policy to select server when the Statas is not formed when it is first run. |
| Retryrule |
public class Retryrule extends Abstractloadbalancerrule |
The retry mechanism on the selected load balancing policy machine. |
During a configuration time period when the server selection is unsuccessful, try to select an available server using the Subrule method |
| Roundrobinrule |
public class Roundrobinrule extends Abstractloadbalancerrule |
Roundrobin Mode Polling Select server |
Poll Index, select the server that corresponds to the index location |
| Randomrule |
public class Randomrule extends Abstractloadbalancerrule |
Randomly select a server |
Randomly on index, select the server that corresponds to the index location |
| Zoneavoidancerule |
public class Zoneavoidancerule extends Predicatebasedrule |
Compound to determine the performance of the server area and the availability of the server Select server |
Using Zoneavoidancepredicate and Availabilitypredicate to determine whether a server is selected, the previous decision determines whether the performance of a zone is available and rejects the unavailable zone (all servers). Availabilitypredicate is used to filter out servers with too many connections. |
V. Use of
Introduction of POM
< Dependency > < groupId >org.springframework.cloud</groupId> < Artifactid>spring-cloud-starter-ribbon</artifactid> </dependency>
Based on Resttemplate
Defined
PackageCom.jsoft.testzookeeper.client.config;Importorg.springframework.cloud.client.loadbalancer.LoadBalanced;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.web.client.RestTemplate; @Configuration Public classConfig {@Bean @LoadBalanced Publicresttemplate resttemplate () {return Newresttemplate (); }}
Note: Callout @loadbalanced to turn on load balancing, which is the default polling method.
Use
PackageCom.jsoft.testzookeeper.client.service;ImportCom.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;Importorg.springframework.web.client.RestTemplate; @Service Public classHelloService { @Autowired Private resttemplate resttemplate; @HystrixCommand (Fallbackmethod= "Sayhellofallback") Publicstring SayHello (string name) {returnResttemplate.getforentity ("http://service-zookeeper/hello?name=" + Name, String.class). GetBody (); } Privatestring Sayhellofallback (string name) {return"Service Error"; }}
More configuration Reference Official documentation: Https://github.com/Netflix/ribbon/wiki/Features, such as the following rule configuration (not practiced):
springboot-h2. Load balancing strategy for RIBBON.NFLOADBALANCERRULECLASSNAME=COM.NETFLIX.LOADBALANCER.RANDOMRULE//Ribbon
Description: Springboot-h2 for ClientName
Based on feign:
Feign clients do not have configuration items in the integration, mainly based on the configuration file to define the rules, examples of reference code, the following are mainly configuration file configuration.
In addition to the above configuration, you can specify the following:
Mainly based on the zookeeper dependency, the official reference: Http://cloud.spring.io/spring-cloud-zookeeper/multi/multi_ spring-cloud-zookeeper-dependencies.html, the sample configuration is as follows:
Spring.cloud.zookeeper.dependencies. service-zookeeper. required=truespring.cloud.zookeeper.dependencies. service-zookeeper. path=/service-zookeeperspring.cloud.zookeeper.dependencies. service-zookeeper. Loadbalancertype=round_robin
Description: Service-zookeeper is the specified alias and will eventually go to/service-zookeeper,loadbalancertype is the rule.
Summarize:
1, feel based on the ZK load balancing rules and the ribbon of the original rules two inseparable, the main looks as if the granularity is different, the global has been divided into different services and other settings.
2, the above conclusion is to be practiced. There will be a lot of things to be expected.
Maven Example:
Https://github.com/easonjim/spring-cloud-demo/tree/master/ZooKeeper
Reference:
Http://www.ccblog.cn/96.htm (The above internal part is transferred from this article)
Http://www.cnblogs.com/wangjing666/p/6985047.html
http://blog.csdn.net/w_x_z_/article/details/71156009 (load Balancing rule customization)
http://blog.csdn.net/shunhua19881987/article/details/75466797
http://www.idouba.net/netflix-source-ribbon-rule/
http://blog.csdn.net/daybreak1209/article/details/53582366
http://blog.csdn.net/liuchuanhong1/article/details/54728406
http://www.jianshu.com/p/7bb96e2dc944
Http://www.cnblogs.com/wangjing666/p/6994451.html
Spring-cloud-starter-ribbon provides client-side software load balancing algorithms