首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ribbon的AvailabilityFilteringRule

Ribbon的AvailabilityFilteringRule

作者头像
克虏伯
发布2019-10-08 10:45:43
8250
发布2019-10-08 10:45:43
举报

    Ribbon的版本是2.3.0.release.

                                                                             图1

    AvailabilityFilteringRule继承了PredicateBasedRule,这是因为使用到了AbstractServerPredicate.

    choose方法如下List-1, 轮循选一个,判读是否满足条件,如果满足则返回,超过10次,则调用父类的choose方法选择.

List-1

public Server choose(Object key) {
    int count = 0;

    for(Server server = this.roundRobinRule.choose(key); count++ <= 10; server = this.roundRobinRule.choose(key)) {
        if (this.predicate.apply(new PredicateKey(server))) {
            return server;
        }
    }

    return super.choose(key);
}

    来看AvailabilityPredicate,如下List-2, apply方法返回true,需要满足俩个条件,断路器闭合,调用服务的并发请求数小于限制数

List-2

public boolean apply(@Nullable PredicateKey input) {
    LoadBalancerStats stats = this.getLBStats();
    if (stats == null) {
        return true;
    } else {
        return !this.shouldSkipServer(stats.getSingleServerStat(input.getServer()));
    }
}

private boolean shouldSkipServer(ServerStats stats) {
    return CIRCUIT_BREAKER_FILTERING.get() && stats.isCircuitBreakerTripped() || stats.getActiveRequestsCount() >= (Integer)this.activeConnectionsLimit.get();
}

    List-1中10次之后还不满足,则调用父类的choose方法,来看下PredicateBasedRule的choose实现,如下List-3

List-3

public Server choose(Object key) {
    ILoadBalancer lb = this.getLoadBalancer();
    Optional<Server> server = this.getPredicate().chooseRoundRobinAfterFiltering(lb.getAllServers(), key);
    return server.isPresent() ? (Server)server.get() : null;
}

    chooseRoundRobinAfterFiltering中是如何实现的呢,如下List-4,

  1. 获取所有的服务实例
  2. 遍历服务列表,过滤掉不满足条件的
  3. 在满足条件的服务列表中,再进行RoundRibbon算法,选出服务

List-4

public Optional<Server> chooseRoundRobinAfterFiltering(List<Server> servers, Object loadBalancerKey) {
    List<Server> eligible = this.getEligibleServers(servers, loadBalancerKey);
    return eligible.size() == 0 ? Optional.absent() : Optional.of(eligible.get(this.incrementAndGetModulo(eligible.size())));
}

public List<Server> getEligibleServers(List<Server> servers, Object loadBalancerKey) {
    if (loadBalancerKey == null) {
        return ImmutableList.copyOf(Iterables.filter(servers, this.getServerOnlyPredicate()));
    } else {
        List<Server> results = Lists.newArrayList();
        Iterator var4 = servers.iterator();

        while(var4.hasNext()) {
            Server server = (Server)var4.next();
            if (this.apply(new PredicateKey(loadBalancerKey, server))) {
                results.add(server);
            }
        }
        return results;
    }
}

    AvailabilityFilteringRule在RoundRibbon的基础上,选择满足条件的服务,如果10次了还没得到,则在满足条件的服务列表中,再用RoundRibbon算法选择.

Reference

  1. https://github.com/Netflix/ribbon/tree/v2.3.0
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reference
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档