【监控利器Prometheus】——Prometheus+Grafana监控SpringBoot项目业务指标监控

本文涉及的产品
可观测监控 Prometheus 版,每月50GB免费额度
可观测可视化 Grafana 版,10个用户账号 1个月
简介: Prometheus+Grafana监控SpringBoot项目业务指标监控1、SpringBoot项目配置2、prometheus添加配置3、Grafana配置

1、SpringBoot项目配置


(1)maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>


(2)application.properties


spring.application.name=springboot_business
server.port=6002
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}


(3)这里以【订单成功数量】、【订单失败数量】、【订单成功金额】、【订单失败金额】为例进行统计

import com.alibaba.fastjson.JSON;
import com.danny.monitor.prometheus.business.bean.Order;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private MeterRegistry registry;
    private Counter orderSuccessCounter; // 累计成功订单数量
    private Counter orderFailedCounter; // 累计失败订单数量
    private DistributionSummary orderSuccessSummary; // 成功订单金额
    private DistributionSummary orderFailedSummary; // 失败订单金额
    @PostConstruct
    private void init() {
        orderSuccessCounter = registry.counter("create_order_total", "create_order_total", "success");
        orderFailedCounter = registry.counter("create_order_total", "create_order_total", "failed");
        orderSuccessSummary = registry.summary("order_amount", "order_amount", "success");
        orderFailedSummary = registry.summary("order_amount", "order_amount", "failed");
    }
    // 创建订单
    @Override
    public Order createOrder(BigDecimal orderAmount) {
        log.info("createOrder orderAmount:{}", orderAmount);
        Order order = null;
        try {
            if (orderAmount.compareTo(BigDecimal.valueOf(10)) < 0) {
                throw new Exception("金额小于10时,模拟订单失败的情况");
            }
            order = new Order();
            order.setOrderAmount(orderAmount);
            order.setOrderNo(RandomStringUtils.randomAlphabetic(32));
            order.setOrderTime(LocalDateTime.now());
            orderSuccessCounter.increment(); // 订单成功数量埋点数据
            orderSuccessSummary.record(orderAmount.doubleValue()); // 订单成功金额埋点数据
        } catch (Exception e) {
            log.info("createOrder error", e);
            orderFailedCounter.increment();  // 订单失败数量埋点数据
            orderFailedSummary.record(orderAmount.doubleValue());  // 订单失败金额埋点数据
            order = null;
        } finally {
            log.info("queryOrder result:{}", JSON.toJSONString(order));
            return order;
        }
    }
}


2、prometheus添加配置


修改prometheus.yml,添加SpringBoot项目的地址

scrape_configs:
  - job_name: 'springboot_business'
    scrape_interval: 10s
    scrape_timeout: 10s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['10.246.140.63:6002']


重启Prometheus后,启动SpringBoot项目并访问几次下单接口(目的是为了造点埋点数据),在 http://localhost:6002/actuator/prometheus 页面可以看到在上面 OrderServiceImpl 中添加的统计指标。


在Prometheus UI界面通过PromeSQL查询统计指标的数据:

(1)查询【创建订单数量】统计数据


33.png


(2)查询【创建订单金额】统计数据


34.png

以上数据在重启SpringBoot项目后,就会丢失。可以把counter、summary中的值在缓存中存一份,在启动的时候初始化counter设置初始值。


3、Grafana配置


(1)订单数量统计

在Grafana中新建一个Dashboard(Create -> Dashboard),在新的Dashboard中新建一个Pannel(Add a new pannel),Pannel标题为【订单数量】,数据源选择【Prometheus】,Metircs选择【create_order_total】(在上面代码中定义的统计订单数量的Counter的名称),Legend写【{undefined{create_order_total}}】(在上面代码中定义的统计订单数量的Counter的tag的key)。


35.png

35.png


保存

36.png


(2)订单金额统计

按照同样的方式添加统计订单金额的pannel,需要注意的是,DistributionSummary定义的指标,在prometheus中会加个后缀,比如上面定义的 DistributionSummary 的 name 为 “order_amount”,在prometheus收集时会有“order_amount_sum”、“order_amount_count”、“order_amount_max”多个指标,具体可以在 http://localhost:6002/actuator/prometheus 中查看。


37.png


(3)订单增长率统计

在Dashboard中新建一个Pannel(Add a new pannel),Pannel标题为【订单量增长率】,数据源选择【Prometheus】,Metircs写【rate(create_order_total [1m])】(表示1分钟内的订单量增长率),Legend写【{undefined{create_order_total}}】(在上面代码中定义的统计订单数量的Counter的tag的key),在Standard options -> Unit 中选择纵坐标的显示方式为 Percent(0.0-1.0),也就是百分比显示。


38.png


(4)同理可以按照(3)的方式配置订单金额增长率的Pannel

39.png


