From 55ea4924138c4ac74f9ab90041fb872069fc13c7 Mon Sep 17 00:00:00 2001 From: Marius Bogoevici Date: Thu, 28 Jul 2016 20:49:07 -0400 Subject: [PATCH] Add back KafkaBinderHealthIndicator --- .../kafka/KafkaBinderHealthIndicator.java | 83 +++++++++++++++++++ .../config/KafkaBinderConfiguration.java | 9 +- 2 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/KafkaBinderHealthIndicator.java diff --git a/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/KafkaBinderHealthIndicator.java b/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/KafkaBinderHealthIndicator.java new file mode 100644 index 000000000..eba4a0bcb --- /dev/null +++ b/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/KafkaBinderHealthIndicator.java @@ -0,0 +1,83 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.stream.binder.kafka; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.kafka.clients.consumer.ConsumerConfig; +import org.apache.kafka.clients.consumer.KafkaConsumer; +import org.apache.kafka.common.PartitionInfo; +import org.apache.kafka.common.serialization.ByteArrayDeserializer; + +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.HealthIndicator; +import org.springframework.cloud.stream.binder.kafka.config.KafkaBinderConfigurationProperties; + +/** + * Health indicator for Kafka. + * @author Ilayaperumal Gopinathan + * @author Marius Bogoevici + */ +public class KafkaBinderHealthIndicator implements HealthIndicator { + + private final KafkaMessageChannelBinder binder; + + private final KafkaBinderConfigurationProperties configurationProperties; + + public KafkaBinderHealthIndicator(KafkaMessageChannelBinder binder, + KafkaBinderConfigurationProperties configurationProperties) { + this.binder = binder; + this.configurationProperties = configurationProperties; + + } + + @Override + public Health health() { + Map properties = new HashMap<>(); + properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, this.configurationProperties + .getKafkaConnectionString()); + properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class.getName()); + KafkaConsumer metadataConsumer = new KafkaConsumer(properties); + try { + Set downMessages = new HashSet<>(); + for (String topic : this.binder.getTopicsInUse().keySet()) { + List partitionInfos = metadataConsumer.partitionsFor(topic); + for (PartitionInfo partitionInfo : partitionInfos) { + if (this.binder.getTopicsInUse().get(topic).contains(partitionInfo) && partitionInfo.leader() + .id() == -1) { + downMessages.add(partitionInfo.toString()); + } + } + } + if (downMessages.isEmpty()) { + return Health.up().build(); + } + return Health.down().withDetail("Following partitions in use have no leaders: ", downMessages.toString()) + .build(); + } + catch (Exception e) { + return Health.down(e).build(); + } + finally { + metadataConsumer.close(); + } + } +} diff --git a/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java b/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java index 82c523074..430210c27 100644 --- a/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java +++ b/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java @@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.stream.binder.Binder; +import org.springframework.cloud.stream.binder.kafka.KafkaBinderHealthIndicator; import org.springframework.cloud.stream.binder.kafka.KafkaExtendedBindingProperties; import org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder; import org.springframework.cloud.stream.config.codec.kryo.KryoCodecAutoConfiguration; @@ -72,8 +73,8 @@ public class KafkaBinderConfiguration { return new LoggingProducerListener(); } -// @Bean -// KafkaBinderHealthIndicator healthIndicator(KafkaMessageChannelBinder kafkaMessageChannelBinder) { -// return new KafkaBinderHealthIndicator(kafkaMessageChannelBinder, configurationProperties); -// } + @Bean + KafkaBinderHealthIndicator healthIndicator(KafkaMessageChannelBinder kafkaMessageChannelBinder) { + return new KafkaBinderHealthIndicator(kafkaMessageChannelBinder, this.configurationProperties); + } }