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

简介: 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


相关文章
|
4月前
|
存储 Prometheus 监控
136_生产监控:Prometheus集成 - 设置警报与指标选择与LLM部署监控最佳实践
在大语言模型(LLM)部署的生产环境中,有效的监控系统是确保服务稳定性、可靠性和性能的关键。随着LLM模型规模的不断扩大和应用场景的日益复杂,传统的监控手段已难以满足需求。Prometheus作为当前最流行的开源监控系统之一,凭借其强大的时序数据收集、查询和告警能力,已成为LLM部署监控的首选工具。
|
7月前
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
721 3
|
7月前
|
JSON 分布式计算 大数据
springboot项目集成大数据第三方dolphinscheduler调度器
springboot项目集成大数据第三方dolphinscheduler调度器
428 3
|
7月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
721 2
|
7月前
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
springboot项目集成dolphinscheduler调度器 可拖拽spark任务管理
408 2
|
7月前
|
Java 测试技术 Spring
简单学Spring Boot | 博客项目的测试
本内容介绍了基于Spring Boot的博客项目测试实践,重点在于通过测试驱动开发(TDD)优化服务层代码,提升代码质量和功能可靠性。案例详细展示了如何为PostService类编写测试用例、运行测试并根据反馈优化功能代码,包括两次优化过程。通过TDD流程,确保每项功能经过严格验证,增强代码可维护性与系统稳定性。
306 0
|
7月前
|
存储 Java 数据库连接
简单学Spring Boot | 博客项目的三层架构重构
本案例通过采用三层架构(数据访问层、业务逻辑层、表现层)重构项目,解决了集中式开发导致的代码臃肿问题。各层职责清晰,结合依赖注入实现解耦,提升了系统的可维护性、可测试性和可扩展性,为后续接入真实数据库奠定基础。
572 0
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
163 0
|
分布式计算 Java 大数据
springboot项目集成dolphinscheduler调度器 项目管理
springboot项目集成dolphinscheduler调度器 项目管理
226 0
|
8月前
|
网络协议 Java
在SpringBoot项目中使用Netty实现远程调用
本文介绍了使用Netty解决网络连接性能问题的方法,重点讲解了Netty的NIO特性及其在SpringBoot中的应用。Netty作为高效的NIO框架,支持非阻塞IO,能通过单线程管理多个客户端连接,简化TCP/UDP套接字服务器开发。文章详细展示了Netty在SpringBoot中实现远程调用的过程,包括服务端与客户端代码实现、依赖配置及测试验证。通过示例代码,如`NettyServer`、`NettyClientUtil`等,清晰说明了Netty的工作原理和实际应用,解决了半包等问题,并提供了完整的测试结果。
852 3

热门文章

最新文章

推荐镜像

更多