Eureka+Ribbon源码解析及负载均衡缓存的优化

问题

熟悉Spring Cloud的同学都知道, 在Zuul内部进行Routing和Load Balancing的时候, 为了保证HA, 不受Eureka掉线的影响, 内存中会有一个Server List缓存. 进行路由和LB的时候并不是每次都实时的去Eureka拉取新的注册信息. 而我们知道有缓存就会有延迟, 会给整个系统的运行带来一些不良影响, 尤为明显的一点:
服务状态变更, 不能及时剔除/增加节点

为了解决服务状态延迟, 通过搜索和文档很容易找到下面两个配置:

ribbon:
  ServerListRefreshInterval: 30000
eureka:
  client:
    registryFetchIntervalSeconds: 30

ribbon和eureka默认缓存刷新频率都是30s, 对系统影响太大,

  1. 经常服务上线半天客户端调用不到, 尤其在测试的时候服务往往只有单节点, 要等20多秒才能调通, 严重影响效率.
  2. 而对于线上多节点环境一样会造成影响, 某一个节点进行升级部署的时候, 虽然服务已经从Eureka下线, 但是Zuul仍然会有30s时间会把请求路由到下线的节点上. 对部分用户造成影响, 当遇到QPS较大的接口时甚至造成没必要的熔断.

对策

为了解决上面两个问题, 一般会把两个配置都改成5s甚至更短, 虽然情况有了很大的缓解, 但是问题依旧.
没办法, 只有深入代码了

代码分析

刚开始一头雾水, 可能想看代码也不知道从哪里开始. 其实找到规律就好了, 代码的入口可以是Zuul的route filter. 也可以是Ribbon的AutoConfiguration. 我从route filter开始.
RibbonRoutingFilter.java

public Object run() {
    RibbonCommandContext commandContext = buildCommandContext(context);
    ClientHttpResponse response = forward(commandContext);
}

很明显, forward是进行请求转发, 继续跟进

RibbonRoutingFilter.java

protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {
    
    RibbonCommand command = this.ribbonCommandFactory.create(context);
    try {
        ClientHttpResponse response = command.execute();
        this.helper.appendDebug(info, response.getRawStatusCode(), response.getHeaders());
        return response;
    }
    catch (HystrixRuntimeException ex) {
        return handleException(info, ex);
    }

}   

这段代码构建了一个RibbonCommand, 实际的请求发送都是通过这个Command来完成的

HttpClientRibbonCommandFactory.java

@Override
public HttpClientRibbonCommand create(final RibbonCommandContext context) {
    ZuulFallbackProvider zuulFallbackProvider = getFallbackProvider(context.getServiceId());
    final String serviceId = context.getServiceId();
    final RibbonLoadBalancingHttpClient client = this.clientFactory.getClient(
            serviceId, RibbonLoadBalancingHttpClient.class);
    client.setLoadBalancer(this.clientFactory.getLoadBalancer(serviceId));

    return new HttpClientRibbonCommand(serviceId, client, context, zuulProperties, zuulFallbackProvider,
            clientFactory.getClientConfig(serviceId));
}

跟到最后会发现, 负载均衡的请求发送都是通过client.setLoadBalancer(this.clientFactory.getLoadBalancer(serviceId));设置的负载均衡器来完成, 这个负载均衡器是从Spring Context中取得的ILoadBalancer类型的Bean
先看一下这个接口的定义
ILoadBalancer.java

public interface ILoadBalancer {
    public void addServers(List<Server> newServers);
    public Server chooseServer(Object key);
    public void markServerDown(Server server);
    public List<Server> getReachableServers();
    public List<Server> getAllServers();
}

再看一下取到的具体实现类:


image.png

是一个ZoneAwareLoadBalancer, 最终挑选服务节点的逻辑:

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

然后发现这个lb.getAllServers()并不是去从Eureka Server拉取注册信息, 使用的是一个内存中的缓存, 所以只要知道这个ServerList的刷新机制就好了.
然后在ZoneAwareLoadBalancer的父类DynamicServerListLoadBalancer中会有一个ServerListUpdater

DynamicServerListLoadBalancer.java

protected final ServerListUpdater.UpdateAction updateAction = new ServerListUpdater.UpdateAction() {
    @Override
    public void doUpdate() {
        updateListOfServers();
    }
};

PollingServerListUpdater.java

public synchronized void start(final UpdateAction updateAction) {
    if (isActive.compareAndSet(false, true)) {
        final Runnable wrapperRunnable = new Runnable() {
            @Override
            public void run() {
                if (!isActive.get()) {
                    if (scheduledFuture != null) {
                        scheduledFuture.cancel(true);
                    }
                    return;
                }
                try {
                    updateAction.doUpdate();
                    lastUpdated = System.currentTimeMillis();
                } catch (Exception e) {
                    logger.warn("Failed one update cycle", e);
                }
            }
        };

        scheduledFuture = getRefreshExecutor().scheduleWithFixedDelay(
                wrapperRunnable,
                initialDelayMs,
                refreshIntervalMs,
                TimeUnit.MILLISECONDS
        );
    } else {
        logger.info("Already active, no-op");
    }
}

