我正在编写一个使用ZooKeeper的应用程序,并且我经常遇到CONNECTIONLOSS
个事件。我使用2000
作为我的投注时间,12000
作为我的会话超时,并且应用程序在Amazon EC2中运行。我相信我在使用之前等待ZooKeeper连接;这是我用来执行此操作的代码:
ZooKeeper zk=new ZooKeeper(connectionString, sessionTimeout, new Watcher() {
@SuppressWarnings("deprecation")
public void process(WatchedEvent event) {
switch(event.getState()) {
case SyncConnected:
case ConnectedReadOnly:
// We're connected, count down...
ready.countDown();
break;
case SaslAuthenticated:
// Life is good here. Nothing to do...
break;
case AuthFailed:
case Disconnected:
case Expired:
case NoSyncConnected:
case Unknown:
default:
LOGGER.error("ZooKeeper event: "+event.getState());
break;
}
}
});
boolean ok;
try {
ok = ready.await(15, TimeUnit.SECONDS);
}
catch(InterruptedException e) {
throw new InterruptedIOException();
}
if(ok == false)
throw new IOException("Failed to connect to ZooKeeper");
为什么ZooKeeper可能会收到这些虚假的CONNECTIONLOSS
事件?
答案 0 :(得分:1)
在AWS上运行时,我们曾经遇到类似的ZK问题。我们通过确保所有EC2实例都使用网络时间协议(NTP)运行来解决这些问题。如果你的实例有时间偏差,似乎导致他们对超时感到困惑。