gpt4 book ai didi

java - 尝试从 zookeeper 节点 : (KeeperErrorCode: ConnectionLoss ) 获取数据时出现异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:06 24 4
gpt4 key购买 nike

我正在使用以下代码从 zookeeper 中提取 Kafka 代理列表:

private static String getBrokerList() {
try {
ZooKeeper zookeeper = new ZooKeeper(zookeeperConnect, 15000, null);
List<String> ids = zookeeper.getChildren(ZkUtils.BrokerIdsPath(), false);
List<String> brokerList = new ArrayList<>();
for (String id : ids) {
String brokerInfo = new String(zookeeper.getData(ZkUtils.BrokerIdsPath() + '/' + id, false, null), Charset.forName("UTF-8"));
JsonObject jsonElement = new JsonParser().parse(brokerInfo).getAsJsonObject();
String host = jsonElement.get("host").getAsString();
brokerList.add(host + ':' + jsonElement.get("port").toString());
}
return Joiner.on(",").join(brokerList);
} catch (KeeperException | InterruptedException e) {
Throwables.propagate(e);
}
return "";
}

当一个线程一次执行代码时,上面的代码工作正常。但是,当多个线程正在执行上述代码时,它偶尔会失败并出现以下异常:

Caused by: org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /brokers/ids
at org.apache.zookeeper.KeeperException.create(KeeperException.java:99)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
at org.apache.zookeeper.ZooKeeper.getChildren(ZooKeeper.java:1532)
at org.apache.zookeeper.ZooKeeper.getChildren(ZooKeeper.java:1560)

我在这里做错了什么?我的 zookeeper 版本是 3.4.6-1569965。

最佳答案

来自 http://zookeeper.apache.org/doc/r3.4.9/api/org/apache/zookeeper/ZooKeeper.html#ZooKeeper(java.lang.String,%20int,%20org.apache.zookeeper.Watcher)

“ session 建立是异步的。此构造函数将启动与服务器的连接并立即返回 - 可能(通常)在 session 完全建立之前。watcher 参数指定将收到状态任何更改通知的观察者。此通知可以在构造函数调用返回之前或之后的任何时间出现。”

您必须等待 zookeeper 连接才能完全建立: https://www.tutorialspoint.com/zookeeper/zookeeper_quick_guide.htm

向下滚动到 api 部分“连接到 ZooKeeper Ensemble”

关于java - 尝试从 zookeeper 节点 : (KeeperErrorCode: ConnectionLoss ) 获取数据时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42207445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com