前言今天在调用很久写的zk相关代码 创建节点突然创建异常。。。异常信息如下
org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for /test
at org.apache.zookeeper.KeeperException.create(KeeperException.java:106)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:54)
at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:1637)
at org.apache.curator.framework.imps.CreateBuilderImpl$17.call(CreateBuilderImpl.java:1189)
at org.apache.curator.framework.imps.CreateBuilderImpl$17.call(CreateBuilderImpl.java:1166)
at org.apache.curator.connection.StandardConnectionHandlingPolicy.callWithRetry(StandardConnectionHandlingPolicy.java:64)
at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:100)
at org.apache.curator.framework.imps.CreateBuilderImpl.pathInForeground(CreateBuilderImpl.java:1163)
at org.apache.curator.framework.imps.CreateBuilderImpl.protectedPathInForeground(CreateBuilderImpl.java:603)
at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:593)
at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:571)
at org.apache.curator.framework.imps.CreateBuilderImpl$4.forPath(CreateBuilderImpl.java:459)
at org.apache.curator.framework.imps.CreateBuilderImpl$4.forPath(CreateBuilderImpl.java:389)
at com.google.zookeeper.lock.DistributeLock.main(DistributeLock.java:29)
简单地创建代码如下
//初始化 CuratorFramework framework = CuratorFrameworkFactory.builder().connectString("zk服务地址") .connectionTimeoutMs(5000) .retryPolicy(new ExponentialBackoffRetry(3000, 3)) .build(); framework.start(); try { String s = framework.create().creatingParentsIfNeeded().forPath("/test"); System.out.println("s=="+s); } catch (Exception e) { e.printStackTrace(); }
看到错误就不淡定了。。。 直接度娘一发 发现是curator 和 zookeper 版本兼容问题
看下了下官方给的版本对比文档 官方文档链接
zookeeper3.5.x 是 curator4.0 很支持 不要任何操作
Curator 4.0 supports ZooKeeper 3.4.x ensembles in a soft-compatibility mode. To use this mode you must exclude ZooKeeper when adding Curator to your dependency management tool.
curator4.0 对zk 3.4.x 属于软兼容需要配置
org.apache.curator curator-recipes 4.0.0 org.apache.zookeeper zookeeper
解决办法
第一次尝试先将 curator-recipes中zk依赖排除 zk版本使用3.5.5 服务器zk版本3.4.12 curator 4.0.0 不好使
org.apache.zookeeper zookeeper 3.5.5 org.slf4j slf4j-api org.slf4j slf4j-log4j12 org.apache.curator curator-framework 4.0.0 org.apache.curator curator-recipes 4.0.0 org.apache.zookeeper zookeeper
第二次尝试将 curator-recipes中zk依赖排除 zk版本使用和服务器上一一致的3.4.12 服务器上zk版本3.4.12 curator 4.0.0 已经解决
org.apache.zookeeper zookeeper 3.4.12 org.slf4j slf4j-api org.slf4j slf4j-log4j12 org.apache.curator curator-framework 4.0.0 org.apache.curator curator-recipes 4.0.0 org.apache.zookeeper zookeeper
第三种curator-recipes中zk依赖排除 zk版本使用和服务器上一一致的3.4.12 服务器上zk版本3.4.12 curator 版本进行降级 使用2.12.0 已经解决
org.apache.zookeeper zookeeper 3.4.12 org.slf4j slf4j-api org.slf4j slf4j-log4j12 org.apache.curator curator-framework 2.12.0 org.apache.curator curator-recipes 2.12.0 org.apache.zookeeper zookeeper
最终解决方法 1.服务器上版本3.5.X zk也使用3.5.5 curator 4.0.0 没问题
2.服务器上版本3.4.x zk版本和服务器zk版本一致 curator 4.0.0 依赖中排除zk依赖
3.服务器上版本3.4.x zk版本和服务器zk版本一致 curator使用2.x.x 也可以解决 具体需要自己尝试版本 (不推荐使用,可能导致兼容问题 如之前已经使用的4.0.0的东西 2.xx的 无法满足)