简体   繁体   English

如何在 Java 中以编程方式获取 Kafka Cluster 和 Broker 信息?

[英]How to programmatically get Kafka Cluster and Broker information in Java?

I wish to get all active brokers on my kafka cluster programmatically from the worker itself.我希望以编程方式从工作人员本身获取我的 kafka 集群上的所有活动代理。 The idea is to create a health scheduler that will check and return the active brokers address.这个想法是创建一个健康调度程序,它将检查并返回活动代理地址。

In each worker, I set up the cluster address when I set up the consumer configuration在每个worker中,我在设置consumer配置的时候设置了集群地址

props.put(onsumerConfig.BOOTSTRAP_SERVERS_CONFIG, myServerAddress);

However, this will not tell me which brokers are active at the moment.但是,这不会告诉我目前哪些经纪人是活跃的。

I did some searching, but I could not find any way of getting this info.我进行了一些搜索,但找不到任何获取此信息的方法。 Is it possible?是否可以?

You can retrieve all details of Kafka brokers in a cluster using describeCluster() of AdminClient :您可以使用AdminClient describeCluster()检索集群中 Kafka 代理的所有详细信息:

Get information about the nodes in the cluster.获取有关集群中节点的信息。

Parameters: options - The options to use when getting information about the cluster.参数: options - 获取有关集群的信息时使用的选项。

Returns: The DescribeClusterResult .返回: DescribeClusterResult

// Create AdminClient
Properties props = new Properties();
props.load(new FileInputStream("ac.properties"));
AdminClient adminClient = KafkaAdminClient.create(props);

// Get brokers' details 
DescribeClusterResult describeClusterResult = adminClient.describeCluster();
List<Node> brokers = new ArrayList<>(describeClusterResult.nodes().get());
for (Node broker : brokers) {
    System.out.println("Host=" + broker.host() + ", Port=" + broker.port());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM