尝试创建 zNode 时 Apache Curator 未实现的错误

Apache Curator Unimplemented Errors When Trying to Create zNodes

我正在尝试将 Apache Curator 与 dockerized zookeeper 实例一起使用,无论我如何尝试连接,我总是以

结束

org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for...

错误。我试过理解文档,但我一无所获。我已经登录到 zookeeper CLI 并确保端口号是正确的:

snerd@powerglove:~$ docker ps CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS   NAMES 31f1093495ba        compose_zookeeper   "/opt/zookeeper/bin/   3 weeks ago         Up About a minute   0.0.0.0:32770->2181/tcp,
0.0.0.0:32769->2888/tcp, 0.0.0.0:32768->3888/tcp   zookeeper

这是我尝试使用的代码:

public class App {
    public static void main( String[] args ) {
        CuratorFramework client = CuratorFrameworkFactory.newClient("0.0.0.0:32770", new RetryUntilElapsed(3000, 1000));
        client.start();

            try {
                client.create().forPath("/larry-smells/foop", "tuna?".getBytes());
            } catch (Exception e) {
                System.out.println(e.toString());
            }

    }
}

据我从 Curator getting started page 中得知,这应该可行。我错过了什么?

edit1 刚刚发现我能够从 zookeeper 集合中提取数据:

System.out.println(new String(curatorFramework.getData().forPath("/larry-smells"))); 

但是创建命令仍然在爆炸。

edit2

错误的堆栈跟踪:

org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for /larry-smells/foop 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:1297) at org.apache.curator.framework.imps.CreateBuilderImpl.call(CreateBuilderImpl.java:1040) at org.apache.curator.framework.imps.CreateBuilderImpl.call(CreateBuilderImpl.java:1023) at org.apache.curator.connection.StandardConnectionHandlingPolicy.callWithRetry(StandardConnectionHandlingPolicy.java:67) at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:99) at org.apache.curator.framework.imps.CreateBuilderImpl.pathInForeground(CreateBuilderImpl.java:1020) at org.apache.curator.framework.imps.CreateBuilderImpl.protectedPathInForeground(CreateBuilderImpl.java:501) at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:491) at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:367) at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:309) at com.mycompany.app.App.main(App.java:35)


编辑:显然,如果您将 Curator 与 Zookeeper 结合使用错误,就会发生此错误。来自 curator.apache.org :

Curator 2.x.x - compatible with both ZooKeeper 3.4.x and ZooKeeper 3.5.x

Curator 3.x.x - compatible only with ZooKeeper 3.5.x and includes support for new features such as dynamic reconfiguration, etc.


很难仅通过错误代码而不是堆栈跟踪来查明您的问题,但我建议进行一些改进以使您的应用程序更稳定:

public class App {
    public static void main( String[] args ) {
        CuratorFramework client = CuratorFrameworkFactory.newClient("0.0.0.0:32770", new RetryUntilElapsed(3000, 1000));
        client.start();

        try {
            //make sure you're connected to zookeeper.
            client.blockUntilConnected();

            //Make sure the parents are created.
            client.create().creatingParentsIfNeeded().forPath("/larry-smells/foop", "tuna?".getBytes());
        } catch (Exception e) {
            System.out.println(e.toString());
            }

    }
}

我遇到了同样的问题。

我尝试按照此处的说明使用 inTransaction ():http://www.programcreek.com/java-api-examples/index.php?api=org.apache.curator.framework.CuratorFramework 练习 6 似乎有效。

client.inTransaction ().create().forPath("/larry-smells/foop", "tuna?".getBytes()).and ().commit ();

问题是由于不兼容引起的。

要解决此问题,您需要像此处说明的那样更改版本:
https://curator.apache.org/zk-compatibility.html

如果这不起作用,只需寻找依赖于 3.4.x zookeeper 版本(当前 '2.12.0')的最新 curator 版本。

@Massimo Da Ros 解决方案有效,但在新版本 Curator 4.0.0 inTransaction 中已弃用,建议使用 transaction 方法,如下所示:

CuratorOp op = client.transactionOp().create()
            .withMode(CreateMode.PERSISTENT)
            .withACL(Ids.OPEN_ACL_UNSAFE)
            .forPath("/test", "Data".getBytes());
result = client.transaction().forOperations(op).get(0).toString();

我也遇到了类似的异常,我使用了以下兼容的依赖项并帮助我解决了异常。

    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
    </dependency>

    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>4.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-x-discovery</artifactId>
        <version>4.0.1</version>
    </dependency>

我遇到了类似的问题。我使用的是 spring-cloud-starter-zookeeper-discovery,它本身当然具有兼容的 zookeeper 和 curator 版本。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>

我检查了依赖关系树和 spring-cloud-starter-zookeeper-discovery 版本 3.1.1。正在使用 zookeeper 版本 3.6.0

问题是,在我的 docker-compose.yml 中,我使用的是 zookeeper 版本 3.4!

因此请确保您的 docker-compose.yml zookeeper 版本适合您的 maven zookeeper 版本。

version: "3.8"
services:
  zookeeper:
    container_name: zookeeper
    image: zookeeper:3.6  <----------------- zookeeper version
    ports:
      - "2181:2181"