带你读《Apache Dubbo微服务开发从入门到精通》——二、 微服务集群监控(4)

本文涉及的产品
Serverless 应用引擎 SAE,800核*时 1600GiB*时
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
简介: 带你读《Apache Dubbo微服务开发从入门到精通》——二、 微服务集群监控(4)

《Apache Dubbo微服务开发从入门到精通》——可视化监测服务状态——二、 微服务集群监控(3) https://developer.aliyun.com/article/1224284



聚合收集器


public class AggregateMetricsCollector implements MetricsCollector, MetricsListener {
    private int bucketNum;
    private int timeWindowSeconds;
    private final Map<MethodMetric, TimeWindowCounter> totalRequests = new ConcurrentHashMap<>();
    private final Map<MethodMetric, TimeWindowCounter> succeedRequests = new ConcurrentHashMap<>();
    private final Map<MethodMetric, TimeWindowCounter> failedRequests = new ConcurrentHashMap<>();
    private final Map<MethodMetric, TimeWindowCounter> qps = new ConcurrentHashMap<>();
    private final Map<MethodMetric, TimeWindowQuantile> rt = new ConcurrentHashMap<>();
    private final ApplicationModel applicationModel;
    private static final Integer DEFAULT_COMPRESSION = 100;
    private static final Integer DEFAULT_BUCKET_NUM = 10;
    private static final Integer DEFAULT_TIME_WINDOW_SECONDS = 120;
//在构造函数中解析配置信息
    public AggregateMetricsCollector(ApplicationModel applicationModel) {
        this.applicationModel = applicationModel;
        ConfigManager configManager = applicationModel.getApplicationConfigManager();
        MetricsConfig config = configManager.getMetrics().orElse(null);
        if (config != null && config.getAggregation() != null && Boolean.TRUE.equals(config.getAggregation().getEnabled())) {
            // only registered when aggregation is enabled.
            registerListener();
            AggregationConfig aggregation = config.getAggregation();
            this.bucketNum = aggregation.getBucketNum() == null ? DEFAULT_BUCKET_NUM : aggregation.getBucketNum();
            this.timeWindowSeconds = aggregation.getTimeWindowSeconds() == null ? DEFAULT_TIME_WINDOW_SECONDS : aggregation.getTimeWindowSeconds();
        }
    }
}

 


如果开启了本地聚合,则通过spring的BeanFactory添加监听,将AggregateMetricsCollector与DefaultMetricsCollector绑定,实现一种生存者消费者的模式,DefaultMetricsCollector中使用监听器列表,方便扩展

 

image.png

 

c) 指标聚合

 

滑动窗口

 

假设我们初始有6个bucket,每个窗口时间设置为2分钟,每次写入指标数据时,会将数据分别写入6个bucket内,每隔两分钟移动一个bucket并且清除原来bucket内的数据读取指标时,读取当前current指向的bucket,以达到滑动窗口的效果

 

具体如下图所示,实现了当前 bucket 内存储了配置中设置的bucket生命周期内的数据,即近期数据

 

image.png

 

在每个bucket内,使用TDigest算法计算分位数指标

 

注:

TDigest算法(极端分位精确度高,如p1 p99,中间分位精确度低,如p50),相关资料如下

 

https://op8867555.github.io/posts/2018-04-09-tdigest.html

https://blog.csdn.net/csdnnews/article/details/116246540

开源实现:https://github.com/tdunning/t-digest

 

代码实现如下,除了TimeWindowQuantile用来计算分位数指标外,另外提供了TimeWindowCounter来收集时间区间内的指标数量


public class TimeWindowQuantile {
    private final double compression;
    private final TDigest[] ringBuffer;
    private int currentBucket;
    private long lastRotateTimestampMillis;
    private final long durationBetweenRotatesMillis;
    public TimeWindowQuantile(double compression, int bucketNum, int timeWindowSeconds) {
        this.compression = compression;
        this.ringBuffer = new TDigest[bucketNum];
        for (int i = 0; i < bucketNum; i++) {
            this.ringBuffer[i] = TDigest.createDigest(compression);
        }
        this.currentBucket = 0;
        this.lastRotateTimestampMillis = System.currentTimeMillis();
        this.durationBetweenRotatesMillis = TimeUnit.SECONDS.toMillis(timeWindowSeconds) / bucketNum;
    }
    public synchronized double quantile(double q) {
        TDigest currentBucket = rotate();
        return currentBucket.quantile(q);
    }
    public synchronized void add(double value) {
        rotate();
        for (TDigest bucket : ringBuffer) {
            bucket.add(value);
        }
    }
    private TDigest rotate() {
        long timeSinceLastRotateMillis = System.currentTimeMillis() - lastRotateTimestampMillis;
        while (timeSinceLastRotateMillis > durationBetweenRotatesMillis) {
            ringBuffer[currentBucket] = TDigest.createDigest(compression);
            if (++currentBucket >= ringBuffer.length) {
                currentBucket = 0;
            }
            timeSinceLastRotateMillis -= durationBetweenRotatesMillis;
            lastRotateTimestampMillis += durationBetweenRotatesMillis;
        }
        return ringBuffer[currentBucket];
    }
}


