news 2026/8/8 19:36:34

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/8/7 2:58:24

GTE模型在电商搜索中的应用:商品语义匹配最佳实践

GTE模型在电商搜索中的应用&#xff1a;商品语义匹配最佳实践 1. 为什么电商搜索需要GTE模型 电商平台上每天有数以百万计的用户搜索行为&#xff0c;但传统关键词匹配方式常常让人失望。你有没有遇到过这样的情况&#xff1a;输入“轻便透气运动鞋”&#xff0c;结果跳出一堆…

作者头像 李华
网站建设 2026/7/31 21:06:14

小白必看!MiniCPM-V-2_6多模态模型快速上手教程

小白必看&#xff01;MiniCPM-V-2_6多模态模型快速上手教程 你是不是经常看到别人用AI模型“看图说话”&#xff0c;识别图片内容、分析图表数据&#xff0c;甚至还能看懂视频&#xff0c;心里痒痒的也想试试&#xff1f;但一想到要下载模型、配置环境、写代码&#xff0c;就觉…

作者头像 李华
网站建设 2026/8/6 12:08:41

3种跨平台文件访问痛点解决方案:技术创新与实用价值指南

3种跨平台文件访问痛点解决方案&#xff1a;技术创新与实用价值指南 【免费下载链接】ext2read A Windows Application to read and copy Ext2/Ext3/Ext4 (With LVM) Partitions from Windows. 项目地址: https://gitcode.com/gh_mirrors/ex/ext2read 在当今多系统协同工…

作者头像 李华
网站建设 2026/8/3 20:18:18

5个突破性功能重构iOS移动体验:H5GG免越狱工具全解析

5个突破性功能重构iOS移动体验&#xff1a;H5GG免越狱工具全解析 【免费下载链接】H5GG an iOS Mod Engine with JavaScript APIs & Html5 UI 项目地址: https://gitcode.com/gh_mirrors/h5/H5GG 在iOS生态系统的封闭环境中&#xff0c;用户对个性化定制和功能扩展的…

作者头像 李华
网站建设 2026/7/26 10:06:09

通义千问3-Reranker-0.6B部署指南:Windows系统安装教程

通义千问3-Reranker-0.6B部署指南&#xff1a;Windows系统安装教程 1. 为什么选择Qwen3-Reranker-0.6B 最近在做RAG项目时&#xff0c;我反复对比了多个重排序模型&#xff0c;最终选定了Qwen3-Reranker-0.6B。不是因为它参数最多&#xff0c;而是它在实际使用中表现得特别稳…

作者头像 李华
网站建设 2026/8/6 11:06:46

PhotoDemon:轻量级图片工具的技术民主化实践

PhotoDemon&#xff1a;轻量级图片工具的技术民主化实践 【免费下载链接】PhotoDemon 项目地址: https://gitcode.com/gh_mirrors/ph/PhotoDemon 突破性能瓶颈&#xff1a;15MB体积如何实现专业级效果 告别传统图片编辑软件的臃肿安装包与复杂操作流程&#xff0c;Pho…

作者头像 李华