简体   繁体   English

使用curator在zookeeper中创建一个ttl节点

[英]Create a ttl node in zookeeper using curator

How to create a ttl node using apache curator?如何使用 apache 馆长创建 ttl 节点? I have tried the following我试过以下

ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);
        String connectionString = "127.0.0.1:2181";
        CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
        client.getUnhandledErrorListenable().addListener((message, e) -> {
            System.err.println("error=" + message);
            e.printStackTrace();
        });
        client.getConnectionStateListenable().addListener((c, newState) -> {
            System.out.println("state=" + newState);
        });
        client.start();

Code 1:代码 1:

PersistentTtlNode persistentTtlNode = new PersistentTtlNode(client, "/stores/abc.com", 10000, "".getBytes());
        persistentTtlNode.start();
        boolean flag = persistentTtlNode.waitForInitialCreate(1000, TimeUnit.MICROSECONDS);
        System.out.println(flag);
        persistentTtlNode.close();

Result: Node is not getting created and the flag is false结果:未创建节点且flagfalse

Code 2:代码 2:

client.create().withTtl(1000).creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT_WITH_TTL).forPath("/stores/india.com");

Result: Getting the following exception:结果:得到以下异常:

Exception in thread "main" org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode = Unimplemented for /stores/india.com
    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:1837)
    at org.apache.curator.framework.imps.CreateBuilderImpl$16.call(CreateBuilderImpl.java:1131)
    at org.apache.curator.framework.imps.CreateBuilderImpl$16.call(CreateBuilderImpl.java:1113)
    at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:93)
    at org.apache.curator.framework.imps.CreateBuilderImpl.pathInForeground(CreateBuilderImpl.java:1110)
    at org.apache.curator.framework.imps.CreateBuilderImpl.protectedPathInForeground(CreateBuilderImpl.java:593)
    at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:583)
    at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:561)
    at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:48)
    at Main.main(Main.java:67)

What's the correct way to create a node with ttl in apache curator?在 apache 馆长中使用 ttl 创建节点的正确方法是什么?

Finally found the answer, apparently we need to enable certain configurations while starting zookeeper.终于找到了答案,显然我们需要在启动zookeeper的同时启用某些配置。

  1. Go to conf directory Go 到conf目录
  2. Create a new file called zoo.cfg创建一个名为zoo.cfg的新文件
  3. Paste the following to the file将以下内容粘贴到文件中
tickTime=2000
dataDir=./data/zookeeper
clientPort=2181
maxClientCnxns=60
extendedTypesEnabled=true
emulate353TTLNodes=true

My mistake was I added zookeeper.extendedTypesEnabled in the config file.我的错误是我在配置文件中添加了zookeeper.extendedTypesEnabled Don't add the word zookeeper .不要添加单词zookeeper It's needed only if you are using command line shell ie zkCli.sh .仅当您使用命令行 shell 即zkCli.sh需要它。

Now restart the cluster using bin/zkServer.sh stop and bin/zkServer.sh start .现在使用bin/zkServer.sh stopbin/zkServer.sh start重新启动集群。

The above commands will work fine without throwing any more exceptions.上面的命令可以正常工作而不会抛出任何异常。

Note: If the znode is not modified within the TTL and has no children it will become a candidate to be deleted by the server at some point in the future.注意:如果 znode在 TTL 内没有被修改并且没有子节点,它将成为将来某个时候被服务器删除的候选节点。

Keep in mind, zookeeper version should be > 3.5.3 and the java client version should be the same as zookeeper version请记住,zookeeper 版本应该 > 3.5.3并且 java 客户端版本应该与 zookeeper 版本相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM