赞
踩
最近搭建zookeeper环境跑demo时候遇到一个小问题,记录如下
源码如下
public static void main(String[] args) throws Exception { // CuratorFramework curatorFramework= CuratorFrameworkFactory.newClient("") CuratorFramework curatorFramework = CuratorFrameworkFactory.builder(). connectString(ZookeeperConf.CONNECTION_STRS).sessionTimeoutMs(5000). retryPolicy(new ExponentialBackoffRetry(1000, 3)).build(); //ExponentialBackoffRetry //RetryOneTime 仅仅只重试一次 //RetryUntilElapsed //RetryNTimes curatorFramework.start(); //启动 createData(curatorFramework); // updateData(curatorFramework); // deleteData(curatorFramework); //CRUD curatorFramework.create(); // curatorFramework.setData(); //修改 // curatorFramework.delete() ;// 删除 // curatorFramework.getData(); //查询 } private static void createData(CuratorFramework curatorFramework) throws Exception { System.out.println("开始创建节点 /data/program"); curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT). forPath("/data/program", "zookeeper—test".getBytes()); } private static void updateData(CuratorFramework curatorFramework) throws Exception { curatorFramework.setData().forPath("/data/program", "up".getBytes()); } private static void deleteData(CuratorFramework curatorFramework) throws Exception { Stat stat = new Stat(); String value = new String(curatorFramework.getData().storingStatIn(stat).forPath("/data/program")); curatorFramework.delete().withVersion(stat.getVersion()).forPath("/data/program"); }
执行后报错如下
Exception in thread "main" org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for /data/program
at org.apache.zookeeper.KeeperException.create(KeeperException.java:103)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:1525)
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:47)
at com.iris.springboot_all.zookeeper.base.CuratorDemo.createData(CuratorDemo.java:38)
at com.iris.springboot_all.zookeeper.base.CuratorDemo.main(CuratorDemo.java:25)
通常这是zookeeper客户端和服务端版本不一致造成的,因zookeeper环境是之前大数据环境已经存在的直接共用。
所以首先检查pom文件及客户端版本
- <!--zookeeper-->
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>4.0.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>4.0.0</version>
- </dependency>
设备上版本为:3.4.10
自身pom.xml中的版本为 4.0.0
Curator官网:curator4.0与zookeeper3.4.X有依赖冲突,需要exclude其zookeeper附属依赖,并重新引入zookeeper版本
修改后pom.xml为
- <!--zookeeper-->
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-framework</artifactId>
- <version>${curator-version}</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.curator</groupId>
- <artifactId>curator-recipes</artifactId>
- <version>${curator-version}</version>
- <exclusions>
- <exclusion>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.4.10</version>
- </dependency>
再次执行后成功
最新版本的curator有些特性在3.4.X版本中是不支持的,为了确保代码的正确,最好加入版本模式的判断,使用isZk34CompatibilityMode()方法进行判断即可。
升级zookeeper集群即可,如果不太方便升级则采用方法一。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。