springboot2.5.15整合prometheus
以下是 Spring Boot 2.5.15 整合 Prometheus 的完整步骤和注意事项:
一、依赖配置(核心步骤)
添加必要依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.7.6</version> <!-- 与Spring Boot 2.5.x兼容版本 --> </dependency>
1
2
3
4
5
6
7
8
9版本兼容性说明
• Spring Boot 2.5.x 官方认证的 Micrometer 版本为 1.7.x• Prometheus Client 推荐使用 0.12.0(避免高版本出现类缺失问题)
二、Actuator端点配置
基础配置(application.yml)
management: endpoints: web: exposure: include: prometheus,health,metrics # 暴露Prometheus端点 base-path: /monitor # 自定义监控路径(可选) metrics: tags: application: ${spring.application.name} # 全局标签 server: response: content-type: text/plain # 强制指定响应类型
1
2
3
4
5
6
7
8
9
10
11
12启用Prometheus端点 • 默认暴露路径:
http://localhost:8080/monitor/prometheus
三、Prometheus服务端配置
修改prometheus.yml
scrape_configs: - job_name: 'springboot-app' metrics_path: '/monitor/prometheus' # 与Actuator配置一致 scrape_interval: 15s static_configs: - targets: ['192.168.1.100:8080'] # 应用实际IP和端口
1
2
3
4
5
6验证抓取状态 • 访问 Prometheus 控制台(
http://prometheus-server:9090
)• 检查 Targets 页面中应用状态是否为 UP
四、自定义指标开发
添加JVM监控(自动集成) • 默认已包含 JVM 内存、线程、GC 等指标
自定义业务指标
@Service public class OrderService { private final Counter orderCounter; @Autowired public OrderService(MeterRegistry registry) { orderCounter = Counter.builder("orders.total") .description("Total orders created") .tag("type", "online") .register(registry); // 注册自定义计数器 } public void createOrder() { orderCounter.increment(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
五、Grafana可视化配置
数据源设置 • 添加 Prometheus 数据源(URL:
http://prometheus-server:9090
)导入仪表板模板 • JVM监控:使用仪表板ID
4701
(Micrometer标准模板)• Spring Boot监控:使用仪表板ID
11378
六、常见问题解决
问题现象 | 解决方案 |
---|---|
/actuator/prometheus 404 | 检查依赖是否包含micrometer-registry-prometheus |
指标数据缺失 | 确认management.endpoints.web.exposure.include 包含prometheus |
Prometheus抓取超时 | 检查防火墙设置,确保Prometheus服务器能访问应用端口 |
版本冲突导致类加载失败 | 使用mvn dependency:tree 排查旧版本prometheus-client |
七、扩展优化建议
安全加固
• 使用 Spring Security 保护 Actuator 端点• 限制
/monitor/**
路径的访问权限性能优化
• 调整抓取间隔:生产环境建议30s
(避免高频采集影响性能)• 启用 Prometheus 的
time series compression
功能
引用来源
: SpringBoot整合Prometheus基础配置(网页1)
: Actuator端点暴露策略(网页3)
: 自定义监控路径和全局标签(网页4)
: 自定义业务指标实现(网页5)
: Prometheus与Grafana集成(网页6)
: Micrometer版本兼容性说明(网页7)
: Prometheus抓取配置与验证(网页8)
: JVM监控与仪表板模板(网页9)
: Grafana数据源和仪表板设置(网页10)
如需更详细的配置示例或遇到特定问题,可参考对应网页的完整说明。