In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use LoadBalancer to achieve load balancing in Ribbon. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Ribbon load balancer
Ribbon's load balancer is implemented through LoadBalancerClient. When the application is started, LoadBalancerClient acquires the service list from EurekaClient by default, and caches the service registration list locally. When calling the choose () method of LoadBalancerClient, select an available service according to the load balancing policy IRule, so as to achieve load balancing.
Of course, LoadBalancerClient does not need to obtain the service list from EurekaClient. You need to maintain a service registration list information by yourself, as shown below:
Ribbon: eureka: enabled: false stores: ribbon: listOfServers: baidu.com, google.comRibbon load balancer flow chart
Main process:
1. When the application starts, ILoadBalancer gets the list of services from EurekaClient
two。 Then a heartbeat test is sent to EurekaClient every 10 seconds, and if the registration list changes, it will be updated and retrieved.
3. When LoadBalancerClient calls the choose () method to select a service, it will call chooseServer () of ILoadBalancer to get a possible service.
4. When ILoadBalancer acquires services, it will choose according to the load balancing policy IRule.
5. Return to available services
Implementation principle of Ribbon load balancer
Let's take a look at the implementation principle of each class
RibbonLoadBalancerClient
RibbonLoadBalancerClient is an important class in the implementation of Ribbon load balancer. It executes the final request processing of load balancer. Let's take a look at its class diagram:
It implements the LoadBalancerClient interface while the LoadBalancerClient interface implements the ServiceInstanceChooser interface:
ServiceInstanceChooser
This interface is used to get an available service from the load balancer, and there is only one way:
Public interface ServiceInstanceChooser {/ * * @ param serviceId: service ID * @ return available service instance * / ServiceInstance choose (String serviceId);} LoadBalancerClient
Indicates that the client of load balancer is an interface that inherits the ServiceInstanceChooser interface. There are three methods:
Public interface LoadBalancerClient extends ServiceInstanceChooser {/ * execute request * @ param serviceId: service ID * @ param request for finding LoadBalancer: allow the implementation of pre-and post-execution operations * / T execute (String serviceId, LoadBalancerRequest request) throws IOException / * * execute request * @ param serviceId: service used to find LoadBalancer ID * @ param serviceInstance: service to execute request * @ param request: allow operation before and after execution * / T execute (String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest request) throws IOException / * create a correct URI with a real host and port. Some systems use URL with a logical service name as the host. Calling this method will use host:port to replace the logical service name * @ param instance: service instance used to rebuild URI * @ param original: URL * @ return A reconstructed URI with logical service name. * / URI reconstructURI (ServiceInstance instance, URI original);} RibbonLoadBalancerClient is implemented as follows:
Mainly take a look at the interface methods implemented from ServiceInstanceChooser,LoadBalancerClient
Public class RibbonLoadBalancerClient implements LoadBalancerClient {/ / Factory: mainly used to create clients, create load balancers, configure clients, etc. / / create a Spring ApplicationContext for each client name, from which you can get the required bean private SpringClientFactory clientFactory; protected ILoadBalancer getLoadBalancer (String serviceId) {return this.clientFactory.getLoadBalancer (serviceId) }.} public class SpringClientFactory extends NamedContextFactory {/ / get client public
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.