[英]How to programmatically get Kafka Cluster and Broker information in Java?
[英]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);
}
輸出:
我需要多節點kafka集群中所有已連接/已斷開連接的代理的信息
使用AdminClient類的describeCluster()
獲取代理詳細信息,例如host
, port
, id
和rock
。
請參考以下代碼:
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.