【问题标题】:how to watch the sub-znodes of znodes please?请问如何查看znodes的子znodes?
【发布时间】:2017-09-20 07:53:38
【问题描述】:

我使用 Curator 对 Zookeeper 进行编程。我想观察 zonde 的子 znode。我的代码如下所示:

CuratorFramework frameWork = CuratorFrameworkFactory.builder()
            .retryPolicy(new RetryNTimes(3, 1000))
            .connectString("127.0.0.1:2181").build();
frameWork.start();

CuratorWatcher watcher = new CuratorWatcher () {

            @Override
            public void process(WatchedEvent event) throws Exception
            {
                System.out.println("event :" + event);
            }

        };

frameWork.getChildren().usingWatcher(watcher).forPath("/aa"); frameWork.setData().forPath("/aa/dd", "asdfd".getBytes());

当调用“/aa/dd”的setdata时,我希望触发观察者。但是失败了,请问有人知道吗?

【问题讨论】:

    标签: apache apache-zookeeper apache-curator


    【解决方案1】:

    Curator 有一个可以做到这一点的食谱。请看TreeCache:http://curator.apache.org/curator-recipes/tree-cache.html

    【讨论】:

    • 我知道这个食谱。我想知道为什么我的代码不起作用。我想我看“/aa”的孩子,所有关于“/aa”孩子的变化都会触发手表。但事实并非如此。
    • ZooKeeper 有几种类型的观察者。为了监视数据变化,您必须首先使用getData() 设置监视。正确使用 ZooKeeper 非常困难,这也是最初创建 Curator 的原因。我强烈建议您使用 Curator 的预构建配方之一,而不是尝试直接编写 ZooKeeper 代码。
    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多