In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the load balancing strategy and principle of Ribbon". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Load Balance load balancing is an algorithm used to solve the problem that one machine (one process) cannot solve all requests. For example, nginx can use load balancer to distribute traffic, ribbon provides load balancer for clients, load balancer in dubbo service calls, and so on, load balancer is used in many places.
The benefits of using load balancing are obvious:
When one or more servers in the cluster down, the remaining servers without down can guarantee the continued use of the service.
The use of more machines ensures the benign use of the machine, and the system cpu will not rise sharply due to a certain peak hour.
There are several implementation strategies for load balancer. The common ones are:
Random (Random)
Polling (RoundRobin)
Consistent hash (ConsistentHash)
Hash (Hash)
Weighted (Weighted)
ILoadBalance load balancer
Ribbon is a service that provides load balancing for clients. It provides an internal API called ILoadBalance to represent the operations of load balancers, such as adding server operations, selecting server operations, getting all server lists, obtaining available server lists, and so on. The inheritance relationship of ILoadBalance is as follows:
The load balancer obtains the service information from EurekaClient (the implementation class of EurekaClient is DiscoveryClient), routes it according to IRule, and judges the availability of the service according to IPing.
How often does the load balancer get registration information from Eureka Client? Under the BaseLoadBalancer class, the constructor of BaseLoadBalancer, which opens a PingTask task setupPingTask ();, the code is as follows:
Public BaseLoadBalancer (String name, IRule rule, LoadBalancerStats stats, IPing ping, IPingStrategy pingStrategy) {if (logger.isDebugEnabled ()) {logger.debug ("LoadBalancer: initialized");} this.name = name; this.ping = ping; this.pingStrategy = pingStrategy; setRule (rule); setupPingTask (); lbStats = stats; init ();}
The specific code logic of setupPingTask (), which turns on the ShutdownEnabledTimer execution PingTask task, which by default has a pingIntervalSeconds of 10, that is, sends a "ping" to EurekaClient every 10 seconds.
Void setupPingTask () {if (canSkipPing ()) {return;} if (lbTimer! = null) {lbTimer.cancel ();} lbTimer = new ShutdownEnabledTimer ("NFLoadBalancer-PingTimer-" >
The PingTask source code, that is, new a Pinger object and execute the runPinger () method.
Check the runPinger () method of Pinger, and finally obtain the availability of the service according to pingerStrategy.pingServers (ping, allServers). If the result is the same as before, do not get the registration list from EurekaClient. If it is different, notify ServerStatusChangeListener or changeListeners that it has changed, update it or pull it again.
The complete process is:
LoadBalancerClient (RibbonLoadBalancerClient is the implementation class) when initializing (execute method), it will obtain the service registration list from the Eureka registry through ILoadBalance (BaseLoadBalancer is the implementation class), and send "ping" to EurekaClient every 10s to determine the availability of the service. If the availability of the service has changed or the number of services is inconsistent with the previous one, it will be updated or re-pulled from the registry. With these service registration lists, LoadBalancerClient can load balance according to the specific IRule.
IRule routing
The IRule API represents the load balancing policy:
Public interface IRule {public Server choose (Object key); public void setLoadBalancer (ILoadBalancer lb); public ILoadBalancer getLoadBalancer ();}
The implementation classes of the IRule interface are as follows:
Where RandomRule stands for random policy, RoundRobinRule for polling policy, WeightedResponseTimeRule for weighted policy, BestAvailableRule for minimum number of requests policy, and so on.
The random strategy is very simple, which is to randomly select a server from the server. The implementation code of RandomRule is as follows:
Public Server choose (ILoadBalancer lb, Object key) {if (lb = = null) {return null;} Server server = null; while (server = = null) {if (Thread.interrupted ()) {return null;} List upList = lb.getReachableServers (); List allList = lb.getAllServers (); int serverCount = allList.size () If (serverCount = = 0) {return null;} int index = rand.nextInt (serverCount); / / use the Random class inside jdk to randomly get the index value index server = upList.get (index); / / get the server instance if (server = = null) {Thread.yield (); continue } if (server.isAlive ()) {return (server);} server = null; Thread.yield ();} return server;}
RoundRobinRule polling policy indicates that one server is fetched each time. For example, there are five servers in total, the first server for the first time, the second server for the second time, the third server for the third time, and so on:
Public Server choose (ILoadBalancer lb, Object key) {if (lb = = null) {log.warn ("no load balancer"); return null;} Server server = null; int count = 0; while (server = = null & & count++
< 10) { List reachableServers = lb.getReachableServers(); List allServers = lb.getAllServers(); int upCount = reachableServers.size(); int serverCount = allServers.size(); if ((upCount == 0) || (serverCount == 0)) { log.warn("No up servers available from load balancer: ">WeightedResponseTimeRule inherits RoundRobinRule and does not have the weight list at the beginning. It uses the parent polling method. There is a scheduled task that updates the weight list every 30 seconds by default. The timing task updates the weight list according to the response time of the instance. What the choose method does is to multiply a random double number (0 double 1) by the maximum weight to get randomWeight, and then iterate through the weight list to find the first instance subscript larger than randomWeight. Then return to the instance with an abbreviated code.
The BestAvailableRule policy is used to select the server with the least concurrent requests:
Public Server choose (Object key) {if (loadBalancerStats = = null) {return super.choose (key);} List serverList = getLoadBalancer (). GetAllServers (); / / get a list of all servers int minimalConcurrentConnections = Integer.MAX_VALUE; long currentTime = System.currentTimeMillis (); Server chosen = null; for (Server server: serverList) {/ / traverse each server ServerStats serverStats = loadBalancerStats.getSingleServerStat (server) / / get the status of each server if (! serverStats.isCircuitBreakerTripped (currentTime)) {/ / continue to execute int concurrentConnections = serverStats.getActiveRequestsCount (currentTime) if the circuit breaker is not triggered / / get the number of requests from the current server if (concurrentConnections < minimalConcurrentConnections) {/ / compare the number of requests between servers, then select the server with the least number of requests and put them in the chosen variable minimalConcurrentConnections = concurrentConnections; chosen = server } if (chosen = = null) {/ / if not selected, call the choose method of the parent class ClientConfigEnabledRoundRobinRule, that is, load balancing return super.choose (key) using RoundRobinRule polling;} else {return chosen;}}
Using the load balancing strategy provided by Ribbon is very simple, and you only need the following:
1. Create a RestTemplate instance with load balancing function
@ Bean@LoadBalancedRestTemplate restTemplate () {return new RestTemplate ();}
When you use RestTemplate for rest operation, you will automatically use the load balancer policy, and it will add LoadBalancerInterceptor as an interceptor to the RestTemplate. The purpose of this interceptor is to use load balancing.
The polling policy is used by default, and if you want to use other policies, specify the IRule implementation, such as:
@ Beanpublic IRule ribbonRule () {return new BestAvailableRule ();}
This approach also works for Feign.
We can also refer to ribbon and write a load balancing implementation class ourselves.
You can obtain which service instance is finally selected by the load balancing policy by using the following methods:
@ Autowired LoadBalancerClient loadBalancerClient; / / Test which instance public String getChoosedService () {ServiceInstance serviceInstance = loadBalancerClient.choose ("USERINFO-SERVICE"); StringBuilder sb = new StringBuilder (); sb.append ("host:") .append (serviceInstance.getHost ()) .append (","); sb.append ("port:") .append (serviceInstance.getPort ()) .append (",") Sb.append ("uri:") .append (serviceInstance.getUri ()); return sb.toString ();} "what is the load balancing strategy and principle of Ribbon"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.