问题描述
限时送ChatGPT账号..我需要用 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.kafkamon.serialization.StringDeserializer")
props.put("value.deserializer", "org.apache.kafkamon.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屋!
更多推荐
[db:关键词]
发布评论