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

问题描述

我是 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 并询问它存在哪些代理,然后将它们放入属性中?否则总会出现代码的config与集群实际状态不同步的问题.

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 broker会由zookeeper管理,所以你可以访问zookeeper实例来收集kafka broker的信息.

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.

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

10-12 10:04