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

相关推荐
摇滚侠1 小时前
Spring Boot 3零基础教程,WEB 开发 静态资源默认配置 笔记27
spring boot·笔记·后端
wb043072013 小时前
性能优化实战:基于方法执行监控与AI调用链分析
java·人工智能·spring boot·语言模型·性能优化
Chen-Edward6 小时前
有了Spring为什么还有要Spring Boot?
java·spring boot·spring
magic334165636 小时前
Springboot整合MinIO文件服务(windows版本)
windows·spring boot·后端·minio·文件对象存储
小学鸡!7 小时前
Spring Boot实现日志链路追踪
java·spring boot·后端
番茄Salad7 小时前
Spring Boot临时解决循环依赖注入问题
java·spring boot·spring cloud
摇滚侠10 小时前
Spring Boot 3零基础教程,WEB 开发 自定义静态资源目录 笔记31
spring boot·笔记·后端·spring
摇滚侠10 小时前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 遍历 笔记40
spring boot·笔记·thymeleaf
橘子海全栈攻城狮11 小时前
【源码+文档+调试讲解】基于SpringBoot + Vue的知识产权管理系统 041
java·vue.js·人工智能·spring boot·后端·安全·spring
Json_12 小时前
学习springBoot框架-开发一个酒店管理系统,熟悉springboot框架语法~
java·spring boot·后端