ZooKeeper的Java API客户端异常:org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorC

时间:2021-05-28 08:24:52
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /servers/server
at org.apache.zookeeper.KeeperException.create(KeeperException.java:99)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:783)
at cn.itcast.bigdata.zkdist.DistributedServer.registerServer(DistributedServer.java:46)

at cn.itcast.bigdata.zkdist.DistributedServer.main(DistributedServer.java:68)

ZooKeeper的Java API客户端异常:org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorC

网上常见的解决方法是:关闭防火墙

但是防火墙就是处于关闭状态,所以此方法不可用。

经过调试发现 private static final int sessionTimeout = 10000 中设置的sessionTimeout值太小,应增大此值,问题解决。

解释:sessionTimeout是会话超时时间,也就是当一个zookeeper超过该时间没有心跳,则认为该节点故障。所以,如果此值小于zookeeper的创建时间,则当zookeeper还未来得及创建连接,会话时间已到,因此抛出异常认为该节点故障。