Spring Boot应用整合Prometheus

本文介绍了如何将SpringBootActuator与Prometheus结合,实现SpringBoot应用程序的实时监控,包括添加依赖、配置Actuator、启动应用和设置Prometheus监控。还提到使用Grafana展示监控数据的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot Actuator 提供了一组用于监控和管理 Spring Boot 应用程序的端点,而 Prometheus 是一个开源的监控和告警工具。通过将这两者结合起来,您可以实时监控您的应用程序的性能指标,并通过 Prometheus 提供的丰富的查询语言来分析和可视化这些指标。

Spring Boot 应用整合 Prometheus

1)添加依赖
首先,您需要在您的 Spring Boot 项目中添加以下依赖:

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

这将添加 Actuator 和 Prometheus 的依赖项到您的项目中。

2)配置 Actuator,启用 Prometheus 端点
application.yml文件中添加以下配置:

spring:
  application:
    name: PrometheusApp

# Prometheus springboot监控配置
management:
  endpoints:
    web:
      exposure:
        include: '*'
    tags:
      application: ${spring.application.name} # 暴露的数据中添加application label

include=* 配置为开启 Actuator 服务,Spring Boot Actuator 自带了一个/actuator/Prometheus的监控端点供给 Prometheus 抓取数据。不过默认该服务是关闭的,所以,使用该配置将打开所有的 Actuator 服务。

Actuator 默认的端点很多,详见:
https://docs.spring.io/spring-boot/docs/2.4.3/reference/html/production-ready-features.html#production-ready-endpoints

3)启动应用程序
现在,您可以启动您的 Spring Boot 应用程序了。
您可以通过访问[http://localhost:8080/actuator/prometheus](http://localhost:8080/actuator/prometheus)来查看 Prometheu 实时监控的指标数据。
以上就是使用 Spring Boot Actuator 和 Prometheus 监控应用程序的整个过程。

将应用添加到 Prometheus

首先,修改 Prometheus 的配置文件 prometheus.yml ,添加上边启动的服务地址来执行监控

scrape_configs:
  - job_name: 'prometheusapp'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['192.168.2.234:8080'] 

上面的 prometheusapp 就是前面创建的 Spring Boot 应用程序,也就是 Prometheus 需要监控的服务地址。
然后,重启 Prometheus 服务,查看 Prometheus UI 界面确认 Target 是否添加成功。

使用 Grafana Dashboard 展示应用数据

[https://grafana.com/grafana/dashboards](https://grafana.com/grafana/dashboards)下载 Spring Boot 的模板(这里使用的是编号4701)。
根据 ID 导入模板,导入完毕后,就可以看到 JVM 的各项监控指标,如果有多个应用,可以通过 Application 选择我们想要查看的应用即可。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Firechou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值