【问题标题】:how to use zookeeper with curator for configuration managment?如何使用 Zookeeper 和 curator 进行配置管理?
【发布时间】:2014-05-14 14:35:15
【问题描述】:

我一直在阅读有关使用 zookeeper for configuration managment 的信息。

我知道有 Apache Curator 可以轻松与 zookeeper 交互。

我有一些客户端连接到一组资源。他们必须以同样的方式使用这些资源。资源之间的分片和主选举之类的东西。

我想使用zookeeper,这样如果客户端在给定时间内注意到其中一个资源已关闭,它可以更改配置,其他客户端可以立即开始使用新配置。

因此,一个客户端将配置写入 zookeeper 中 znode 的路径中,而其他客户端正在监视该路径。

如何在curator中设置数据:

zk.setData().forPath(pathName, "data".getBytes());

如何在curator中查看路径:

zk.getData().usingWatcher(watcher).forPath(pathName);

现在,当路径的值发生变化并且手表被触发时,我必须获取路径的新值。 我该怎么做?

这总是在 proccess() 中返回 null

zk.getData().watched().inBackground().forPath(pathName)

其他问题:As it says in the documentation,我拿到新值后,是不是还要重新设置watcher?

观察者来源:

CuratorWatcher watcher = new CuratorWatcher(){

                public void process(WatchedEvent event) {
                    System.out.println("event wt");
                    System.out.println(ToStringBuilder.reflectionToString(event));
                    try {
                        System.out.println(zk.getData().watched().inBackground().forPath(pathName));
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    };
                    System.out.println("event wt");

                    try {
                        zk.getData().usingWatcher(this).forPath(pathName);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }};

【问题讨论】:

  • 与其编写自己的食谱,我建议您使用预先构建的 Curator 食谱之一。尤其是 PathChildrenCache 会帮助你。

标签: apache-zookeeper configuration-management apache-curator


【解决方案1】:

在 Curator 中有适配器:

 * Get children and set the given watcher on the node.
         */
 return client.getChildren().usingWatcher(watcher).forPath(path);

或者你可以使用 CuratorListener

/**
         * Get children and set a watcher on the node. The watcher notification will come through the
         * CuratorListener (see setDataAsync() above).
         */
        return client.getChildren().watched().forPath(path);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 2014-10-04
    • 2018-09-15
    • 1970-01-01
    • 2011-01-16
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多