A: 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.
II: 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 components of the Ribbon, all of which are 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: Stochastic load Balancing (random)
Randomly select server with a status of up
3: Weighted response time Load balancer (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 balancer (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: Ribbon used alone
Create a MAVEN project name Ribbon_client
Pom Content
<dependencies><dependency><groupId>com.netflix.ribbon</groupId><artifactId> ribbon-core</artifactid><version>2.2.0</version></dependency><dependency>< Groupid>com.netflix.ribbon</groupid><artifactid>ribbon-httpclient</artifactid><version >2.2.0</version></dependency></dependencies>
Sample-client.properties configuration file
# max number of retries sample-client.ribbon.maxautoretries=1 # max number of next servers to retry (Excluding the first server) sample-client.ribbon.maxautoretriesnextserver=1 # whether all operations can be retried for this client Sample-client.ribbon.oktoretryonalloperations=true # interval to refresh the server list from the source Sample-client.ribbon.serverlistrefreshinterval=2000 # connect timeout used by Apache HttpClient sample-client.ribbon.ConnectTimeout=3000 # Read timeout used by apache httpclient sample-client.ribbon.readtimeout=3000 # initial list of servers, can be changed vIa archaius dynamic property at runtime sample-client.ribbon.listofservers =www.sohu.com:80,www.163.com:80,www.sina.com.cn:80 sample-client.ribbon.enableprimeconnections=true
Ribbonmain Code
import java.net.uri;import com.netflix.client.clientfactory;import com.netflix.client.http.httprequest;import com.netflix.client.http.httpresponse;import Com.netflix.config.configurationmanager;import com.netflix.loadbalancer.zoneawareloadbalancer;import com.netflix.niws.client.http.RestClient;public class RibbonMain { Public static void main ( String[] args ) throws Exception { configurationmanager.loadpropertiesfromresources (" Sample-client.properties "); system.out.println ( Configurationmanager.getconfiginstance (). GetProperty ("Sample-client.ribbon.listofservers")); restclient client = (restclient) Clientfactory.getnamedclienT ("Sample-client"); httprequest request = httprequest.newbuilder (). Uri (New uri ("/")). Build (); for (int i = 0; i < 4; i ++) { httpresponse response = client.executewithloadbalancer (Request); system.out.println ("Status for uri: " + response.getrequesteduri () + " &NBSP;IS&NBSP;: " + Response.getstatus ()); } zoneawareloadbalancer lb = (ZoneawarelOadbalancer) client.getloadbalancer (); System.out.println (Lb.getloadbalancerstats ()); configurationmanager.getconfiginstance (). SetProperty ("Sample-client.ribbon.listofservers", "ccblog.cn:80,www.linkedin.com:80"); System.out.println ("CHANGING&NBSP;SERVERS&NBSP; ..."); thread.sleep (; ) for (int i = 0; i < 3; i ++) { HttpResponse Response = client.executewithloadbalanCER (Request); System.out.println ("Status for uri:" + response.getrequesteduri () + " is : " + response.getstatus ()); } system.out.println (Lb.getloadbalancerstats ()); } }
Code parsing
Load properties using Archaius ConfigurationManager;
Create a client and load balancer using Clientfactory;
Use Builder to build HTTP requests. Note that we only support the path of the "/" section of the URI, and once the server is selected by the load balancer, the complete URI is computed by the client;
Call API Client.executewithloadbalancer (), not exeucte () API;
Dynamic remediation of server pools in configuration;
Wait for the server list to refresh (the refresh interval defined in the configuration file is 3 seconds);
Print out server statistics for the load Balancer record.
Six: Ribbon combined with Eureka use
Start Eureka_register_service Engineering (Registration Center) and biz-service-0 Project (service producer) first
Create MAVEN Engineering Eureka_ribbon_client The project starts and related configurations depend on Eureka_register_service and biz-service-0
Pom Join
<parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-parent</artifactid> <version >1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --></parent><dependencies> < dependency> <groupid>org.springframework.cloud</ groupid> <artifactid>spring-cloud-starter-ribbon</ artifactid> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependencY> <dependency> <groupid> Org.springframework.boot</groupid> <artifactid> spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</ groupid> <artifactid>spring-boot-starter-test</ artifactid> <scope>test</scope> </dependency></dependencies><dependencyManagement> < dependencies> <dependency> < Groupid>org.springframework.cloud</groupid> <artifactid> Spring-cloud-dependencies</artifactid&gT; <version>brixton.release</version> <type> pom</type> <scope>import</scope></dependency> </dependencies></dependencyManagement>
In the application main class, the Discovery service capability is added through @enablediscoveryclient annotations. Create a Resttemplate instance and turn on the load balancing capability with @loadbalanced annotations.
@SpringBootApplication @enablediscoveryclientpublic class Ribbonapplication {@Bean @loadbalancedresttemplate Resttemplate () {return new resttemplate ();} public static void Main (string[] args) {Springapplication.run (ribbonapplication.class, args);}}
Create Consumercontroller to consume Biz-service-0 's GetUser service. Invoking a service by direct resttemplate
@RestControllerpublic class Consumercontroller {@Autowired resttemplate resttemplate; @RequestMapping (value = "/getuserinfo", method = Requestmethod.get) public String Add () {return resttemplate.ge Tforentity ("Http://biz-service-0/getuser", String.class). GetBody (); }}
Configuring the Eureka Service Registry in Application.properties
Spring.application.name=ribbon-consumer
server.port=8003
eureka.client.serviceurl.defaultzone=http://localhost:8000/eureka/
When you are finished, you can open http://localhost:8003/getuserinfo to see the results
Summary: The ribbon is actually a soft-load-balanced client component that can be used in conjunction with other client-requested clients, and Eureka is just one example.
Code Address: Https://github.com/zhp8341/SpringCloudDemo
"Micro-Service Architecture" Springcloud Ribbon (iv)