如何从kafka群集中检索所有经纪人的详细信息

时间:2018-07-13 07:50:26

标签: java apache-kafka apache-zookeeper

我们应该如何从kafka cluster / zookeeper中检索所有(已连接/已断开连接)代理的完整详细信息?

我发现以下方法只能获取活动的代理,但是我想要以前在群集中服务但现在已断开连接的代理的IP地址

以下代码段提供了活动代理的列表:

ZooKeeper zkInstance = new ZooKeeper("mymachine:port", 10000, null);
brokerIDs = zkInstance.getChildren("/brokers/ids", false);
for (String brokerID : brokerIDs) {
    brokerInfo = new String(zkInstance.getData("/brokers/ids/" + brokerID,     false, null));
    String     host=brokerInfo.substring(brokerInfo.indexOf("\"host\"")).split(",")    [0].replaceAll("\"","").split(":")[1];
    String     port=brokerInfo.substring(brokerInfo.indexOf("\"jmx_port\"")).split(",")    [0].replaceAll("\"","").split(":")[1];
    System.out.println(host+":"+port);              
}

输出:

  • my-machine-1:port
  • my-machine-2:port
  • my-machine-3:port
  • my-machine-4:port

我需要多节点kafka集群中所有已连接/已断开连接的代理的信息

1 个答案:

答案 0 :(得分:0)

使用AdminClient类的describeCluster()获取代理详细信息,例如hostportidrock

请参考以下代码:

Properties kafkaProperties = new Properties();
kafkaProperties.put("bootstrap.servers", "localhost:9092,localhost:9093,localhost:9094");
AdminClient adminClient = AdminClient.create(kafkaProperties);
DescribeClusterResult describeClusterResult = adminClient.describeCluster();
Collection<Node> brokerDetails = describeClusterResult.nodes().get();
System.out.println("host and port details");
for(Node broker:brokerDetails) {
    System.out.println(broker.host()+":"+broker.port());
}