/actuator/health未检测Spring Cloud Stream已停止binder的解决方案咨询
问题1:停止绑定后健康检查未正确显示状态的原因
该现象是Spring Cloud Stream Kafka Streams Binder的默认设计导致的:
- 官方默认的
KafkaStreamsBinderHealthIndicator健康检查逻辑仅会收集处于运行状态的Kafka Streams绑定实例信息,当绑定被主动停止后,对应实例会被标记为非活跃状态,直接被默认健康检查逻辑过滤,不会出现在健康详情列表中。 - 由于没有检测到异常的活跃组件,
/actuator/health的整体状态自然保持为UP,无法触发基于健康状态的告警。
问题2:自定义健康指示器实现方案
你可以通过替换默认的Kafka Streams健康指示器,实现停止绑定保留在健康列表、整体状态变为DOWN的需求,无需修改Icinga的监控端点配置,代码示例如下:
第一步:关闭默认健康指示器
在application.yml中添加配置,禁用官方默认的Kafka Streams健康检查:
management: health: kstream: enabled: false
第二步:编写自定义健康指示器
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsBindingInformationCatalogue; import org.springframework.cloud.stream.binding.BindingsLifecycleRegistry; import org.springframework.cloud.stream.binding.BindingState; import org.springframework.stereotype.Component; import java.util.Map; @Component public class CustomKafkaStreamsHealthIndicator extends AbstractHealthIndicator { private final KafkaStreamsBindingInformationCatalogue streamsCatalogue; private final BindingsLifecycleRegistry lifecycleRegistry; // 注入Kafka Streams绑定信息仓库、绑定生命周期注册中心 public CustomKafkaStreamsHealthIndicator(KafkaStreamsBindingInformationCatalogue streamsCatalogue, BindingsLifecycleRegistry lifecycleRegistry) { this.streamsCatalogue = streamsCatalogue; this.lifecycleRegistry = lifecycleRegistry; } @Override protected void doHealthCheck(Health.Builder builder) throws Exception { // 默认先标记为UP builder.up(); // 获取所有Kafka Streams绑定的名称列表 Map<String, Object> allBindingDetails = streamsCatalogue.getBindingsDetails(); for (String bindingName : allBindingDetails.keySet()) { BindingState currentState = lifecycleRegistry.get(bindingName).getState(); if (currentState == BindingState.RUNNING) { // 运行状态的绑定直接透传原有健康详情 builder.withDetail(bindingName, allBindingDetails.get(bindingName)); } else { // 停止状态的绑定加入自定义详情,同时将整体健康状态设为DOWN builder.withDetail(bindingName, Map.of( "threadState", currentState.name(), "stopReason", "STOPPED_BY_CIRCUIT_BREAKER" )); builder.down(); } } } }
修改后停止的绑定会保留在/actuator/health的详情列表中,只要有任意绑定被停止,整体健康状态就会变为DOWN,可直接被Icinga识别触发告警。
内容的提问来源于stack exchange,提问作者gira1
相关产品推荐
相关产品推荐

