news 2026/4/3 7:40:42

Spring Boot应用整合Prometheus:实现全方位应用监控

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring Boot应用整合Prometheus:实现全方位应用监控

一、Spring Boot应用整合Prometheus

Spring Boot Actuator为Spring Boot应用提供了一组强大的监控和管理端点,而Prometheus是一款开源的监控和告警工具。通过将两者结合,可以实时监控应用性能指标,并利用PromQL进行深入分析和可视化。

1.1 添加依赖

首先,在Spring Boot项目的pom.xml文件中添加以下依赖:

xml

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
  • spring-boot-starter-actuator:提供应用监控端点

  • micrometer-registry-prometheus:将Micrometer指标暴露为Prometheus格式

1.2 配置Actuator端点

application.yml配置文件中添加以下配置:

yaml

spring: application: name: PrometheusApp management: endpoints: web: exposure: include: '*' # 开启所有Actuator端点 tags: application: ${spring.application.name} # 在暴露的数据中添加application标签

配置说明

  • include: '*':开启所有Actuator端点服务

  • Spring Boot Actuator自带/actuator/prometheus端点,用于向Prometheus提供监控数据

  • 默认情况下该端点关闭,此配置将打开所有Actuator服务

Actuator端点参考
Spring Boot Actuator提供了丰富的端点,详细列表可参考官方文档:
Spring Boot Actuator Endpoints

1.3 启动并验证应用

启动Spring Boot应用后,访问以下地址查看Prometheus格式的指标数据:

text

http://localhost:8080/actuator/prometheus

该页面显示的应用监控指标包括:

  • JVM内存使用情况

  • 线程池状态

  • HTTP请求统计

  • 数据库连接池指标

  • 自定义业务指标等


二、将应用添加到Prometheus监控

2.1 配置Prometheus抓取任务

修改Prometheus配置文件prometheus.yml,添加Spring Boot应用的监控目标:

yaml

scrape_configs: - job_name: 'prometheusapp' # 任务名称 metrics_path: '/actuator/prometheus' # 指标路径 static_configs: - targets: ['192.168.2.234:8080'] # 应用地址

配置参数说明

  • job_name:监控任务名称,可自定义

  • metrics_path:指标暴露路径,对应Actuator的Prometheus端点

  • targets:应用实例地址列表,支持多个实例

2.2 重启Prometheus并验证

  1. 重启Prometheus服务使配置生效

  2. 访问Prometheus UI界面(默认端口9090)

  3. 进入Status → Targets页面

  4. 查看prometheusapp任务状态是否为UP

状态说明

  • UP:Prometheus成功连接到应用并获取指标

  • DOWN:连接失败,需检查网络和配置

2.3 查询应用指标

在Prometheus的Graph页面中,可以使用PromQL查询应用指标,例如:

promql

# 查询应用HTTP请求总数 http_server_requests_seconds_count{application="PrometheusApp"} # 查询应用内存使用量 jvm_memory_used_bytes{application="PrometheusApp"} # 查询应用CPU使用率 process_cpu_usage{application="PrometheusApp"}

三、使用Grafana Dashboard展示应用数据

3.1 下载Spring Boot监控模板

Grafana官方提供了丰富的Dashboard模板,Spring Boot应用监控推荐使用以下模板:

  1. 访问Grafana官方模板库:grafana.com/grafana/dashboards

  2. 搜索Spring Boot监控模板

  3. 本文使用模板ID:4701(Spring Boot 2.1 Statistics)

3.2 导入Dashboard模板

在Grafana中导入模板的步骤:

  1. 登录Grafana,点击左侧菜单Create → Import

  2. Import via grafana.com输入模板ID:4701

  3. 点击Load加载模板

  4. 选择Prometheus数据源

  5. 点击Import完成导入

3.3 配置与使用

导入成功后,Dashboard将展示以下关键指标:

JVM监控区域
  • 堆内存使用情况(Heap Memory Usage)

  • 非堆内存使用情况(Non-Heap Memory Usage)

  • 垃圾回收统计(Garbage Collection)

  • 线程状态(Thread States)

HTTP请求监控
  • 请求速率(Request Rate)

  • 错误率(Error Rate)

  • 响应时间分布(Response Time Percentiles)

系统资源监控
  • CPU使用率(CPU Usage)

  • 文件描述符(File Descriptors)

  • 日志事件(Log Events)

数据库监控
  • 连接池状态(Connection Pool)

  • 查询性能(Query Performance)

3.4 多应用筛选

如果监控多个Spring Boot应用,Dashboard顶部提供了Application下拉选择器,可以切换查看不同应用的监控数据。


四、高级配置与优化

4.1 自定义业务指标

除了系统默认指标,还可以在应用中自定义业务指标:

java

import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.stereotype.Component; @Component public class BusinessMetrics { private final Counter orderCounter; public BusinessMetrics(MeterRegistry registry) { orderCounter = Counter.builder("orders.total") .tag("application", "PrometheusApp") .description("Total number of orders") .register(registry); } public void incrementOrder() { orderCounter.increment(); } }

