一、Spring Boot 3 暴露监控端点
powered by Moshow@https://zhengkai.blog.csdn.net/
1. 添加依赖
<dependencies>
<!-- Actuator 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Prometheus 指标输出 -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>
2. 配置应用 (application.yml
)
management:
endpoints:
web:
exposure:
include: "prometheus,health,metrics" # 暴露监控端点
metrics:
tags:
application: ${spring.application.name} # 添加应用标识标签
server:
port: 8080 # 应用端口
3. 验证端点
启动应用后访问:
http://localhost:8080/actuator/prometheus
将看到 Prometheus 格式的指标(如 jvm_memory_used_bytes
, http_server_requests_seconds_count
)
⚙️ 二、Prometheus 抓取配置
1. 修改 prometheus.yml
scrape_configs:
- job_name: "spring_boot_app"
scrape_interval: 15s
metrics_path: /actuator/prometheus # 指标端点路径
static_configs:
- targets: ["host.docker.internal:8080"] # 应用地址
labels:
env: "dev" # 自定义标签
关键说明:
如果 Prometheus 在 Docker 中运行,使用
host.docker.internal
访问宿主机应用本地直接运行时用
localhost:8080
2. 启动 Prometheus
docker run -d -p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
3. 验证抓取状态
访问 http://localhost:9090/targets
✅ 状态应为 UP
✅ 检查 /actuator/prometheus
的指标是否出现
📊 三、Grafana 配置仪表板
1. 添加 Prometheus 数据源
-
访问
http://localhost:3000
(默认账号 admin/admin) -
Configuration → Data Sources → Add data source
-
选择 Prometheus,URL 填
http://prometheus-host:9090
2. 导入 Spring Boot 仪表板
使用官方推荐的仪表板模板:
-
Create → Import
-
输入仪表板 ID:
11378
(Spring Boot 2.x/3.x 通用) -
选择 Prometheus 数据源
https://grafana.com/api/dashboards/11378/images/8342/image
3. 核心监控指标示例
监控目标 | PromQL 查询 |
---|---|
JVM 内存使用 | jvm_memory_used_bytes{area="heap"} |
HTTP 请求速率 | rate(http_server_requests_seconds_count[1m]) |
CPU 使用率 | system_cpu_usage |
数据库连接池 | tomcat_jdbc_connections_active |
异常数量 | rate(spring_errors_total[5m]) |
⚠️ 四、常见问题解决方案
问题 1:Prometheus 无法抓取指标
-
原因:网络不通/安全组限制
-
解决:
# application.yml 添加 server: forward-headers-strategy: native # 解决代理环境问题 management: server: port: 8081 # 独立管理端口(可选)
问题 2:指标名称不匹配
-
现象:Grafana 显示 "No Data"
-
解决:在 Prometheus 中检查指标是否存在:
{__name__=~"jvm_.*"} # 查看所有JVM指标
问题 3:安全加固(生产环境必做)
根据自身业务场景开发接口,其他接口需要授权即可
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/actuator/prometheus").permitAll() // 开放监控端点
.requestMatchers("/actuator/**").hasRole("ADMIN") // 保护其他端点
);
return http.build();
}
}
🔧 五、高级配置技巧
1. 自定义业务指标
@Service
public class OrderService {
private final Counter orderCounter;
public OrderService(MeterRegistry registry) {
orderCounter = Counter.builder("app.orders.count")
.description("Total orders created")
.tag("env", "prod")
.register(registry);
}
public void createOrder() {
orderCounter.increment();
// 业务逻辑...
}
}
2. 指标标签分组
management:
metrics:
distribution:
percentiles-histogram:
http.server.requests: true # 开启HTTP请求分位数统计
percentiles:
http.server.requests: 0.5,0.95,0.99 # 统计P50/P95/P99
3. Grafana 告警设置
# HTTP错误率 > 1% sum(rate(http_server_requests_seconds_count{status!~"2.."}[5m])) / sum(rate(http_server_requests_seconds_count[5m])) > 0.01