《Apache Dubbo微服务开发从入门到精通》——可视化监测服务状态——二、 微服务集群监控(5) https://developer.aliyun.com/article/1224282

相关文章
|
17天前
|
Kubernetes Cloud Native 微服务
微服务实践之使用 kube-vip 搭建高可用 Kubernetes 集群
微服务实践之使用 kube-vip 搭建高可用 Kubernetes 集群
203 3
|
1月前
|
Dubbo Java 应用服务中间件
性能工具之JMeter Dubbo 脚本开发
【5月更文挑战第13天】性能工具之JMeter Dubbo 脚本开发
40 3
性能工具之JMeter Dubbo 脚本开发
|
9天前
|
Apache 数据库
杨校老师课堂之基于Apache的数据库连接池DBCP的工具类开发
杨校老师课堂之基于Apache的数据库连接池DBCP的工具类开发
11 0
|
6天前
|
监控 API 数据安全/隐私保护
构建高效后端服务:微服务架构的实践与挑战
【6月更文挑战第23天】在现代软件开发中,微服务架构已成为设计高性能、可扩展后端系统的首选模式。本文将深入探讨微服务的设计原则、实践方法及其面临的技术挑战,旨在为开发者提供一个全面的微服务实施指南。
21 3
|
14天前
|
存储 分布式计算 物联网
Apache IoTDB进行IoT相关开发实践
IoTDB是面向物联网的时序数据库,专注于时间序列数据管理,提供高效的数据处理、集成Hadoop和Spark生态、支持多目录存储策略。它还具有InfluxDB协议适配器,允许无缝迁移原本使用InfluxDB的业务。文章讨论了IoTDB的体系结构,包括数据文件、系统文件和预写日志文件的存储策略,并介绍了如何配置数据存储目录。此外,还提及了InfluxDB版本和查询语法的支持情况。IoTDB在物联网数据管理和分析中扮演关键角色,尤其适合处理大规模实时数据。
29 5
|
17天前
|
存储 分布式计算 物联网
Apache IoTDB进行IoT相关开发实践
物联网技术带来数据库管理挑战,特别是实时数据整合与安全性。IoTDB是一个专为时间序列数据设计的数据库,提供数据收集、存储和分析服务,适用于海量物联网数据。其架构包括数据文件、系统文件和预写日志文件的管理,并支持多目录存储策略。此外,IoTDB还开发了InfluxDB协议适配器,使得用户能无缝迁移原有InfluxDB业务。此适配器基于IoTDB的Java服务接口,转换InfluxDB的元数据格式,实现与IoTDB的数据交互。目前,适配器支持InfluxDB 1.x版本及部分查询语法。
60 5
|
18天前
|
消息中间件 运维 监控
微服务架构中的服务通信与数据一致性挑战
在微服务架构的海洋中,服务之间的通信和数据一致性问题犹如潜藏的暗礁和漩涡,随时可能威胁到整个应用的健康运行。本文将深入探讨微服务间通信机制的选择、数据一致性维护的策略,以及面对网络延迟和分区容忍性时如何保持系统的灵活性和健壮性。通过分析常见的模式和最佳实践,旨在为开发者提供一套应对这些挑战的航海图。
|
23天前
|
缓存 网络协议 算法
微服务架构之从类库到服务之服务发现
服务发现是分布式系统中的核心技术,其实现需要在可用性和一致性之间进行权衡。通过合理设计服务注册中心的架构,并采用有效的健康检查和缓存机制,可以提高系统的可靠性和可用性。不同的服务发现框架各有优缺点,选择适合的框架需要根据具体需求进行权衡和取舍。总之,服务发现的有效实现对于构建可靠的大型分布式系统至关重要。
16 3
|
2天前
|
JSON Java Spring
实战SpringCloud响应式微服务系列教程(第八章)构建响应式RESTful服务
实战SpringCloud响应式微服务系列教程(第八章)构建响应式RESTful服务
|
2天前
|
XML 监控 Dubbo
Dubbo怎么配置监控中心
**摘要:** 本文介绍了如何配置Dubbo的简单监控中心。首先,通过添加`&lt;dubbo:monitor protocol=&quot;registry&quot; /&gt;`到配置文件启用监控。接着,修改`dubbo.properties`设置Zookeeper地址。启动监控中心,服务提供者和消费者需添加`monitorEnabled=&quot;true&quot;`以开启监控功能。配置完成后,监控中心的Web界面能展示服务状态和性能指标,助力开发者和运维人员实时监控服务健康。

推荐镜像

更多