ZooKeeper Connection Loss 异常
来源:互联网 发布:中国制裁挪威知乎 编辑:程序博客网 时间:2024/04/26 02:50
原因:
一般是由于连接还未完成就执行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 Connection Loss 异常
- 【zookeeper】zookeeper集群配置异常 Connection refused 和 myid file is missing
- Fragment提交transaction导致state loss异常
- Fragment提交transaction导致state loss异常
- Fragment提交transaction导致state loss异常
- Fragment提交transaction导致state loss异常
- Fragment提交transaction导致state loss异常
- Fragment提交transaction导致state loss异常
- Fragment提交transaction导致state loss异常
- Fragment提交transaction导致state loss异常
- Fragment提交transaction导致state loss异常
- 连接不到 Linux 上的 zookeeper 异常:Session 0x0 for server null,unexpected error, closing socket connection a
- zookeeper异常处理
- Zookeeper异常ConnectionLossException解决
- Zookeeper异常ConnectionLossException解决
- zookeeper启动异常
- 项目连接zookeeper异常
- zookeeper连接超时异常
- Linux下安装OpenCV
- C语言中的sizeof解析
- 使用C#开发ActiveX控件(新)
- svn: Commit failed: svn: File '' is out of date
- 在VM上安装centOS后的网络配置
- ZooKeeper Connection Loss 异常
- VI的使用
- Java的内存泄漏
- ExternalInterface类的使用
- Html5游戏开发-145行代码完成一个RPG小Demo
- 用缓冲区方式复制一个文本
- Rossum--自主运动的秘密
- shell date 用法总结
- istringstream对象可以绑定一行字符串,然后以空格为分隔符把改行分隔开来