4.2 安全配置

生产环境中,建议对Actuator端点进行安全保护:

yaml

management: endpoints: web: exposure: include: health,info,prometheus # 仅暴露必要端点 endpoint: prometheus: enabled: true server: port: 9091 # 使用独立端口

同时配置Spring Security:

java

@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/actuator/prometheus").hasRole("MONITOR") .anyRequest().authenticated() .and() .httpBasic(); } }

4.3 指标采样与聚合

对于高并发应用,可配置指标采样以减少数据量:

yaml

management: metrics: export: prometheus: step: 30s # 采样间隔 distribution: percentiles-histogram: http.server.requests: true # 启用HTTP请求直方图

五、常见问题排查

5.1 Prometheus无法连接应用

  • 检查网络连通性:确保Prometheus服务器能访问应用IP和端口

  • 验证端点可访问:直接浏览器访问http://应用IP:端口/actuator/prometheus

  • 检查防火墙规则:确保端口未被防火墙阻止

5.2 指标数据不显示

  • 确认依赖正确:检查micrometer-registry-prometheus版本兼容性

  • 验证配置生效:确保management.endpoints.web.exposure.include包含prometheus

  • 查看应用日志:检查是否有相关错误日志

5.3 Grafana面板无数据

  • 检查数据源配置:确认Grafana中Prometheus数据源配置正确

  • 验证查询语句:在Grafana中使用Explore功能测试PromQL查询

  • 检查时间范围:确保查询的时间范围内有数据


总结

通过本文的步骤,您将Spring Boot应用与Prometheus监控系统集成,并利用Grafana实现了数据的可视化展示。这套监控方案提供了:

  1. 全面监控:覆盖JVM、HTTP请求、系统资源、业务指标等多个维度

  2. 实时告警:基于Prometheus Alertmanager可实现灵活的告警规则

  3. 历史分析:长期存储监控数据,便于性能趋势分析和问题排查

  4. 多应用管理:支持同时监控多个Spring Boot应用实例

后续优化建议

  • 根据业务特点自定义监控指标

  • 设置合理的告警阈值和通知渠道

  • 定期审查和优化监控配置

  • 结合日志系统(如ELK)进行全链路监控

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/28 8:56:53

3步掌握OpenLayers扩展实战:从入门到地图开发进阶

3步掌握OpenLayers扩展实战&#xff1a;从入门到地图开发进阶 【免费下载链接】ol-ext Cool extensions for Openlayers (ol) - animated clusters, CSS popup, Font Awesome symbol renderer, charts for statistical map (pie/bar), layer switcher, wikipedia layer, animat…

作者头像 李华
网站建设 2026/4/2 8:32:34

cv_unet_image-colorization实测:3步完成专业级照片上色

cv_unet_image-colorization实测&#xff1a;3步完成专业级照片上色 你有没有翻看过家里的老相册&#xff1f;那些泛黄的黑白照片&#xff0c;记录着珍贵的回忆&#xff0c;却总让人觉得少了点什么。是的&#xff0c;缺少色彩。过去&#xff0c;给黑白照片上色是件费时费力的专…

作者头像 李华
网站建设 2026/3/29 2:52:15

YOLOv5与Magma融合:智能视频分析系统实战

YOLOv5与Magma融合&#xff1a;智能视频分析系统实战 最近在做一个智能监控项目&#xff0c;客户提了个挺有意思的需求&#xff1a;不仅要能实时检测画面里的人、车、物&#xff0c;还要能理解他们在干什么&#xff0c;甚至预测接下来可能发生什么。比如停车场里&#xff0c;不…

作者头像 李华
网站建设 2026/3/26 19:52:23

5个理由让fre:ac成为你的音频处理瑞士军刀

5个理由让fre:ac成为你的音频处理瑞士军刀 【免费下载链接】freac The fre:ac audio converter project 项目地址: https://gitcode.com/gh_mirrors/fr/freac 在数字音频处理领域&#xff0c;选择一款既能满足专业需求又易于上手的工具往往是件头疼事。fre:ac作为一款开…

作者头像 李华
网站建设 2026/3/3 13:53:01

即梦LoRA镜像测评:多版本模型切换如此简单

即梦LoRA镜像测评&#xff1a;多版本模型切换如此简单 在文生图工作流中&#xff0c;一个常被低估却极其关键的痛点是&#xff1a;如何高效验证LoRA训练过程中的效果演进&#xff1f; 你花了几周时间迭代训练了10个Epoch的即梦&#xff08;Jimeng&#xff09;风格LoRA&#xf…

作者头像 李华
网站建设 2026/3/13 1:50:45

GTE+SeqGPT边缘计算部署:树莓派实战案例

GTESeqGPT边缘计算部署&#xff1a;树莓派实战案例 1. 引言&#xff1a;当AI遇见树莓派 想象一下&#xff0c;你有一个智能家居项目&#xff0c;需要让设备理解你的语音指令&#xff0c;并根据本地存储的说明书自动生成解决方案。或者&#xff0c;你正在开发一个野外科研设备…

作者头像 李华