Home  >  Article  >  Java  >  Analysis of spring-cloud-ribbon principle: several core classes of ribbon

Analysis of spring-cloud-ribbon principle: several core classes of ribbon

php是最好的语言
php是最好的语言Original
2018-08-02 14:35:252257browse

Ribbon is a client load balancer that provides great control over the behavior of HTTP and TCP clients. Feign has used Ribbon by default (reference article)

1. Let’s take a look at several core classes of ribbon

1, IClientConfig Default implementation classDefaultClientConfigImpl , mainly used to configure the relevant attribute configuration of the ribbon client

2, ServerListUpdaterDefault implementation class PollingServerListUpdater, mainly responsible for dynamically updating the server list

  • After the start method is called, a scheduled task will be started, with a delay of 1s to start execution, and it will be executed periodically every 30s time interval

    • Periodic time interval You can set the

      start method by
    • ribbon.ServerListRefreshInterval=1000
    or
  • ribbonClientName.ribbon.ServerListRefreshInterval=1000

    . ##DynamicServerListLoadBalancerExecute the call during initialization

3,

ServerListGet the server list

  • Default implementation class

    ConfigurationBasedServerList, the default is to get the server list from the configuration file, configure it like this [ribbonClinetName].ribbon.listOfServers=xxx,xxx

  • ConsulServerListIntroduces the implementation class of consul for service discovery, which is mainly responsible for obtaining the server list of the registration center

    Analysis of spring-cloud-ribbon principle: several core classes of ribbon

    Remarks: You can extend your own# through configuration ##ServerList

    Implementation, like this: [ribbonClient].ribbon.NIWSServerListClassName=Class name

    ##4、
  • ServerListFilter
Server list Filter

Default implementation class
    ZonePreferenceServerListFilter
  • Mainly filters the server list based on partitions

  • HealthServiceServerListFilter
  • Introduce the implementation class of consul service discovery, which is mainly responsible for filtering the list of servers that pass the consul health check (all server lists will be obtained in ConsulServerList, including servers that fail the health check)

    Analysis of spring-cloud-ribbon principle: several core classes of ribbonNote: You can extend your own
    ServerList

    implementation through configuration, like this

    [ribbonClient].ribbon.NIWSServerListFilterClassName=Class name

    5,
  • IPing
Check whether the server or

default implementation
    DummyPing
  • , this is a false detection, always returns true

  • ConsulPing
  • , introduces the implementation class of consul service discovery, mainly based on the checks parameter returned by consul to determine whether the server is alive, and

    HealthServiceServerListFilter The filtering judgment is the same

    Analysis of spring-cloud-ribbon principle: several core classes of ribbonNote: You can extend your own
    ServerList

    implementation through configuration, like this:

    [ribbonClient].ribbon .NFLoadBalancerPingClassName=Class name

    6、
  • IRule
Load balancing selector

Default implementation
    ZoneAvoidanceRule
  • , which combines the performance of the zone where the server is located and the availability of the server to select server

  • RandomRule
  • : randomly select a server

  • RoundRobinRule
  • : RoundRobin mode polls the selected server

  • RetryRule
  • : On-machine retry mechanism for the selected load balancing policy.

  • WeightedResponseTimeRule
  • : Assign a weight based on the response time. The longer the response time, the smaller the weight, and the lower the possibility of being selected.

  • AvailabilityFilteringRule
  • : Filter out those back-end servers that are marked as circuit tripped because of persistent connection failures, and filter out those back-end servers with high concurrency ( active connections exceeds the configured threshold)

  • BestAvailableRule
  • : Select a server with the smallest concurrent requests

7, Analysis of spring-cloud-ribbon principle: several core classes of ribbonILoadBalancer

Load balancing master controller, default implementation class

ZoneAwareLoadBalancer, which starts the entire load balancing client

You can extend your own
    ServerList
  • implementation through configuration, like this:

    [ribbonClient].ribbon.NFLoadBalancerClassName=Class name

    Analysis of spring-cloud-ribbon principle: several core classes of ribbon Load balancer startup sequence diagram:

  • During initialization, a scheduled task will be initialized first, which will be executed every 30 seconds. The cache will store all the instance lists allServerList obtained from the registration center and the instance list upServerList that have been successfully pinged. However, during load balancing The list I got is allServerList instead of upServerList. I don’t understand the meaning of this ping

  • Start a scheduled task to pull the service list from the registration center regularly and execute it every 30 seconds

  • Initialize to obtain the service list. The pulled service list is filtered by ServiceFilter and stored in the cache.

When the client initiates a call, it will Call the chooseServer method of ILoadBalancer to select an instance and return it to the caller according to the load balancing algorithm of IRule.

Related articles:

WPF 4 Ribbon Development Quick Access Toolbar )

SharePoint 2013 Hide the page’s Ribbon, menu and other non-content elements

The above is the detailed content of Analysis of spring-cloud-ribbon principle: several core classes of ribbon. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn