ZooKeeper服務發現客戶端--重連認證

壹頁書發表於2016-06-29
前文:
http://blog.itpub.net/29254281/viewspace-2108223/

挖坑小王子

這裡面有大坑.
由於線上都配置了ACL控制
前文的程式碼,在Reconnection的時候,會有如下異常.

2016-06-23 22:13:01 [ main-EventThread:23807640 ] - [ INFO ] State change: RECONNECTED
2016-06-23 22:13:01 [ ConnectionStateManager-0:23807641 ] - [ INFO ] zk connection state changed:RECONNECTED
2016-06-23 22:13:02 [ pool-1-thread-1:23807734 ] - [ ERROR ] KeeperErrorCode = NoAuth for /service
org.apache.zookeeper.KeeperException$NoAuthException: KeeperErrorCode = NoAuth for /service
        at org.apache.zookeeper.KeeperException.create(KeeperException.java:113)
        at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
        at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:783)
        at org.apache.curator.framework.imps.CreateBuilderImpl$11.call(CreateBuilderImpl.java:676)
        at org.apache.curator.framework.imps.CreateBuilderImpl$11.call(CreateBuilderImpl.java:660)
        at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107)
        at org.apache.curator.framework.imps.CreateBuilderImpl.pathInForeground(CreateBuilderImpl.java:657)
        at org.apache.curator.framework.imps.CreateBuilderImpl.protectedPathInForeground(CreateBuilderImpl.java:441)
        at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:431)
        at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:44)
        at com.vv.zkClient.ServiceProvider.run(ServiceProvider.java:69)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)

這是因為重連的時候,沒有認證資訊.
修改connection函式即可.
  1. private void connection() {  
  2.     try {  
  3.         this.client = CuratorFrameworkFactory.newClient(meta.getConnectionString(),  
  4.                 new RetryUntilElapsed(Integer.MAX_VALUE, 1000));  
  5.           
  6.         client.getConnectionStateListenable().addListener(new ConnectionStateListener(){  
  7.             public void stateChanged(CuratorFramework arg0,  
  8.                     ConnectionState arg1) {  
  9.                 LOGGER.info("zk client connection state changed:" + arg1.toString());  
  10.                 if("RECONNECTED".equals(arg1.toString())){  
  11.                     try {  
  12.                         arg0.getZookeeperClient().getZooKeeper().addAuthInfo("digest", meta.getReadwritePassword().getBytes());  
  13.                     } catch (Exception e) {  
  14.                         LOGGER.error(e.getMessage(),e);  
  15.                     }  
  16.                 }  
  17.             }  
  18.         });  
  19.           
  20.         client.start();  
  21.         client.getZookeeperClient().getZooKeeper().addAuthInfo("digest", meta.getReadOnlyPassword().getBytes());  
  22.     } catch (Exception e) {  
  23.         LOGGER.error(e.getMessage(), e);  
  24.     }  
  25. }  


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2121214/,如需轉載,請註明出處,否則將追究法律責任。

相關文章