原因:

一般是由于连接还未完成就执行zookeeper的get/create/exsit操作引起的.

解决方法:

利用"CountDownLatch 类 + zookeeper的watcher + zookeeper的getStat" 实现连接完成后再调用.

可防止此错误发生.


示例类如下(为一配置获取类):

[java]  view plain copy
  1. import java.util.concurrent.CountDownLatch;  
  2.   
  3.   
  4. import org.apache.zookeeper.WatchedEvent;  
  5. import org.apache.zookeeper.Watcher;  
  6. import org.apache.zookeeper.Watcher.Event.KeeperState;  
  7. import org.apache.zookeeper.ZooKeeper;  
  8. import org.apache.zookeeper.ZooKeeper.States;  
  9. import org.apache.zookeeper.data.Stat;  
  10. public class Conf{  
  11.     public static void waitUntilConnected(ZooKeeper zooKeeper, CountDownLatch connectedLatch) {  
  12.         if (States.CONNECTING == zooKeeper.getState()) {  
  13.             try {  
  14.                 connectedLatch.await();  
  15.             } catch (InterruptedException e) {  
  16.                 throw new IllegalStateException(e);  
  17.             }  
  18.         }  
  19.     }  
  20.    
  21.     static class ConnectedWatcher implements Watcher {  
  22.    
  23.         private CountDownLatch connectedLatch;  
  24.    
  25.         ConnectedWatcher(CountDownLatch connectedLatch) {  
  26.             this.connectedLatch = connectedLatch;  
  27.         }  
  28.    
  29.         @Override  
  30.         public void process(WatchedEvent event) {  
  31.            if (event.getState() == KeeperState.SyncConnected) {  
  32.                connectedLatch.countDown();  
  33.            }  
  34.         }  
  35.     }  
  36.     static public Conf Instance(){  
  37.         if(static_ == null){  
  38.             static_ = new Conf();  
  39.         }  
  40.         return static_;  
  41.     }  
  42.     public boolean Init(String hostports, int times){  
  43.         try{  
  44.             CountDownLatch connectedLatch = new CountDownLatch(1);  
  45.             Watcher watcher = new ConnectedWatcher(connectedLatch);  
  46.             zk_ = new ZooKeeper(hostports, times, watcher);  
  47.             waitUntilConnected(zk_, connectedLatch);  
  48.         }  
  49.         catch(Exception e){  
  50.             System.out.println(e);  
  51.             return false;  
  52.         }  
  53.         return true;  
  54.     }  
  55.     public String Get(String keys){  
  56.         String re = "";  
  57.         String ppath = "/zookeeper";  
  58.         int oldpos = -1;  
  59.         int pos = 0;  
  60.         while(true){  
  61.             pos = keys.indexOf(".", oldpos + 1);  
  62.             if(pos < 0){  
  63.                 ppath += "/";  
  64.                 String str = keys.substring(oldpos + 1);  
  65.                 ppath += str;  
  66.                 break;  
  67.             }  
  68.             ppath += "/";  
  69.             String str = keys.substring(oldpos + 1,  pos);  
  70.             ppath += str;  
  71.             oldpos = pos;  
  72.         }  
  73.         Stat stat = new Stat();  
  74.         try{  
  75.             byte[] b = zk_.getData(ppath, false, stat);    //获取节点的信息及存储的数据  
  76.             re = new String(b);  
  77.         }  
  78.         catch(Exception e){  
  79.             System.out.println(e);  
  80.         }  
  81.         return re;  
  82.     }  
  83.     private Conf(){  
  84.           
  85.     }  
  86.     private ZooKeeper zk_;  
  87.     static private Conf static_;  
  88.     public static void main(String args[]){  
  89.         String hostports = "192.168.1.88:2181,192.168.1.88:2182,192.168.1.88:2183";  
  90.           
  91.         Conf.Instance().Init(hostports, 1000);  
  92.           
  93.         String str = Conf.Instance().Get("conf.logicpoint.subscriberserverip");  
  94.         str = Conf.Instance().Get("conf.logicpoint.subscriberserverport");  
  95.         System.out.println(str);  
  96.         while(true){  
  97.             try{Thread.sleep(100);}  
  98.             catch(Exception e){  
  99.                   
  100.             }  
  101.         }  
  102.           
  103.     }  
  104. }  
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