基于Actuator的故障诊断机制

ThickBronze +0/-0 0 0 正常 2025-12-24T07:01:19 Spring Boot · 监控

基于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

诊断建议

  1. 确保所有相关依赖已添加
  2. 检查配置文件中的端点暴露设置
  3. 使用正确的API调用方式
  4. 关注监控数据的格式和权限问题

通过这些问题的解决,对Actuator的使用有了更深入的理解。

推广
广告位招租

讨论

0/2000
心灵的迷宫
心灵的迷宫 · 2026-01-08T10:24:58
Actuator健康检查500错误基本都是依赖或配置问题,别急着看代码,先确认spring-boot-starter-actuator是否引入,并且management.endpoints.web.exposure.include配置正确。
Sam353
Sam353 · 2026-01-08T10:24:58
自定义HealthIndicator千万别用Health.up()这种简化写法,必须显式使用Status.UP,否则Actuator解析会出错,建议加个日志打印确保状态正常。
Bella359
Bella359 · 2026-01-08T10:24:58
curl测试actuator接口记得加Accept头,不然返回的是HTML而不是JSON,调试时很容易被误导。可以先用Postman或者浏览器直接访问确认格式。
WetSong
WetSong · 2026-01-08T10:24:58
监控数据权限控制很关键,特别是生产环境,建议将敏感端点如env、configprops等从exposure.exclude中排除,避免信息泄露