簡體   English   中英

如何從kafka集群中檢索所有經紀人的詳細信息

[英]How to retrieve details of all broker from kafka cluster

我們應該如何從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);              
}

輸出:

  • 我的機器1:端口
  • 我的機器2:端口
  • 我的機器3:端口
  • 我的機器4:端口

我需要多節點kafka集群中所有已連接/已斷開連接的代理的信息

使用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());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM