ITPub博客

首页 > 应用开发 > Java > ZooKeeper服务发现客户端--重连认证

ZooKeeper服务发现客户端--重连认证

原创 Java 作者:壹頁書 时间:2016-06-29 18:00:50 0 删除 编辑
前文:
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博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2121214/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339907