我们应该如何从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集群中所有已连接/已断开连接的代理的信息
答案 0 :(得分:0)
使用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());
}