Spring boot Spring Cloud Stream Kafka自定义健康检查不工作
我正在spring boot(消费者)应用程序中使用spring cloud stream kafka。即使应用程序无法连接到kafka(kafka broker已关闭),应用程序的运行状况也不准确。我读过关于卡夫卡健康检查的文章。看起来在弹簧执行器健康检查中禁用了kafka健康检查 因此,我成功地编写了以下代码来为我的应用程序启用kafka健康检查。我想,我在应用程序配置和代码之间缺少了一些联系,我看不到卡夫卡的运行状况 (1) 我正在创建一个自定义健康指示器bean,如下所示:Spring boot Spring Cloud Stream Kafka自定义健康检查不工作,spring-boot,spring-cloud-stream,spring-kafka,Spring Boot,Spring Cloud Stream,Spring Kafka,我正在spring boot(消费者)应用程序中使用spring cloud stream kafka。即使应用程序无法连接到kafka(kafka broker已关闭),应用程序的运行状况也不准确。我读过关于卡夫卡健康检查的文章。看起来在弹簧执行器健康检查中禁用了kafka健康检查 因此,我成功地编写了以下代码来为我的应用程序启用kafka健康检查。我想,我在应用程序配置和代码之间缺少了一些联系,我看不到卡夫卡的运行状况 (1) 我正在创建一个自定义健康指示器bean,如下所示:
import java.util.HashMap;
import java.util.Map;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.ByteArrayDeserializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.stream.binder.kafka.KafkaBinderHealthIndicator;
import org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder;
import org.springframework.cloud.stream.binder.kafka.properties.KafkaBinderConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.util.ObjectUtils;
@Configuration
@ConditionalOnClass(name = "org.springframework.boot.actuate.health.HealthIndicator")
public class KafkaBinderHealthIndicatorConfiguration {
@Bean
KafkaBinderHealthIndicator healthIndicator(KafkaMessageChannelBinder kafkaMessageChannelBinder,
KafkaBinderConfigurationProperties configurationProperties) {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
Map<String, Object> mergedConfig = configurationProperties.getConsumerConfiguration();
if (!ObjectUtils.isEmpty(mergedConfig)) {
props.putAll(mergedConfig);
}
if (!props.containsKey(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG)) {
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, configurationProperties.getKafkaConnectionString());
}
ConsumerFactory<?, ?> consumerFactory = new DefaultKafkaConsumerFactory<>(props);
KafkaBinderHealthIndicator indicator = new KafkaBinderHealthIndicator(kafkaMessageChannelBinder, consumerFactory);
indicator.setTimeout(configurationProperties.getHealthTimeout());
return indicator;
}
}
"binders": {
"status": "UNKNOWN",
"kafka": {
"status": "UNKNOWN"
}
},
通过使用最新版本的“spring cloud stream binder kafka”解决了该问题。最初我使用的是旧版本(版本早于1.3.0.RELEASE),卡夫卡的健康检查不起作用。正如@Sobychacko所建议的,我使用了最新版本2.0.0 REALEASE,kafka活页夹的健康检查很好:)没有自定义健康指示器bean “活页夹”:{ “状态”:“向上”, “卡夫卡”:{ “状态”:“向上”, “健康指标”:{ “状态”:“向上” } } },
此检查也应适用于版本1.3.0。版本问题已通过使用最新版本的“spring cloud stream binder kafka”解决。最初我使用的是旧版本(版本早于1.3.0.RELEASE),卡夫卡的健康检查不起作用。正如@Sobychacko所建议的,我使用了最新版本2.0.0 REALEASE,kafka活页夹的健康检查很好:)没有自定义健康指示器bean “活页夹”:{ “状态”:“向上”, “卡夫卡”:{ “状态”:“向上”, “健康指标”:{ “状态”:“向上” } } },
此检查还应适用于1.3.0.RELEASE版本您使用的是哪个版本的spring cloud stream?我刚刚用kafka活页夹在最新快照上本地尝试了一个应用程序,点击
/exactor/health
端点,看到出现了{“status”:“UP”}消息。当经纪人下跌时,也会看到“下跌”消息。默认情况下,如果缺少management.health.binders.enabled
属性,则该属性将匹配,无需将该属性设置为true。您是否可以尝试一个没有自定义Bean的简单应用程序来配置health indicator,并查看它是否适用于最新版本的spring cloud stream?当代理关闭时,只有在health indicator check.compile中连接到Kafka的请求超时后,才会出现“关闭”消息(组:org.springframework.cloud,名称:'spring-cloud-stream-binder-kafka11',版本:'1.3.0.RELEASE').好的,我将尝试一个最新版本的小应用程序。谢谢。你使用的是2.0.0.RELEASE吗?你使用的是哪个版本的spring cloud stream?我刚刚在kafka binder的最新快照上本地尝试了一个应用程序,点击/exactor/health
端点,看到{“status”:“UP”}消息出现。当经纪人下跌时,看到“下跌”消息。默认情况下,如果缺少management.health.binders.enabled
属性,则该属性将匹配,并且无需将该属性设置为true。您是否可以尝试一个没有自定义bean的简单应用程序进行健康指示器配置,并查看它是否适用于最新版本的spring cloud stream?当代理关闭时“DOWN”消息在health indicator check.compile(组:org.springframework.cloud,名称:'spring-cloud-stream-binder-kafka11',版本:'1.3.0.RELEASE')中连接到Kafka的请求超时后出现。好的,我将尝试一个最新版本的小应用程序。谢谢。您使用的是2.0.0.RELEASE吗?
"binders": {
"status": "UNKNOWN",
"kafka": {
"status": "UNKNOWN"
}
},