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

本文涉及的产品
可观测可视化 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


相关文章
|
16天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
23天前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
78 2
|
23天前
|
前端开发 JavaScript Java
6个SpringBoot 项目拿来就可以学习项目经验接私活
6个SpringBoot 项目拿来就可以学习项目经验接私活
34 0
|
1月前
|
前端开发 Java 关系型数据库
SpringBoot+MyBatis 天猫商城项目
SpringBoot+MyBatis 天猫商城项目
55 1
|
1月前
|
JSON JavaScript 前端开发
解决js中Long类型数据在请求与响应过程精度丢失问题(springboot项目中)
解决js中Long类型数据在请求与响应过程精度丢失问题(springboot项目中)
41 0
|
26天前
|
Java Maven 微服务
springboot项目开启远程调试-jar包
springboot项目开启远程调试-jar包
20 0
|
13天前
|
JSON 前端开发 Java
统一异常处理:让Spring Boot项目异常更优雅
统一异常处理:让Spring Boot项目异常更优雅
24 1
|
17天前
|
JSON 前端开发 Java
Springboot前后端分离项目统一封装返回结果
Springboot前后端分离项目统一封装返回结果
|
27天前
|
Prometheus 监控 Cloud Native
Spring Boot 应用可视化监控
Spring Boot 应用可视化监控
15 0
|
1月前
|
Java Unix Shell
springboot项目重启的shell命令
springboot项目重启的shell命令
16 0