如何检查Kafka Server是否正在运行? [英] How to check whether Kafka Server is running?
问题描述
我想在开始生产和消费工作之前确保kafka服务器是否正在运行。它是在windows环境中,这是我的kafka服务器在eclipse中的代码...
I want to ensure whether kafka server is running or not before starting production and consumption jobs. It is in windows environment and here's my kafka server's code in eclipse...
Properties kafka = new Properties();
kafka.setProperty("broker.id", "1");
kafka.setProperty("port", "9092");
kafka.setProperty("log.dirs", "D://workspace//");
kafka.setProperty("zookeeper.connect", "localhost:2181");
Option<String> option = Option.empty();
KafkaConfig config = new KafkaConfig(kafka);
KafkaServer server = new KafkaServer(config, new CurrentTime(), option);
server.startup();
在这种情况下 if(server!= null)
还不够,因为它总是如此。那么有没有办法知道我的kafka服务器正在运行并为生产者做好准备。我有必要检查一下,因为它会导致丢失一些起始数据包。
In this case if (server != null)
is not enough because it is always true. So is there any way to know that my kafka server is running and ready for producer. It is necessary for me to check this because it causes loss of some starting data packets.
谢谢。
推荐答案
必须为所有Kafka经纪人分配 broker.id
。启动时,代理将在Zookeeper中创建一个短暂的节点,路径为 / broker / ids / $ id
。由于节点是短暂的,所以一旦代理断开连接,它将被移除,例如,通过关闭。
All Kafka brokers must be assigned a broker.id
. On startup a broker will create an ephemeral node in Zookeeper with a path of /broker/ids/$id
. As the node is ephemeral it will be removed as soon as the broker disconnects, e.g. by shutting down.
您可以查看短暂的代理节点列表,如下所示:
You can view the list of the ephemeral broker nodes like so:
echo dump | nc localhost 2181 | grep brokers
ZooKeeper客户端接口公开了许多命令; dump
列出集群的所有会话和临时节点。
The ZooKeeper client interface exposes a number of commands; dump
lists all the sessions and ephemeral nodes for the cluster.
注意,上面假设:
- 您在
上的默认端口(
,而2181
)上运行ZooKeeper localhostlocalhost
是群集的领导者 - 您的
zookeeper .connect
Kafka config没有为你的Kafka集群指定一个chroot env,即它只是host:port
而不是host :port / path
- You're running ZooKeeper on the default port (
2181
) onlocalhost
, and thatlocalhost
is the leader for the cluster - Your
zookeeper.connect
Kafka config doesn't specify a chroot env for your Kafka cluster i.e. it's justhost:port
and nothost:port/path
这篇关于如何检查Kafka Server是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!