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

相关推荐
后端小张15 小时前
基于飞算AI的图书管理系统设计与实现
spring boot
考虑考虑1 天前
Jpa使用union all
java·spring boot·后端
阿杆2 天前
同事嫌参数校验太丑,我直接掏出了更优雅的 SpEL Validator
java·spring boot·后端
昵称为空C2 天前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
麦兜*3 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*3 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
汤姆yu3 天前
基于springboot的毕业旅游一站式定制系统
spring boot·后端·旅游
爱敲代码的TOM3 天前
Prometheus+Grafana构建企业级监控方案
prometheus
计算机毕业设计木哥3 天前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
java·vue.js·spring boot·mysql·spark·毕业设计·课程设计
hdsoft_huge3 天前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot