当前位置:   article > 正文

KeeperErrorCode = ConnectionLoss 问题解决

keepererrorcode = connectionloss

日志内容如下:

ERROR 19282 — [tor-Framework-0] o.a.c.f.imps.CuratorFrameworkImpl : Background retry gave up
org.apache.curator.CuratorConnectionLossException: KeeperErrorCode = ConnectionLoss

原因:

一般是由于连接还未完成就执行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){
				
			}
		}
		
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/618309
推荐阅读
相关标签
  

闽ICP备14008679号