码迷,mamicode.com
首页 > 其他好文 > 详细

ribbon源码之IPingStrategy

时间:2017-12-06 22:05:42      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:ribbon   writing   ati   whether   sim   执行   res   design   strong   

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

标签:ribbon   writing   ati   whether   sim   执行   res   design   strong   

原文地址:http://www.cnblogs.com/zhangwanhua/p/7994579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!