检查系统是否连接到Kafka

编程入门 行业动态 更新时间:2024-10-23 04:58:34
本文介绍了检查系统是否连接到Kafka的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-19 12:46:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/963077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   系统   Kafka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!