Check if the system is connected to Kafka

I need to write a smoke test in Java that checks if the system is connected to kafka,

Does anyone have any ideas? I found this post:

How to check if the Kafka server is running?

But this is too difficult to do from Java code, and I don’t think this is the direction that I should use.

Thanks in advance.

+4
source share
2 answers

I had the same question, and I do not want to leave this question unanswered. I read a lot about how I can test the connection, and most of the answers I found are checking the connection to Zk, but I really want to check the connection directly to the Kafka server.

, KafkaConsumer listTopics(). , - . TimeoutException.

  def validateKafkaConnection(kafkaParams : mutable.Map[String, Object]) : Unit = {
    val props = new Properties()
    props.put("bootstrap.servers", kafkaParams.get("bootstrap.servers").get.toString)
    props.put("group.id", kafkaParams.get("group.id").get.toString)
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
    val simpleConsumer = new KafkaConsumer[String, String](props)
    simpleConsumer.listTopics()
  }

try-catch, .

+4

, , :

ZkClient zkClient = new ZkClient("your_zookeeper_server", 5000 /* ZOOKEEPER_SESSION_TIMEOUT */, 5000 /* ZOOKEEPER_CONNECTION_TIMEOUT */, ZKStringSerializer$.MODULE$);
List<Broker> brokers = scala.collection.JavaConversions.seqAsJavaList(zkUtils.getAllBrokersInCluster());
if (brokers.isEmpty()) {
    // No brokers available
} else {
    // There are brokers available
}
+1

Source: https://habr.com/ru/post/1663087/


All Articles