前文:
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函数即可.
-
private void connection() {
-
try {
-
this.client = CuratorFrameworkFactory.newClient(meta.getConnectionString(),
-
new RetryUntilElapsed(Integer.MAX_VALUE, 1000));
-
-
client.getConnectionStateListenable().addListener(new ConnectionStateListener(){
-
public void stateChanged(CuratorFramework arg0,
-
ConnectionState arg1) {
-
LOGGER.info("zk client connection state changed:" + arg1.toString());
-
if("RECONNECTED".equals(arg1.toString())){
-
try {
-
arg0.getZookeeperClient().getZooKeeper().addAuthInfo("digest", meta.getReadwritePassword().getBytes());
-
} catch (Exception e) {
-
LOGGER.error(e.getMessage(),e);
-
}
-
}
-
}
-
});
-
-
client.start();
-
client.getZookeeperClient().getZooKeeper().addAuthInfo("digest", meta.getReadOnlyPassword().getBytes());
-
} catch (Exception e) {
-
LOGGER.error(e.getMessage(), e);
-
}
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2121214/
,如需转载,请注明出处,否则将追究法律责任。