Search code examples
javaspring-bootapache-kafkaspring-kafka

How to check Kafka cluster connectivity and topic availability status using Spring Boot


I want to implement 2 separate methods to return boolean value based on Kafka cluster connectivity and then Kafka topic availability status.

However the KafkaAdmin/Admin provides methods to return topic list and descriptions at runtime. But I am not able to finalize the best approach to proceed further for both connectivity and topic availability check using Spring Boot.

@Component
@AllArgsConstructor
public class KafkaAdminstrator{

    private final AdminClient adminClient;

    public boolean isKafkaClusterExists(String bootstrapServer){
        //Kafka cluster connectivity check 
    ...
    }

    public boolean isTopicExists(String topic){
        //Kafka topic availability check
        //adminClient.describeTopics(topic);
    ...
    }
}

Solution

  • You should pass an AdminClient instance into both methods. The client already is defined with a bootstrap server property, so you wouldn't set a new one for the cluster check (describeCluster or describeTopics will both make a network request to the cluster, thus checking connectivity)

    If you want to really check cluster health, you'd expose JMX metrics on the servers, not from a client