Spring自定义HealthIndicator在Actuator存活/就绪端点未触发调用问题
问题原因与解决方案
核心原因
Spring Boot 2.4+ 引入的 livenessState 和 readinessState 端点,默认仅校验应用自身的基础存活/就绪状态(比如Spring上下文是否正常启动),不会自动聚合自定义的HealthIndicator。而/actuator/health端点默认会聚合所有HealthIndicator的结果,所以能触发你的自定义检查逻辑。
解决方案
通过配置健康分组,将自定义的MarkLogicDBHealthIndicator纳入存活/就绪探针的检查范围。修改你的application.yml配置,添加健康分组规则:
management: endpoint: health: probes: enabled: true livenessState: enabled: true readinessState: enabled: true health: groups: liveness: include: marklogic, livenessState readiness: include: marklogic, readinessState
配置说明
include: marklogic, livenessState:表示liveness端点会同时检查自定义的marklogic(你的HealthIndicator Bean名称)和Spring内置的livenessState(应用自身存活状态)readiness分组同理,同时校验自定义数据库状态和应用就绪状态
额外优化(可选)
如果希望自定义Indicator只参与就绪探针(比如数据库不可用不影响应用存活,但影响服务就绪),可以调整分组配置,仅把marklogic加入readiness组:
management: health: groups: liveness: include: livenessState readiness: include: marklogic, readinessState
验证
重启应用后,访问http://localhost:9743/actuator/health/readiness或http://localhost:9743/actuator/health/liveness,查看日志会发现MarkLogicDBHealthIndicator.health()方法已被调用,端点返回结果也会包含数据库健康状态的细节。
内容的提问来源于stack exchange,提问作者Bill Metazone
相关产品推荐
相关产品推荐

