【Zookeeper】zookeeper客戶端KeeperErrorCode = ConnectionLoss
1.概述
今天執行進去zk shell想執行一個命令,結果直接報錯
zookeeper】bin/zkCli.sh
0] ls /
然後直接報錯如下
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
at org.apache.zookeeper.KeeperException.create(KeeperException.java:99)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
at org.apache.zookeeper.ZooKeeper.exists(ZooKeeper.java:1045)
at org.apache.zookeeper.ZooKeeper.exists(ZooKeeper.java:1073)
at com.taobao.pamirs.schedule.zk.ScheduleStrategyDataManager4ZK.loadManagerFactoryInfo(ScheduleStrategyDataManager4ZK.java:295)
at com.taobao.pamirs.schedule.strategy.TBScheduleManagerFactory.refresh(TBScheduleManagerFactory.java:164)
at com.taobao.pamirs.schedule.strategy.ManagerFactoryTimerTask.run(TBScheduleManagerFactory.java:438)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
2.原因
原因:
一般是由於連線還未完成就執行zookeeper的get/create/exsit操作引起的.
解決方法:
利用"CountDownLatch 類 + zookeeper的watcher + zookeeper的getStat" 實現連線完成後再呼叫.
可防止此錯誤發生.
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooKeeper.States;
import org.apache.zookeeper.data.Stat;
public class Conf{
public static void waitUntilConnected(ZooKeeper zooKeeper, CountDownLatch connectedLatch) {
if (States.CONNECTING == zooKeeper.getState()) {
try {
connectedLatch.await();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}
static class ConnectedWatcher implements Watcher {
private CountDownLatch connectedLatch;
ConnectedWatcher(CountDownLatch connectedLatch) {
this.connectedLatch = connectedLatch;
}
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connectedLatch.countDown();
}
}
}
static public Conf Instance(){
if(static_ == null){
static_ = new Conf();
}
return static_;
}
public boolean Init(String hostports, int times){
try{
CountDownLatch connectedLatch = new CountDownLatch(1);
Watcher watcher = new ConnectedWatcher(connectedLatch);
zk_ = new ZooKeeper(hostports, times, watcher);
waitUntilConnected(zk_, connectedLatch);
}
catch(Exception e){
System.out.println(e);
return false;
}
return true;
}
public String Get(String keys){
String re = "";
String ppath = "/zookeeper";
int oldpos = -1;
int pos = 0;
while(true){
pos = keys.indexOf(".", oldpos + 1);
if(pos < 0){
ppath += "/";
String str = keys.substring(oldpos + 1);
ppath += str;
break;
}
ppath += "/";
String str = keys.substring(oldpos + 1, pos);
ppath += str;
oldpos = pos;
}
Stat stat = new Stat();
try{
byte[] b = zk_.getData(ppath, false, stat); //獲取節點的資訊及儲存的資料
re = new String(b);
}
catch(Exception e){
System.out.println(e);
}
return re;
}
private Conf(){
}
private ZooKeeper zk_;
static private Conf static_;
public static void main(String args[]){
String hostports = "192.168.1.88:2181,192.168.1.88:2182,192.168.1.88:2183";
Conf.Instance().Init(hostports, 1000);
String str = Conf.Instance().Get("conf.logicpoint.subscriberserverip");
str = Conf.Instance().Get("conf.logicpoint.subscriberserverport");
System.out.println(str);
while(true){
try{Thread.sleep(100);}
catch(Exception e){
}
}
}
}
相關文章
- 【zookeeper之七】Zookeeper客戶端客戶端
- Zookeeper 客戶端 API客戶端API
- Zookeeper Java 客戶端搭建Java客戶端
- ZooKeeper客戶端CRUD命令客戶端
- Zookeeper--Java客戶端Java客戶端
- zookeeper的Java客戶端APIJava客戶端API
- Zookeeper C客戶端庫編譯客戶端編譯
- zookeeper 開源客戶端ZkClient使用客戶端client
- zookeeper 開源客戶端Curator使用客戶端
- Zookeeper--客戶端基礎命令客戶端
- ZooKeeper服務發現客戶端客戶端
- 聊一聊 Zookeeper 客戶端之 Curator客戶端
- Zookeeper客戶端使用與叢集原理客戶端
- Zookeeper之Zookeeper底層客戶端架構實現原理(轉載)客戶端架構
- zookeeper在Linux客戶端操作命令大全Linux客戶端
- zookeeper Java客戶端API的使用方法Java客戶端API
- ZooKeeper客戶端事件序列化處理客戶端事件
- ZooKeeper服務發現客戶端--重連認證客戶端
- zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for {root.path}ExceptionError
- Zookeeper之Curator(1)客戶端基本的建立,刪除,更新,查詢操作api客戶端API
- Zookeeper-Zookeeper clientclient
- Zookeeper之Curator(1)客戶端對節點的一些監控事件的api使用客戶端事件API
- 【zookeeper】zookeeper分散式鎖分散式
- 【Zookeeper】zookeeper叢集安裝
- Zookeeper之Zookeeper的Client的分析client
- Zookeeper原始碼分析(二) —– zookeeper日誌原始碼
- ZooKeeper 系列(一)—— ZooKeeper核心概念詳解
- zookeeper學習04 zookeeper收尾+dubbo前瞻
- ZooKeeper 06 - ZooKeeper 的常用命令
- Zookeeper系列一:Zookeeper基礎命令操作
- Zookeeper原始碼分析(二) ----- zookeeper日誌原始碼
- ZooKeeper管理員指南——部署與管理ZooKeeper
- zookeeper理解
- Zookeeper流程
- zookeeper初探
- zookeeper原理
- zookeeper:spark-project專案的zookeeper配置SparkProject
- [Zookeeper-01]什麼是zookeeper?& Linux / Windows Zookeeper安裝和部署(單點)LinuxWindows