ribbon源码之IPingStrategy
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ribbon源码之IPingStrategy相关的知识,希望对你有一定的参考价值。
IPingStrategy
IPingStrategy用来探测注册在BaseLoadBalancer中的server的存活情况。
public interface IPingStrategy { boolean[] pingServers(IPing ping, Server[] servers); }
BaseLoadBalancer实现了一个默认的序列化执行的实现类,我们也可以实现自己的并发策略。
private static class SerialPingStrategy implements IPingStrategy { @Override public boolean[] pingServers(IPing ping, Server[] servers) { int numCandidates = servers.length; boolean[] results = new boolean[numCandidates];for (int i = 0; i < numCandidates; i++) { results[i] = false; /* Default answer is DEAD. */ try { // NOTE: IFF we were doing a real ping // assuming we had a large set of servers (say 15) // the logic below will run them serially // hence taking 15 times the amount of time it takes // to ping each server // A better method would be to put this in an executor // pool // But, at the time of this writing, we dont REALLY // use a Real Ping (its mostly in memory eureka call) // hence we can afford to simplify this design and run // this // serially if (ping != null) { results[i] = ping.isAlive(servers[i]); } } catch (Exception e) { logger.error("Exception while pinging Server: ‘{}‘", servers[i], e); } } return results; } }
IPing
ping服务器的实现接口,定义ping的接口
public interface IPing { /** * Checks whether the given <code>Server</code> is "alive" i.e. should be * considered a candidate while loadbalancing * */ public boolean isAlive(Server server); }
在构造BaseLoadBalancer需要用户实现IPing
public BaseLoadBalancer(IClientConfig config, IRule rule, IPing ping) { initWithConfig(config, rule, ping); }
以上是关于ribbon源码之IPingStrategy的主要内容,如果未能解决你的问题,请参考以下文章