news 2026/6/9 4:46:35

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/5/30 7:18:34

立知-lychee-rerank-mm模型迁移学习:小样本场景应用

立知-lychee-rerank-mm模型迁移学习&#xff1a;小样本场景应用 1. 小众领域排序的现实困境 古玩市场里&#xff0c;一位资深藏家想快速比对三件清代瓷瓶的真伪特征。他手头有高清细节图、器型描述、款识拓片&#xff0c;还有一份专业鉴定报告的扫描件。传统搜索工具面对这种…

作者头像 李华
网站建设 2026/5/28 21:19:43

AI编程助手限制突破:开发工具权限重置的完整技术方案

AI编程助手限制突破&#xff1a;开发工具权限重置的完整技术方案 【免费下载链接】go-cursor-help 解决Cursor在免费订阅期间出现以下提示的问题: Youve reached your trial request limit. / Too many free trial accounts used on this machine. Please upgrade to pro. We h…

作者头像 李华
网站建设 2026/5/28 21:19:42

Lychee-Rerank部署教程:低显存(6GB)设备上的Qwen2.5-1.5B优化方案

Lychee-Rerank部署教程&#xff1a;低显存&#xff08;6GB&#xff09;设备上的Qwen2.5-1.5B优化方案 1. 项目概述 Lychee-Rerank是一款基于Qwen2.5-1.5B模型的本地检索相关性评分工具&#xff0c;专为低显存设备优化设计。它能高效评估查询语句与文档集的相关性&#xff0c;…

作者头像 李华
网站建设 2026/6/4 17:19:15

Coqui TTS模型下载与部署效率优化实战指南

最近在做一个语音合成的项目&#xff0c;用到了 Coqui TTS 这个强大的开源工具。不得不说&#xff0c;它的效果确实惊艳&#xff0c;但第一步——下载模型——就给了我一个“下马威”。动辄几百兆甚至上G的模型文件&#xff0c;加上默认的下载方式速度感人&#xff0c;依赖库的…

作者头像 李华
网站建设 2026/6/6 8:55:02

如何用Python解锁Blender创意潜能:从入门到实战的非传统指南

如何用Python解锁Blender创意潜能&#xff1a;从入门到实战的非传统指南 【免费下载链接】rhinoscriptsyntax rhinoscriptsyntax library for Python scripting engine that runs on both the Windows and OSX Rhino as well as Grasshopper 项目地址: https://gitcode.com/g…

作者头像 李华
网站建设 2026/6/6 1:40:18

RexUniNLU中文NLU效果对比:零样本vs 100条标注数据微调效果分析

RexUniNLU中文NLU效果对比&#xff1a;零样本vs 100条标注数据微调效果分析 1. 为什么这场对比值得你花5分钟读完 你有没有遇到过这样的场景&#xff1a;手头有个新业务&#xff0c;需要快速上线一个文本分类功能&#xff0c;但标注团队排期要两周&#xff0c;产品却明天就要…

作者头像 李华