使用Java API如何列出Kafka集群中的所有可用代理?

huangapple go评论74阅读模式
英文:

How to list all available brokers in Kafka cluster using Java API?

问题

仅翻译内容如下:

标题所述。

是否有任何方式(使用Kafka Java API)来获取集群中可用的Kafka代理数量?

我知道使用随Kafka服务器一起提供的命令行工具可以完成此操作;但是我还没有找到如何使用Java API来实现这一点。

英文:

What the title says.

Is there anyway (using Kafka Java API) to get the number of AVAILABLE Kafka brokers in a cluster?

I know that with the command line utility that comes with the Kafka server, this can be done; but I haven't found anything on how to do it using Java API.

答案1

得分: 2

你可以从kafkaAdminClient.describeCluster获取代理(broker)和集群信息。

> 使用默认选项获取集群中节点的信息。这是一个方便的方法,用于带有默认选项的#describeCluster(DescribeClusterOptions)。有关更多详细信息,请参阅重载方法。

Properties properties = new Properties();
properties.setProperty("bootstrap.servers", "localhost:9092");
properties.setProperty("client.id", "producerAdmin");
properties.setProperty("metadata.max.age.ms", "3000");
kafkaAdminClient = AdminClient.create(properties);

DescribeClusterResult result = kafkaAdminClient.describeCluster();
KafkaFuture<Collection<Node>> nodes = result.nodes();

您还可以在Node类中使用hostport方法来获取更多信息。

英文:

You can get the broker's and cluster information from kafkaAdminClient.describeCluster

>Get information about the nodes in the cluster, using the default options. This is a convenience method for #describeCluster(DescribeClusterOptions) with default options. See the overload for more details.

Properties properties = new Properties();
properties.setProperty(&quot;bootstrap.servers&quot;, &quot;localhost:9092&quot;);
properties.setProperty(&quot;client.id&quot;, &quot;producerAdmin&quot;);
properties.setProperty(&quot;metadata.max.age.ms&quot;, &quot;3000&quot;);
kafkaAdminClient = AdminClient.create(properties);

DescribeClusterResult result = kafkaAdminClient.describeCluster()
KafkaFuture&lt;Collection&lt;Node&gt;&gt; nodes = result.nodes();

And you can use host,port methods in Node class to get more information

huangapple
  • 本文由 发表于 2020年5月5日 03:35:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61600158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定