Apache Curator Node Cache Watcher

只能监听某一路径本身的add,delete,update

1.run NodeListener

2.run NLTest

package com.collonn.javaUtilMvn.zookeeper.curator.NodeCache;

public class NLTest {
    public static void main(String[] args) throws Exception {
        NLClientCreate.main(null);
        Thread.sleep(1000 * 2);
        NLClientUpdate.main(null);
        Thread.sleep(1000 * 2);
        NLClientDelete.main(null);
    }
}
package com.collonn.javaUtilMvn.zookeeper.curator.NodeCache;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.EnsurePath;

import java.util.List;

public class NodeListener {
    public static final String C_PATH = "/TestNode";
    public static final String CHARSET = "UTF-8";

    public static void main(String[] args) {
        try {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        String zookeeperConnectionString = "127.0.0.1:2181";
                        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                        CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                        client.start();

                        final NodeCache nodeCache = new NodeCache(client, C_PATH);
                        nodeCache.getListenable().addListener(new NodeCacheListener() {
                            @Override
                            public void nodeChanged() throws Exception {
                                System.out.println("================== catch node data change ==================");
                                ChildData childData = nodeCache.getCurrentData();
                                if(childData == null){
                                    System.out.println("===delete, path=" + C_PATH + ", childData=" + childData);
                                }else{
                                    System.out.println("===update or add, path=" + C_PATH + ", childData=" + new String(childData.getData(), CHARSET));
                                }
                            }
                        });
                        nodeCache.start();

                        Thread.sleep(Integer.MAX_VALUE);
                        client.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
package com.collonn.javaUtilMvn.zookeeper.curator.NodeCache;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.util.Random;

public class NLClientCreate {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String zookeeperConnectionString = "127.0.0.1:2181";
                    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                    client.start();

                    Stat stat = client.checkExists().forPath(NodeListener.C_PATH);
                    if (stat == null) {
                        client.create().withMode(CreateMode.PERSISTENT).forPath(NodeListener.C_PATH, "-1".getBytes(NodeListener.CHARSET));
                    }

                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
package com.collonn.javaUtilMvn.zookeeper.curator.NodeCache;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;

import java.util.Random;

public class NLClientUpdate {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String zookeeperConnectionString = "127.0.0.1:2181";
                    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                    client.start();

                    Stat stat = client.checkExists().forPath(NodeListener.C_PATH);
                    if (stat != null) {
                        client.setData().forPath(NodeListener.C_PATH, "128".getBytes(NodeListener.CHARSET));
                    }

                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
package com.collonn.javaUtilMvn.zookeeper.curator.NodeCache;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.util.Random;

public class NLClientDelete {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String zookeeperConnectionString = "127.0.0.1:2181";
                    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
                    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
                    client.start();

                    Stat stat = client.checkExists().forPath(NodeListener.C_PATH);
                    if (stat != null) {
                        client.delete().forPath(NodeListener.C_PATH);
                    }

                    client.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

时间: 2024-10-20 05:20:40

Apache Curator Node Cache Watcher的相关文章

Apache Curator Path Cache Watcher

可以监控某一路径的直接子结点(一级子结点)变化,add,update,delete. 利用此特性可以很方便的监控集群中的所有结点,当然也就很方便的可以实现简单的key.hashCode()%serverCount式的分布式计算,还可以实现简单的定制规则的负载均衡. 1.run ChildrenListener 2.run CLTest package com.collonn.javaUtilMvn.zookeeper.curator.PathCache; public class CLTest

Apache Curator Tree Cache Watcher

可以监控某一路径下的子结点(所有子结节,不管有多少层子结点)变化. 比NodeCache方便的是,可以监听一群结点,而不用一个节点一个节点的去设置监听 1.run TreeListener 2.run TLTest package com.collonn.javaUtilMvn.zookeeper.curator.TreeCache; public class TLTest { public static void main(String[] args) throws Exception { T

使用Apache Curator监控Zookeeper的Node和Path的状态

1.Zookeeper经常被我们用来做配置管理,配置的管理在分布式应用环境中很常见,例如同一个应用系统需要多台 PC Server 运行,但是它们运行的应用系统的某些配置项是相同的,如果要修改这些相同的配置项,那么就必须同时修改每台运行这个应用系统的 PC Server,这样非常麻烦而且容易出错.像这样的配置信息完全可以交给 Zookeeper 来管理,将配置信息保存在 Zookeeper 的某个目录节点中,然后将所有需要修改的应用机器监控配置信息的状态,一旦配置信息发生变化,每台应用机器就会收

Apache Curator入门实战

版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Apache Curator入门实战 Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeeper客户端的开发量. 1.Zookeeper安装部署 Zookeeper的部署很简单,如果已经有Java运行环境的话,下载tarball解压后即可运行. [root@vm Temp]$ wget http://mirror.bi

Apache Curator操作zookeeper的API使用

curator简介与客户端之间的异同点 常用的zookeeper java客户端: zookeeper原生Java API zkclient Apache curator ZooKeeper原生Java API的不足之处: 在连接zk超时的时候,不支持自动重连,需要手动操作 Watch注册一次就会失效,需要反复注册 不支持递归创建节点 Apache curator: Apache 的开源项目 解决Watch注册一次就会失效的问题 提供的 API 更加简单易用 提供更多解决方案并且实现简单,例如:

Apache Curator Leader Election

用于Leader选举,也可以用Shared Reentrant Lock来实现. 如果需要集群中的固定的一台机器去做的事,就可以用此特性来实现,直到这台Leader死去,会产生新的Leader. 1.直接运行LLtest package com.collonn.javaUtilMvn.zookeeper.curator.LeaderElection; import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NLClientCreate

Tomcat8 启动中提示 org.apache.catalina.webresources.Cache.getResource Unable to add the resource

Tomcat8 启动过程中提示: org.apache.catalina.webresources.Cache.getResource Unable to add the resource at xxx to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the

15. 使用Apache Curator管理ZooKeeper

Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. 社区已经在ZooKeeper数据模型及其API之上开发了高级框架. Apache Curator是一个高级的包装类库和框架,使得ZooKeeper非常简单易用. Tips Curator最初由Netflix开发,现在是一个Apache项目. 项目页面位于http://curator.apache.

【zookeeper】Apache curator的使用及zk分布式锁实现

上篇,本篇主要讲Apache开源的curator的使用,有了curator,利用Java对zookeeper的操作变得极度便捷. 其实在学之前我也有个疑虑,我为啥要学curator,撇开涨薪这些外在的东西,就单技术层面来讲,学curator能帮我做些什么?这就不得不从zookeeper说起,上篇我已经大篇幅讲了zk是做什么的了,但真正要靠zk去实现多服务器自动拉取更新的配置文件等功能是非常难的,如果没有curator,直接去写的话基本上能把你累哭,就好比连Mybatis或者jpa都没有,让你用原