检查kafka是否正常连接
By:Roy.LiuLast updated:2021-12-24
本文简单记录了利用java代码判断kafka是否正常工作。原理就是配置参数之后,连接kafka,看是否可以在规定的时间 (超时时间)内获取到topics.
一、依赖包.
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.12</artifactId> <version>3.0.0</version> </dependency>
二、判断逻辑
public boolean kafkaAlive(){ Properties properties = new Properties(); properties.put("bootstrap.servers", "192.168.2.200:9092"); properties.put("connections.max.idle.ms", 10000); properties.put("request.timeout.ms", 5000); try (AdminClient client = KafkaAdminClient.create(properties)) { ListTopicsResult topics = client.listTopics(new ListTopicsOptions().timeoutMs(5000)); Set<String> names = topics.names().get(); System.out.println("connect to kafka cluster success"); return true; } catch (Exception e){ return false; } }
真正的超时,是这一句
new ListTopicsOptions().timeoutMs(5000)
起了重要作用。
From:一号门
Previous:复习二叉树 层序遍历,前序遍历,中序遍历,后序遍历 java版本
Next:备份MySQL并保存两周数据
COMMENTS