检查系统是否连接到Kafka [英] Check If System Is Connected To Kafka

查看:101
本文介绍了检查系统是否连接到Kafka的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用Java编写烟雾测试,以验证系统是否已连接到kafka,

I need to write a smoke test in Java which validates whether the system is connected to kafka,

有人有什么主意吗?我发现了这篇文章:

Does anyone have any idea? I have found this post:

如何检查Kafka Server是否正在运行?

但是用Java代码做起来太复杂了,我不认为这是我应该使用的方向.

But it's too complicated to do from a Java code and I don't think It's the direction i should use.

谢谢.

推荐答案

我有同样的问题,我不想离开这个问题而没有任何答案.我读了很多关于如何检查连接的信息,发现的大多数答案是检查与Zk的连接,但是我真的很想直接使用Kafka服务器检查连接.

I had the same question and I don't want to leave this question without any answer. I read a lot about how I can check the connection and most of the answers I found was checking the connection with Zk, but I really want to check the connection directly with Kafka server.

我要做的是创建一个简单的 KafkaConsumer ,并使用 listTopics()列出所有主题.如果连接成功,那么您将获得回报.否则,您将得到一个 TimeoutException .

What I did is to create a simple KafkaConsumer and list all the topics with listTopics(). If the connection is success, then you will get something as a return. Otherwise, you will get a 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 语句中以捕获异常.

then you can wrap this method in a try-catch sentence to catch the exception.

这篇关于检查系统是否连接到Kafka的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