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 {
        TLClientCreate.main(null);
        Thread.sleep(2000);
        TLClientUpdate.main(null);
        Thread.sleep(2000);

        TLClientCreateSub.main(null);
        Thread.sleep(2000);
        TLClientUpdateSub.main(null);
        Thread.sleep(2000);
        TLClientDeleteSub.main(null);

        Thread.sleep(2000);
        TLClientDelete.main(null);
    }
}


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

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;
import java.util.Map;

public class TreeListener {
    public static final String C_PATH = "/TestTree";
    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 TreeCache treeCache = new TreeCache(client, C_PATH);
                        treeCache.getListenable().addListener(new TreeCacheListener() {
                            @Override
                            public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
                                System.out.println("================== catch tree change ==================");
                                if(event.getData() == null){
                                    System.out.println("===init," + event.getType());
                                    return;
                                }

                                if(event.getData().getData() == null){
                                    System.out.println("===delete," + event.getType() + "," + event.getData().getPath());
                                }else{
                                    System.out.println("===update or add," + event.getType() + "," + event.getData().getPath() + "," + new String(event.getData().getData(), TreeListener.CHARSET));
                                }
                            }
                        });
                        treeCache.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.TreeCache;

import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;
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;

public class TLClientCreate {
    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(TreeListener.C_PATH);
                    if (stat == null) {
                        client.create().withMode(CreateMode.PERSISTENT).forPath(TreeListener.C_PATH, "-1".getBytes(NodeListener.CHARSET));
                    }

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

import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;
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;

public class TLClientCreateSub {
    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(TreeListener.C_PATH + "/sub1");
                    if (stat == null) {
                        client.create().withMode(CreateMode.PERSISTENT).forPath(TreeListener.C_PATH + "/sub1", "-1".getBytes(NodeListener.CHARSET));
                    }

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

import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;
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;

public class TLClientDelete {
    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(TreeListener.C_PATH);
                    if (stat != null) {
                        client.delete().deletingChildrenIfNeeded().forPath(TreeListener.C_PATH);
                    }

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

import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;
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;

public class TLClientDeleteSub {
    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(TreeListener.C_PATH + "/sub1");
                    if (stat != null) {
                        client.delete().forPath(TreeListener.C_PATH + "/sub1");
                    }

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

import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NodeListener;
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;

public class TLClientUpdate {
    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(TreeListener.C_PATH);
                    if (stat != null) {
                        client.setData().forPath(TreeListener.C_PATH, "64".getBytes(NodeListener.CHARSET));
                    }

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

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;

public class TLClientUpdateSub {
    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(TreeListener.C_PATH + "/sub1");
                    if (stat != null) {
                        client.setData().forPath(TreeListener.C_PATH + "/sub1", "128".getBytes(TreeListener.CHARSET));
                    }

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






















				
时间: 2024-12-28 12:15:04

Apache Curator Tree Cache Watcher的相关文章

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

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入门实战

版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 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监控Zookeeper的Node和Path的状态

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

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

ZooKeeper系列(四)—— Java 客户端 Apache Curator

一.基本依赖 Curator 是 Netflix 公司开源的一个 Zookeeper 客户端,目前由 Apache 进行维护.与 Zookeeper 原生客户端相比,Curator 的抽象层次更高,功能也更加丰富,是目前 Zookeeper 使用范围最广的 Java 客户端.本篇文章主要讲解其基本使用,项目采用 Maven 构建,以单元测试的方法进行讲解,相关依赖如下: <dependencies> <!--Curator 相关依赖--> <dependency> &l

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.