概述
标题这里输入引用文本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;
}
...