动态获取经纪人列表 [英] Getting the list of Brokers Dynamically

查看:111
本文介绍了动态获取经纪人列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Kafka的新手.我是这样写我的第一个消息产生者的

I am new to Kafka. I wrote my first message producer like this

private Properties kafkaProps = new Properties();
kafkaProps.put("bootstrap.servers", "broker1:9092,broker2:9092");
kafkaProps.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
kafkaProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
producer = new KafkaProducer<String, String>(kafkaProps);

我是否有可能引导至Kafka并询问存在哪些经纪人,然后将其放入属性中?否则,总会出现代码配置与集群的实际状态不同步的问题.

is it possible that I bootstrap to Kafka and ask it what brokers are present and then put them in the properties? Otherwise there will always be a problem that the config of the code is out of sync with the actual state of the cluster.

推荐答案

通常,kafka代理将由zookeeper管理,因此您可以访问zookeeper实例以收集有关kafka代理的信息.

Normally, kafka brokers will be managed by a zookeeper, so you can access the zookeeper instance to gather information about kafka brokers.

例如,您可以使用 org.apache.storm.shade.org.apache.zookeeper.ZooKeeper 实例连接到Zookeeper,并运行其 getChildren(pathToBrokerIds,false)方法,该方法返回kafka经纪人ID的列表.然后,您可以运行Zookeeper的 getData(..)方法(每个id作为参数),并获取该代理的信息,包括主机和端口.

You can use, for example, a org.apache.storm.shade.org.apache.zookeeper.ZooKeeper instance to connect to Zookeeper, and run its getChildren(pathToBrokerIds, false) method which returns a list of kafka brokers' ids. Then you can run zookeeper's getData(..) method with each id as an argument, and get the info for that broker, including host and port.

这篇关于动态获取经纪人列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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