What is the Ribbon?
The Ribbon is an open source project released by Netflix. Its main function is to provide a client-side software load balancing algorithm that connects Netflix’s middle-tier services together. The Ribbon client component provides a comprehensive set of configuration options such as connection timeout and retry. Simply put, the Ribbon lists all the machines behind the Load Balancer in the configuration file. The Ribbon automatically helps you connect to these machines based on certain rules (e.g. simple polling, random linking, etc.). It’s also easy to implement custom load balancing algorithms using the Ribbon.
Two :LB scheme classification
At present, the mainstream LB schemes can be divided into two types: one is centralized LB, which uses an independent LB facility (either hardware, such as F5, or software, such as Nginx) between the consumer and provider of the service, and the facility is responsible for forwarding the access request to the provider of the service through a certain policy. The other is in-process LB, which integrates THE LB logic into the consumer, who learns from the service registry what addresses are available and then selects a suitable server from those addresses. The Ribbon is the latter. It is simply a library that integrates with consumer processes to retrieve service provider addresses.
3. Main components and workflow of the Ribbon
The core components of the Ribbon (interface types) are:
ServerList
Used to get the address list. It can be static (providing a fixed set of addresses) or dynamic (periodically querying the address list from the registry).
ServerListFilter
This parameter is used only when dynamic ServerList is used. It is used to filter some addresses in the original service list using a certain policy.
IRule
Select a final service address as the LB result. Selection strategies include polling, weighting by response time, circuit breakers (when Hystrix is available), and so on.
The Ribbon uses ServerList to get a list of all available services. The Ribbon uses ServerListFilter to filter some addresses, and IRule to select a server from the remaining addresses.
4. Introduces the main load balancing policies provided by the Ribbon
1: Simple Polling load Balancing (RoundRobin)
The requests are scheduled to different servers in turn in a polling manner, that is, I = (I + 1) mod n is executed each time and the i-th server is selected.
2: Random Load Balancing
Select the Server in UP state randomly
3: WeightedResponseTime load balancing
Assign a weight according to the corresponding time, the longer the corresponding time, the smaller the weight, the lower the probability of being selected.
4: Zone Aware Polling load Balancing
Select a server to judge the performance and availability of the server
The Ribbon compares load balancing policies
Policy name | Policy statement | Strategy description | Implementation notes |
BestAvailableRule | public class BestAvailableRule extends ClientConfigEnabledRoundRobinRule | Select a server with minimum concurrent requests | Inspect servers one by one, skip tripped, and select the Server with the smallest activity equestscount |
AvailabilityFilteringRule | public class AvailabilityFilteringRule extends PredicateBasedRule | Filter out back-end servers marked with circuit tripped because they kept connecting, and filter out those with high concurrency (active connections tripped above configured threshold) | Use an AvailabilityPredicate to include the logic for filtering servers, which checks the status of each server recorded in 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 probability of being selected. | A background thread periodically reads the response time from status, calculating a weight for each server. It’s also a little bit easier to calculate the Weight of the responseTime minus the average responsetime of each server is the Weight of the server. When no StatAs is formed, the Roubine policy is used to select the server. |
RetryRule | public class RetryRule extends AbstractLoadBalancerRule | Retry mechanism for the selected load balancing policy. | If the server selection fails during a configuration period, the system tries to use subRule to select an available server |
RoundRobinRule | public class RoundRobinRule extends AbstractLoadBalancerRule | RoundRobin Round selection of the server | Poll index and select the server corresponding to index |
RandomRule | public class RandomRule extends AbstractLoadBalancerRule | Select a server at random | Randomly select the server corresponding to index |
ZoneAvoidanceRule | public class ZoneAvoidanceRule extends PredicateBasedRule | Select a server to judge the performance and availability of the server | Make use of zone avoidancePredicate and AvailabilityPredicate to determine whether a server is selected. The former predicate determines whether the running performance of a zone is available and excludes the unavailable zone. AvailabilityPredicate is used to filter out servers that have too many connections. |
5. Ribbon is 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 > < the dependency > < groupId > com.net flix. Ribbon < / groupId > The < artifactId > ribbon - httpclient < / artifactId > < version > 2.2.0 < / version > < / dependency > < / dependencies >Copy the code
The overall structure is as follows:
The registry
Complete project source code
Spring Cloud large enterprise distributed micro service Cloud to build B2B2C e-commerce platform source code please add penguin beg: 10387746626