流量数据统计
上面我说了各种流控规则如何进行限流,那这个流量数据是如何统计出来的呢?这里我们回到StatisticNode
//每秒统计数据
private transient volatile Metric rollingCounterInSecond = new ArrayMetric(SampleCountProperty.SAMPLE_COUNT,
IntervalProperty.INTERVAL);
/**
* Holds statistics of the recent 60 seconds. The windowLengthInMs is deliberately set to 1000 milliseconds,
* meaning each bucket per second, in this way we can get accurate statistics of each second.
*/
//每分钟统计数据
private transient Metric rollingCounterInMinute = new ArrayMetric(60, 60 * 1000, false);
/**
* The counter for thread count.
*/
//线程总数
private AtomicInteger curThreadNum = new AtomicInteger(0);
我们可以看到统计节点主要是保存三种实时统计指标,每秒统计信息、每分钟的统计信息、并发线程数
Metric接口定义了获取各种统计信息的方法,如
long success();
long block();
long exception();
long pass();
long rt();
......
ArrayMetric底层又是通过LeapArray来完成数据的统计,其中几个重要的属性
protected int windowLengthInMs;//每个窗口时间长度
protected int sampleCount;//时间窗口个数
protected int intervalInMs;//总的间隔时间
protected final AtomicReferenceArray<WindowWrap<T>> array;//存放时间窗口的集合
public LeapArray(int sampleCount, int intervalInMs) {
//每个窗口时间长度
this.windowLengthInMs = intervalInMs / sampleCount;
//总的间隔时间
this.intervalInMs = intervalInMs;
//窗口个数
this.sampleCount

本文详细剖析了统计节点的实现,涉及实时数据统计、时间窗口机制和并发控制,展示了如何通过Metric和LeapArray高效收集流量数据。
最低0.47元/天 解锁文章
1559

被折叠的 条评论
为什么被折叠?



