Spring Boot应用整合Prometheus

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

Spring Boot 应用整合 Prometheus

1)添加依赖

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

bash 复制代码
<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文件中添加以下配置:

bash 复制代码
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 ,添加上边启动的服务地址来执行监控

bash 复制代码
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 选择我们想要查看的应用即可。

相关推荐
Flittly8 小时前
【雕虫大技】Agent 动态 Skill 供应链安全加固(二):执行隔离沙箱实战
java·spring boot·spring
沐言人生9 小时前
HelloAgentR工程手记1/100天——项目工作流搭建
spring boot·agent·vibecoding
陌上丨9 小时前
Spring Boot 应用类型推断机制:一次从源码到原理的深度探索
java·spring boot·后端
云烟成雨TD10 小时前
Micrometer 系列【33】Spring Boot Micrometer Metrics 自动配置模块解析
spring boot·云原生·micrometer
用户31268748772012 小时前
接口太慢?Spring Boot 缓存体系 @Cacheable 全链路拆解
spring boot
snow@li12 小时前
SpringBoot:AOP日志切面全景梳理/原理+流程+实战+避坑
java·开发语言·spring boot
小张同学a.13 小时前
zabbix企业级监控平台4——分布式监控与grafana数据可视化
linux·运维·数据库·分布式·信息可视化·zabbix·grafana
snow@li14 小时前
SpringBoot:全套生命周期全景详解/应用级+Bean级
java·spring boot·rpc
凤山老林1 天前
Spring Boot @Async 线上实战:从默认配置到生产级线程池治理
java·spring boot·后端
凤山老林1 天前
Spring Boot 定时任务进阶:动态 Cron 与集群防重实战
java·spring boot·后端·定时任务·集群定时任务