1. 程式人生 > >ZooKeeper的Java API客戶端異常:org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorC

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

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)


網上常見的解決方法是:關閉防火牆

但是防火牆就是處於關閉狀態,所以此方法不可用。

經過除錯發現 private static final int sessionTimeout = 10000 中設定的sessionTimeout值太小,應增大此值,問題解決。

解釋:sessionTimeout是會話超時時間,也就是當一個zookeeper超過該時間沒有心跳,則認為該節點故障。所以,如果此值小於zookeeper的建立時間,則當zookeeper還未來得及建立連線,會話時間已到,因此丟擲異常認為該節點故障。