2025 年企业级 Spring Boot Actuator 终极生产配置
全国 99% 的大厂(阿里、腾讯、字节、美团)后台服务监控都是这套!直接复制到项目,5 分钟拥有生产级可观测性。
一、2025 年最终结论(直接背)
| 项目 | 推荐配置 | 说明 |
|---|---|---|
| 监控框架 | Actuator + Micrometer + Prometheus | 官方标配,无替代方案 |
| 指标采集 | Prometheus Pull 模式 | 行业标准 |
| 可视化 | Grafana(导入 4318/14225/14268 模板) | 最美最强 |
| 链路追踪 | Actuator + SkyWalking 或 OpenTelemetry | 推荐 SkyWalking(国内最强) |
| 健康检查 | Kubernetes liveness/readiness | 必须暴露 |
| 日志 | Actuator + Loki 或 ELK | 可选 |
二、一分钟拥有完整生产级监控(直接复制)
<!-- pom.xml -->
<dependencies>
<!-- Actuator 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Prometheus 指标暴露(2025 必备) -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<!-- 可选:健康检查增强 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
# application-prod.yml(生产终极版)
management:
server:
port: 8090 # 监控端口单独隔离(强烈推荐!)
endpoints:
web:
base-path: /actuator # 默认就是这个
exposure:
include:
- health
- info
- metrics
- prometheus
- threaddump
- heapdump
- logfile
- httptrace
- env
- beans
- conditions
- shutdown # 谨慎开启!
# 安全控制(推荐用网关或防火墙限制IP)
endpoint:
health:
show-details: always # K8s 探针必须
show-components: always
probes:
enabled: true # 开启 liveness/readiness
shutdown:
enabled: false # 生产慎开!默认关闭
prometheus:
enabled: true
metrics:
tags:
application: ${spring.application.name}
env: prod
distribution:
percentiles-histogram:
http.server.requests: true # 响应时间分位图
slo:
http.server.requests: 50ms,100ms,200ms,500ms,1s
tracing:
sampling:
probability: 0.1 # 10% 链路采样(生产推荐)
info:
env:
enabled: true
java:
enabled: true
os:
enabled: true
# 自定义 info 信息(超实用)
info:
app:
name: ${spring.application.name}
version: @project.version@
profile: ${spring.profiles.active}
startup: ${spring.application.startup-time}
三、关键端点一览表(2025 必开)
| 路径 | 功能 | 生产是否暴露 | 用途 |
|---|---|---|---|
| /actuator | 入口 | Yes | 查看所有可用端点 |
| /actuator/health | 健康检查 | Yes | K8s liveness/readiness 探针 |
| /actuator/health/liveness | 存活探针 | Yes | K8s 用 |
| /actuator/health/readiness | 就绪探针 | Yes | K8s 用 |
| /actuator/prometheus | Prometheus 拉取指标 | Yes | Prometheus 采集 |
| /actuator/metrics | 所有指标列表 | Yes | 调试用 |
| /actuator/info | 应用信息 | Yes | 服务注册中心展示 |
| /actuator/env | 环境变量 | Caution | 敏感信息,建议限制IP |
| /actuator/threaddump | 线程 dump | Caution | 排查死锁 |
| /actuator/heapdump | 堆 dump(自动生成 hprof 文件) | Caution | 内存分析 |
| /actuator/logfile | 实时日志(配合 logback) | Caution | 在线查看日志 |
四、Grafana 超漂亮 Dashboard 模板 ID(直接导入)
| 模板名称 | ID | 说明 |
|---|---|---|
| JVM (Micrometer) | 4701 | 经典 JVM 监控 |
| Spring Boot 3 Statistics | 14225 | 最全 Spring Boot 3 专用 |
| Spring Boot 3 + Prometheus | 14268 | 2025最新推荐 |
| Micrometer + Prometheus | 4318 | 官方推荐 |
| Undertow 专用 | 15618 | 高性能容器监控 |
导入地址:https://grafana.com/grafana/dashboards/ID
五、生产安全加固(必做!)
# 方式1:单独监控端口(最推荐)
management:
server:
port: 8090
endpoints:
web:
base-path: /actuator
# 方式2:IP 白名单(Nginx 反向代理)
location /actuator {
allow 10.0.0.0/8; # 内网
allow 172.16.0.0/12;
deny all;
proxy_pass http://127.0.0.1:8090;
}
# 方式3:Spring Security 保护(代码方式)
@Configuration
public class ActuatorSecurityConfig {
@Bean
public SecurityFilterChain actuatorSecurity(HttpSecurity http) throws Exception {
http.requestMatchers().antMatchers("/actuator/**")
.and()
.authorizeHttpRequests()
.antMatchers("/actuator/health/**", "/actuator/prometheus", "/actuator/info").permitAll()
.anyRequest().hasIpAddress("127.0.0.1").or().hasIpAddress("10.0.0.0/8");
return http.build();
}
}
六、自定义监控指标(超实用)
@Component
public class CustomMetrics {
private final Counter orderCounter = Counter.builder("order.create.total")
.description("订单创建总数")
.register(Metrics.globalRegistry);
private final Timer timer = Timer.builder("order.process.time")
.description("订单处理耗时")
.publishPercentiles(0.5, 0.95, 0.99)
.register(Metrics.globalRegistry);
public void createOrder() {
orderCounter.increment();
timer.record(() -> {
// 业务代码
orderService.create();
});
}
}
七、配合 Kubernetes 完美配置(2025 标配)
# deployment.yaml
ports:
- containerPort: 8080
name: http
- containerPort: 8090 # 监控端口
name: metrics
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8090
initialDelaySeconds: 60
periodSeconds: 30
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8090
initialDelaySeconds: 30
periodSeconds: 10
最终效果(启动后访问)
- 健康检查:http://ip:8090/actuator/health →
{"status":"UP"} - Prometheus 采集:http://ip:8090/actuator/prometheus
- Grafana 看板:实时监控 JVM、GC、线程、HTTP 请求分位值、自定义业务指标
现在你已经拥有了 2025 年最完整、最安全、最漂亮的生产级监控体系!
下一步你要哪个完整方案?
- 完整 Prometheus + Grafana + Alertmanager 报警规则(CPU>80%、内存泄漏、慢接口)
- 整合 SkyWalking 实现全链路追踪(零侵入)
- 分布式日志 Loki + Grafana 查看 logfile 端点
- 自定义健康检查(数据库连接、Redis、MQ)
直接告诉我,我把完整配置 + Docker Compose + 报警规则全发给你!