Curator的三种缓存。


在使用原生的ZooKeeper的时候,是可以使用Watcher对节点进行监听的,但是唯一不方便的是一个Watcher只能生效一次,也就是说每次进行监听回调之后我们需要自己重新的设置监听才能达到永久监听的效果。

Curator在这方面做了优化,Curator引入了Cache的概念用来实现对ZooKeeper服务器端进行事件监听。Cache是Curator对事件监听的包装,其对事件的监听可以近似看做是一个本地缓存视图和远程ZooKeeper视图的对比过程。而且Curator会自动的再次监听,我们就不需要自己手动的重复监听了。

ZooKeeper版本:3.4.11,Curator版本:2.12.0


Curator分类。

Curator中的Cache共有三种:NodeCache、PathChildrenCache、TreeCache。

这三种缓存均在:org.apache.curator.framework.recipes.cache 中,该包下的全部内容如下:

这里写图片描述

可以看到都是与这三种有关的类,其中ChildData就是对节点信息的封装,官方API地址。,建议先看官方的API,因为本文不会对缓存中的方法进行详细的介绍,只会介绍其中的大概使用和使用时需要注意的东西。

下面分别对三种Cache进行介绍。


NodeCache。

NodeCache是用来监听节点的数据变化的,当监听的节点的数据发生变化的时候就会回调对应的函数。NodeCache的构造方法有两种:

NodeCache(CuratorFramework client, String path) 

NodeCache(CuratorFramework client, String path, boolean dataIsCompressed) 

第一个参数就是传入创建的Curator客户端即可,第二个参数就是监听节点的路径,第三个dataIsCompressed参数表示是否对数据进行压缩。

在创建完NodeCache的实例之后,我们需要调用它的start方法才能进行缓存:

void    start()//Start the cache.

void    start(boolean buildInitial)//Same as start() but gives the option of doing an initial build

唯一的一个参数代表着是否将该节点的数据立即进行缓存。如果设置为true的话,我们现在调用NodeCache的getCurrentData方法就能够得到对应节点的信息ChildData类,如果设置为false的就得不到对应的信息。

来看下面一段伪代码:

    /**
     * 监听节点数据变化
     */
    public static void nodeCache() {
        final NodeCache nodeCache = new NodeCache(client, parentPath, false);
        try {
            nodeCache.start(false);//true代表缓存当前节点
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (nodeCache.getCurrentData() != null) {
  //只有start中的设置为true才能够直接得到
            System.out.println( num++ + ".nodeCache-------CurrentNode Data is:" + new String(nodeCache.getCurrentData().getData()) + "\n===========================\n");//输出当前节点的内容
        }

        //添加节点数据监听
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            public void nodeChanged() throws Exception {
                System.out.println( num++ + ".nodeCache------节点数据发生了改变,发生的路径为:" + nodeCache.getCurrentData().getPath() + ",节点数据发生了改变 ,新的数据为:" + new String(nodeCache.getCurrentData().getData()) + "\n===========================\n");
            }
        });
    }

如果nodeCache的start使用了true参数,在下面的nodeCache.getCurrentData()得到的才不会是null。

同时在代码中也可以看到对节点进行监听的方法:调用NodeCache的getListenable方法然后使用addListener传入一个NodeCacheListener监听器。每次该节点中的数据发生变化的时候就会执行监听器中的nodeChanged方法。

监听器:

new NodeCacheListener() {
            public void nodeChanged() throws Exception {
            }
        }

使用注意:

如果NodeCache监听的节点为空(也就是说传入的路径不存在)。那么如果我们后面创建了对应的节点,也是会触发事件从而回调nodeChanged方法。但是遗憾的是,删除了该节点并不会触发。


PathChildrenCache。

PathChildrenCache是用来监听指定节点 的子节点变化情况。共有六种构造方法(有两种弃用了,否则就是八种):

PathChildrenCache(CuratorFramework client, String path, boolean cacheData) 

PathChildrenCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, CloseableExecutorService executorService) 

PathChildrenCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, ExecutorService executorService) 

PathChildrenCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, ThreadFactory threadFactory) 

PathChildrenCache(CuratorFramework client, String path, boolean cacheData, ThreadFactory threadFactory) 

PathChildrenCache(CuratorFramework client, String path, PathChildrenCacheMode mode)

六种常用的也就是第一种,传入Curator端和想要监听的路径,cacheData表示是否将监听变化的节点缓存在其。如果设置为true的话,客户端在接收到节点列表发生变化的同时,也能够获取到节点的数据内容。

同样的在得到PathChildrenCache的实例之后,我们需要调用其start方法才能开始缓存,这里的start方法中可以传入三种模式,也就是API列表中看到的StartMode,其中定义了下面三种枚举:

  1. NORMAL
    The cache will be primed (in the background) with initial values. Events for existing and new nodes will be posted.

  2. BUILD_INITIAL_CACHE

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值