相关实践学习
通过可观测可视化Grafana版进行数据可视化展示与分析
使用可观测可视化Grafana版进行数据可视化展示与分析。
相关文章
|
23天前
|
自然语言处理 IDE Java
SpringBoot start.aliyun.com创建项目,解决properties乱码的问题
通过确保文件和开发环境的编码一致,配置 Maven 编码,设置 Spring Boot 应用和嵌入式服务器的编码,可以有效解决 properties 文件的乱码问题。以上步骤可以帮助开发者确保在 Spring Boot 项目中正确处理和显示多语言字符,避免因编码问题导致的乱码现象。
36 5
|
28天前
|
XML Java 应用服务中间件
SpringBoot项目打war包流程
本文介绍了将Spring Boot项目改造为WAR包并部署到外部Tomcat服务器的步骤。主要内容包括:1) 修改pom.xml中的打包方式为WAR;2) 排除Spring Boot内置的Tomcat依赖;3) 添加Servlet API依赖;4) 改造启动类以支持WAR部署;5) 打包和部署。通过这些步骤,可以轻松地将Spring Boot应用转换为适合外部Tomcat服务器的WAR包。
130 64
SpringBoot项目打war包流程
|
1月前
基于springboot+thymeleaf+Redis仿知乎网站问答项目源码
基于springboot+thymeleaf+Redis仿知乎网站问答项目源码
134 36
|
2月前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana监控Linux主机
通过本文的步骤,我们成功地在 Linux 主机上使用 Prometheus 和 Grafana 进行了监控配置。具体包括安装 Prometheus 和 Node Exporter,配置 Grafana 数据源,并导入预设的仪表盘来展示监控数据。通过这种方式,可以轻松实现对 Linux 主机的系统指标监控,帮助及时发现和处理潜在问题。
206 7
|
2月前
|
存储 数据采集 Prometheus
Grafana Prometheus Altermanager 监控系统
Grafana、Prometheus 和 Alertmanager 是一套强大的开源监控系统组合。Prometheus 负责数据采集与存储,Alertmanager 处理告警通知,Grafana 提供可视化界面。本文简要介绍了这套系统的安装配置流程,包括各组件的下载、安装、服务配置及开机自启设置,并提供了访问地址和重启命令。适用于希望快速搭建高效监控平台的用户。
162 20
|
2月前
|
Prometheus 监控 Cloud Native
无痛入门Prometheus:一个强大的开源监控和告警系统,如何快速安装和使用?
Prometheus 是一个完全开源的系统监控和告警工具包,受 Google 内部 BorgMon 系统启发,自2012年由前 Google 工程师在 SoundCloud 开发以来,已被众多公司采用。它拥有活跃的开发者和用户社区,现为独立开源项目,并于2016年加入云原生计算基金会(CNCF)。Prometheus 的主要特点包括多维数据模型、灵活的查询语言 PromQL、不依赖分布式存储、通过 HTTP 拉取时间序列数据等。其架构简单且功能强大,支持多种图形和仪表盘展示模式。安装和使用 Prometheus 非常简便,可以通过 Docker 快速部署,并与 Grafana 等可
484 2
|
2月前
|
Prometheus 运维 监控
Prometheus+Grafana+NodeExporter:构建出色的Linux监控解决方案,让你的运维更轻松
本文介绍如何使用 Prometheus + Grafana + Node Exporter 搭建 Linux 主机监控系统。Prometheus 负责收集和存储指标数据,Grafana 用于可视化展示,Node Exporter 则采集主机的性能数据。通过 Docker 容器化部署,简化安装配置过程。完成安装后,配置 Prometheus 抓取节点数据,并在 Grafana 中添加数据源及导入仪表盘模板,实现对 Linux 主机的全面监控。整个过程简单易行,帮助运维人员轻松掌握系统状态。
295 3
|
26天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue实现的留守儿童爱心网站设计与实现(计算机毕设项目实战+源码+文档)
博主是一位全网粉丝超过100万的CSDN特邀作者、博客专家,专注于Java、Python、PHP等技术领域。提供SpringBoot、Vue、HTML、Uniapp、PHP、Python、NodeJS、爬虫、数据可视化等技术服务,涵盖免费选题、功能设计、开题报告、论文辅导、答辩PPT等。系统采用SpringBoot后端框架和Vue前端框架,确保高效开发与良好用户体验。所有代码由博主亲自开发,并提供全程录音录屏讲解服务,保障学习效果。欢迎点赞、收藏、关注、评论,获取更多精品案例源码。
61 10
|
26天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue实现的家政服务管理平台设计与实现(计算机毕设项目实战+源码+文档)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
45 8
|
26天前
|
JavaScript 搜索推荐 Java
基于SpringBoot+Vue实现的家乡特色推荐系统设计与实现(源码+文档+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
56 8