基于Actuator的故障诊断机制踩坑记录
最近在项目中集成Spring Boot Actuator进行应用监控,发现了一些令人头疼的问题。本文记录下遇到的典型故障诊断场景。
环境配置问题
最初按照官方文档配置时,发现health endpoint返回500错误。排查后发现是缺少了spring-boot-starter-actuator依赖,以及没有正确配置management.endpoints.web.exposure.include=*。
management:
endpoints:
web:
exposure:
include: health,info,metrics,prometheus
endpoint:
health:
show-details: always
健康检查自定义陷阱
尝试自定义健康检查时,使用了错误的返回格式:
@Component
public class CustomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// 错误写法 - 直接return new Health.Builder()
return Health.up().withDetail("status", "OK").build();
}
}
正确的做法应该是:
@Component
public class CustomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// 正确写法
return Health.status(Status.UP).withDetail("custom", "healthy").build();
}
}
监控数据获取失败
使用curl命令测试监控接口时发现:
# 错误的请求方式
curl http://localhost:8080/actuator/health
# 正确的方式应该是
curl -H "Accept: application/json" http://localhost:8080/actuator/health
诊断建议
- 确保所有相关依赖已添加
- 检查配置文件中的端点暴露设置
- 使用正确的API调用方式
- 关注监控数据的格式和权限问题
通过这些问题的解决,对Actuator的使用有了更深入的理解。

讨论