ZooKeeper之watch机制

1. watch机制

Client: zookeeper的客户端;
watchManager: Client中的回调函数

Client向Server注册成功后, 一旦node节点发生改变, server就会主动通知Client, 对于原生zookeeper, watchManager模块就会处理这些改变,如呈现改变的内容, 完成操作后, watcherManager就停止监听node节点了, 如果想继续监听node节点,需要重新注册,比较麻烦;
而且只能监听节点和儿子辈儿节点;

Curator是一款连接zookeeper的框架,优化了watch机制, 使用缓存机制, 客户端本地缓存和服务器端缓存对比, 不同则通知, 同时Curator会再次自动监听;

ZooKeeper之watch机制_第1张图片

2 缓存实现zookeeper监控

NodeCache: 只监视本节点;

public class WatchTest {
     

    @Test
    public void nodeCacheTest() throws Exception {
     

        //重试策略
        RetryPolicy retryPolicy=new ExponentialBackoffRetry(3000,3);
        //Curator客户端对象
        CuratorFramework client =
                CuratorFrameworkFactory.newClient("127.0.0.1:2181", 1000, 1000, retryPolicy);

        //开启客户端
        client.start();

        //创建NodeCache对象,监听的是哪一个zookeeper客户端, 监听哪一个节点
        NodeCache nodeCache= new NodeCache(client,"/hello");

        //开始缓存
        nodeCache.start(true);

        //添加监听对象(如果节点数据有变化,会调用回调方法)
        nodeCache.getListenable().addListener(new NodeCacheListener() {
     
            @Override
            public void nodeChanged() throws Exception {
     
                //获取一个节点的最新数据
                ChildData data =
                        nodeCache.getCurrentData();
                //打印最新的节点数据
                System.out.println("节点数据:===path:"+ data.getPath()+"==数据:"+new String(data.getData()));
            }
        });
        //阻塞程序执行, 一直等待控制台输入
        System.in.read();
    }
}

TreeCache: 监听全部节点

 @Test
    public void treeCache() throws Exception {
     
        //创建重试策略
        RetryPolicy retryPolicy=new ExponentialBackoffRetry(3000,3);
        //创建Curator客户端对象
        CuratorFramework client=
                CuratorFrameworkFactory.newClient("127.0.0.1:2181",1000,100,retryPolicy);
        //开启客户端
        client.start();

        //创建节点的数据监控对象
        TreeCache treeCache=new TreeCache(client,"/hello");

        //开始缓存
        treeCache.start();

        treeCache.getListenable().addListener(new TreeCacheListener() {
     
            @Override
            public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception {
     
                if(event.getType() == TreeCacheEvent.Type.CONNECTION_RECONNECTED){
     
                    System.out.println("重新连接");
                } else if(event.getType() == TreeCacheEvent.Type.CONNECTION_RECONNECTED){
     
                    System.out.println("连接过时");
                } else if(event.getType() == TreeCacheEvent.Type.CONNECTION_LOST){
     
                    System.out.println("连接过时一段时间");
                } else if(event.getType() == TreeCacheEvent.Type.INITIALIZED){
     
                    System.out.println("初始化完成");
                } else if(event.getType() == TreeCacheEvent.Type.NODE_ADDED){
     
                    System.out.println("子节点添加"+ "===路径:"+event.getData().getPath() + "====数据"+new String(event.getData().getData()));
                }else if(event.getType() == TreeCacheEvent.Type.NODE_UPDATED){
     
                    System.out.println("子节点修改"+ "===路径:"+event.getData().getPath() + "====数据"+new String(event.getData().getData()));
                }else if(event.getType() == TreeCacheEvent.Type.NODE_REMOVED){
     
                    System.out.println("子节点删除"+ "===路径:"+event.getData().getPath() + "====数据"+new String(event.getData().getData()));
                }
            }
        });

        //阻塞程序
        System.in.read();
        
    }

你可能感兴趣的:(Java入门)