定时刷新. 这个refreshIntervalMs就是上面说到的ribbon.ServerListRefreshInterval, 最终发现ServerList是通过DiscoveryClient的InstanceInfos来获取的

DiscoveryClient.java

public List<InstanceInfo> getInstancesByVipAddress(String vipAddress, boolean secure,
                                                   @Nullable String region) {
    applications = this.localRegionApps.get();

    if (!secure) {
        return applications.getInstancesByVirtualHostName(vipAddress);
    } else {
        return applications.getInstancesBySecureVirtualHostName(vipAddress);

    }

}

这个InstanceInfo仍然是一个缓存.. 再找这个缓存的刷新机制

DiscoveryClient.java

private void initScheduledTasks() {
    if (clientConfig.shouldFetchRegistry()) {
        // registry cache refresh timer
        int registryFetchIntervalSeconds = clientConfig.getRegistryFetchIntervalSeconds();
        int expBackOffBound = clientConfig.getCacheRefreshExecutorExponentialBackOffBound();
        scheduler.schedule(
                new TimedSupervisorTask(
                        "cacheRefresh",
                        scheduler,
                        cacheRefreshExecutor,
                        registryFetchIntervalSeconds,
                        TimeUnit.SECONDS,
                        expBackOffBound,
                        new CacheRefreshThread()
                ),
                registryFetchIntervalSeconds, TimeUnit.SECONDS);
    }

}

这里的registryFetchIntervalSeconds对应上面说到的配置项:eureka.client.registryFetchIntervalSeconds, 然后进去CacheRefreshThread才算彻底搞清楚ServerList的缓存刷新机制是怎样的, 真正的从Eureka拉取注册信息就是在这个CacheRefreshThread里, 每registryFetchIntervalSeconds会从Eureka进行一次delta更新

private void getAndUpdateDelta(Applications applications) throws Throwable {
    long currentUpdateGeneration = fetchRegistryGeneration.get();

    Applications delta = null;
    EurekaHttpResponse<Applications> httpResponse = eurekaTransport.queryClient.getDelta(remoteRegionsRef.get());
    if (httpResponse.getStatusCode() == Status.OK.getStatusCode()) {
        delta = httpResponse.getEntity();
    }

    if (delta == null) {
        logger.warn("The server does not allow the delta revision to be applied because it is not safe. "
                + "Hence got the full registry.");
        getAndStoreFullRegistry();
    } else if (fetchRegistryGeneration.compareAndSet(currentUpdateGeneration, currentUpdateGeneration + 1)) {
        logger.debug("Got delta update with apps hashcode {}", delta.getAppsHashCode());
        String reconcileHashCode = "";
        if (fetchRegistryUpdateLock.tryLock()) {
            try {
                updateDelta(delta);
                reconcileHashCode = getReconcileHashCode(applications);
            } finally {
                fetchRegistryUpdateLock.unlock();
            }
        } else {
            logger.warn("Cannot acquire update lock, aborting getAndUpdateDelta");
        }
        // There is a diff in number of instances for some reason
        if (!reconcileHashCode.equals(delta.getAppsHashCode()) || clientConfig.shouldLogDeltaDiff()) {
            reconcileAndLogDifference(delta, reconcileHashCode);  // this makes a remoteCall
        }
    } else {
        logger.warn("Not updating application delta as another thread is updating it already");
        logger.debug("Ignoring delta update with apps hashcode {}, as another thread is updating it already", delta.getAppsHashCode());
    }
}

现在缓存更新策略已经研究透彻, 如何在服务状态变化的时候实时触发Server List的更新?

终极对策

目前的想法是配合Bus, 服务上下线的时候通过Bus来触发Zuul的refresh, 但是Server List的更新schedule并不会更新, 考虑自己实现一套可随Context refresh的DiscoveryClient

To Be Continued...

续:

自定义DiscoveryClient比较复杂.
通过代码分析可以知道只需要能调到DiscoveryClient的refreshRegistry就可以实时刷新了, 但它是个private方法, 那就反射嘛, 配合Bus的refresh功能同时刷新注册信息

@Component
@Slf4j
public class RefreshListener implements ApplicationListener<RefreshScopeRefreshedEvent> {
    @Override
    public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
        try {
            Method method = DiscoveryClient.class.getDeclaredMethod("refreshRegistry");
            method.setAccessible(true);
            method.invoke(SpringUtils.getBean(DiscoveryClient.class));
        } catch (Exception e) {
            log.error("Failed to refreshRegistry.", e);
        }
    }
}

完.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,757评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,478评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,540评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,593评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,903评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,329评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,659评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,383评论 0 195
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,055评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,337评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,864评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,227评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,820评论 3 231
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,999评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,750评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,365评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,260评论 2 258

推荐阅读更多精彩内容