SpringCloud源码走读--Turbine中忽略延迟高数据的功能

概述

标题这里输入引用文本Turbine为了保证显示的数据实时性,会自动忽略掉延迟高的数据,所以在搭建一些实时服务时一定要保证所有服务器时间基本一致,误差时间应远小于1秒。

具体代码

  • InstanceMonitor

当turbine根据配置从eureka中获取注册服务列表后,会创建InstanceMonitor用于收集应用接口的hystrix统计流数据。以下代码截取循环读流数据的处理部分,主要展示自动过滤延迟数据功能。 如果latency > latencyThreshold.get()则自动忽略,latency为当前时间和收集数据时间之差, latencyThreshold默认为2500毫秒,通过turbine.InstanceMonitor.eventStream.skipLineLogic.latencyThreshold进行配置。


private static DynamicIntProperty latencyThreshold = DynamicPropertyFactory.getInstance().getIntProperty("turbine.InstanceMonitor.eventStream.skipLineLogic.latencyThreshold", 2500);


private DataFromSingleInstance getNextStatsData() throws Exception {

        long skipProcessingUntil = 0;
        ...
        String line = null;
        while ((line = reader.readLine()) != null) {
        ...
        long currentTime = System.currentTimeMillis();
        if (skipLineLogic.get()) {
           if (currentTime < skipProcessingUntil) {
               // continue ... skip processing and just keep reading from the stream
               if (logger.isDebugEnabled()) {
                   logger.debug("Skipping event to catch up to end of feed and reduce latency");
                }
                continue;
            }
        }

        ...

        latency = currentTime - timeOfEvent;

        ...

        if (skipLineLogic.get() && timeOfEvent > 0 && latency > latencyThreshold.get()) {
              // tell the loop to ignore all events for the next X milliseconds so it can just read data from the stream and catch up
              if (logger.isDebugEnabled()) {
                  logger.info("Telling stream reader to wait for 1 second before processing anymore so it can catch up." + latency);
              }
              skipProcessingUntil = System.currentTimeMillis() + skipLogicDelay.get();
              // we also skip pushing this data since it's latent and we would rather quickly catch up and send current data than sending this latent data

              markEventDiscarded();
              continue;
        }

     ...

转载于:https://my.oschina.net/u/2408085/blog/713947

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值