0    课程地址

https://coding.imooc.com/lesson/201.html#mid=12737

 

1    重点关注

1.1    本节内容

curator自带节点监听多次,监听子节点(可以针对增删改不同类型的节点修改类型判断)

 

1.2    关键代码

// 为子节点添加watcher
        // PathChildrenCache: 监听数据节点的增删改,会触发事件
        String childNodePathCache =  nodePath;
        // cacheData: 设置缓存节点的数据状态
        final PathChildrenCache childrenCache = new PathChildrenCache(cto.client, childNodePathCache, true);
        /**
         * StartMode: 初始化方式
         * POST_INITIALIZED_EVENT:异步初始化,初始化之后会触发事件,这个经常使用
         * NORMAL:异步初始化 这个不常用,更多的异步处理需要触发事件
         * BUILD_INITIAL_CACHE:同步初始化,这个用的较少,同步无法触发事件
         */
        childrenCache.start(StartMode.POST_INITIALIZED_EVENT);
        
        List<ChildData> childDataList = childrenCache.getCurrentData();
        System.out.println("当前数据节点的子节点数据列表:");
        for (ChildData cd : childDataList) {
            String childData = new String(cd.getData());
            System.out.println(childData);
        }
        // 根据event.getType判断节点变化类型
        childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                if(event.getType().equals(PathChildrenCacheEvent.Type.INITIALIZED)){
                    System.out.println("子节点初始化ok...");
                }
                // 这里的数据路径,通常可以从配置平台和环境变量获取,匹配到对应节点只能对特定文件生效
                else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)){
                    String path = event.getData().getPath();
                    if (path.equals(ADD_PATH)) {
                        System.out.println("添加子节点:" + event.getData().getPath());
                        System.out.println("子节点数据:" + new String(event.getData().getData()));
                    } else if (path.equals("/super/imooc/e")) {
                        System.out.println("添加不正确...");
                    }
                    
                }else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED)){
                    System.out.println("删除子节点:" + event.getData().getPath());
                }else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
                    System.out.println("修改子节点路径:" + event.getData().getPath());
                    System.out.println("修改子节点数据:" + new String(event.getData().getData()));
                }
            }
        });
        
        Thread.sleep(1000000);
        
        cto.closeZKClient();
        boolean isZkCuratorStarted2 = cto.client.isStarted();
        System.out.println("当前客户的状态:" + (isZkCuratorStarted2 ? "连接中" : "已关闭"));
    }
    
    public final static String ADD_PATH = "/super/imooc/d";
    

 

 

 

 

2    课程内容


 

 


 

3    Coding

3.1    Curator使用监听(可判断节点变更类型)

  • 启动服务端
    进入到
cd /usr/local/zookeeper/bin

 
    重启zookeeper服务端
./zkServer.sh restart

 

  • 主类
package com.imooc.curator;

import java.util.List;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;

public class CuratorOperator {

    public CuratorFramework client = null;
    public static final String zkServerPath = "172.26.139.4:2181";

    /**
     * 实例化zk客户端
     */
    public CuratorOperator() {
        /**
         * 同步创建zk示例,原生api是异步的
         * 
         * curator链接zookeeper的策略:ExponentialBackoffRetry
         * baseSleepTimeMs:初始sleep的时间
         * maxRetries:最大重试次数
         * maxSleepMs:最大重试时间
         */
//        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
        
        /**
         * curator链接zookeeper的策略:RetryNTimes
         * n:重试的次数
         * sleepMsBetweenRetries:每次重试间隔的时间
         */
        RetryPolicy retryPolicy = new RetryNTimes(3, 5000);
        
        /**
         * curator链接zookeeper的策略:RetryOneTime
         * sleepMsBetweenRetry:每次重试间隔的时间
         */
//        RetryPolicy retryPolicy2 = new RetryOneTime(3000);
        
        /**
         * 永远重试,不推荐使用
         */
//        RetryPolicy retryPolicy3 = new RetryForever(retryIntervalMs)
        
        /**
         * curator链接zookeeper的策略:RetryUntilElapsed
         * maxElapsedTimeMs:最大重试时间
         * sleepMsBetweenRetries:每次重试间隔
         * 重试时间超过maxElapsedTimeMs后,就不再重试
         */
//        RetryPolicy retryPolicy4 = new RetryUntilElapsed(2000, 3000);
        
        client = CuratorFrameworkFactory.builder()
                .connectString(zkServerPath)
                .sessionTimeoutMs(10000).retryPolicy(retryPolicy)
                .namespace("workspace").build();
        client.start();
    }
    
    /**
     * 
     * @Description: 关闭zk客户端连接
     */
    public void closeZKClient() {
        if (client != null) {
            this.client.close();
        }
    }
    
    public static void main(String[] args) throws Exception {
        // 实例化
        CuratorOperator cto = new CuratorOperator();
        boolean isZkCuratorStarted = cto.client.isStarted();
        System.out.println("当前客户的状态:" + (isZkCuratorStarted ? "连接中" : "已关闭"));
        
        // 创建节点
        String nodePath = "/super/imooc";
//        byte[] data = "superme".getBytes();
//        cto.client.create().creatingParentsIfNeeded()
//            .withMode(CreateMode.PERSISTENT)
//            .withACL(Ids.OPEN_ACL_UNSAFE)
//            .forPath(nodePath, data);
        
        // 更新节点数据
//        byte[] newData = "batman".getBytes();
//        cto.client.setData().withVersion(0).forPath(nodePath, newData);
        
        // 删除节点
//        cto.client.delete()
//                  .guaranteed()                    // 如果删除失败,那么在后端还是继续会删除,直到成功
//                  .deletingChildrenIfNeeded()    // 如果有子节点,就删除
//                  .withVersion(0)
//                  .forPath(nodePath);
        
        
        
        // 读取节点数据
//        Stat stat = new Stat();
//        byte[] data = cto.client.getData().storingStatIn(stat).forPath(nodePath);
//        System.out.println("节点" + nodePath + "的数据为: " + new String(data));
//        System.out.println("该节点的版本号为: " + stat.getVersion());
        
        
        // 查询子节点
//        List<String> childNodes = cto.client.getChildren()
//                                            .forPath(nodePath);
//        System.out.println("开始打印子节点:");
//        for (String s : childNodes) {
//            System.out.println(s);
//        }
        
                
        // 判断节点是否存在,如果不存在则为空
//        Stat statExist = cto.client.checkExists().forPath(nodePath + "/abc");
//        System.out.println(statExist);
        
        
        // watcher 事件  当使用usingWatcher的时候,监听只会触发一次,监听完毕后就销毁
//        cto.client.getData().usingWatcher(new MyCuratorWatcher()).forPath(nodePath);
//        cto.client.getData().usingWatcher(new MyWatcher()).forPath(nodePath);
        
        // 为节点添加watcher
        // NodeCache: 监听数据节点的变更,会触发事件
//        final NodeCache nodeCache = new NodeCache(cto.client, nodePath);
//        // buildInitial : 初始化的时候获取node的值并且缓存
//        nodeCache.start(true);
//        if (nodeCache.getCurrentData() != null) {
//            System.out.println("节点初始化数据为:" + new String(nodeCache.getCurrentData().getData()));
//        } else {
//            System.out.println("节点初始化数据为空...");
//        }
//        nodeCache.getListenable().addListener(new NodeCacheListener() {
//            public void nodeChanged() throws Exception {
//                if (nodeCache.getCurrentData() == null) {
//                    System.out.println("空");
//                    return;
//                }
//                String data = new String(nodeCache.getCurrentData().getData());
//                System.out.println("节点路径:" + nodeCache.getCurrentData().getPath() + "数据:" + data);
//            }
//        });
        
        
        // 为子节点添加watcher
        // PathChildrenCache: 监听数据节点的增删改,会触发事件
        String childNodePathCache =  nodePath;
        // cacheData: 设置缓存节点的数据状态
        final PathChildrenCache childrenCache = new PathChildrenCache(cto.client, childNodePathCache, true);
        /**
         * StartMode: 初始化方式
         * POST_INITIALIZED_EVENT:异步初始化,初始化之后会触发事件
         * NORMAL:异步初始化
         * BUILD_INITIAL_CACHE:同步初始化
         */
        childrenCache.start(StartMode.POST_INITIALIZED_EVENT);
        
        List<ChildData> childDataList = childrenCache.getCurrentData();
        System.out.println("当前数据节点的子节点数据列表:");
        for (ChildData cd : childDataList) {
            String childData = new String(cd.getData());
            System.out.println(childData);
        }
        
        childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                if(event.getType().equals(PathChildrenCacheEvent.Type.INITIALIZED)){
                    System.out.println("子节点初始化ok...");
                }
                
                else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)){
                    String path = event.getData().getPath();
                    if (path.equals(ADD_PATH)) {
                        System.out.println("添加子节点:" + event.getData().getPath());
                        System.out.println("子节点数据:" + new String(event.getData().getData()));
                    } else if (path.equals("/super/imooc/e")) {
                        System.out.println("添加不正确...");
                    }
                    
                }else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED)){
                    System.out.println("删除子节点:" + event.getData().getPath());
                }else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
                    System.out.println("修改子节点路径:" + event.getData().getPath());
                    System.out.println("修改子节点数据:" + new String(event.getData().getData()));
                }
            }
        });
        
        Thread.sleep(1000000);
        
        cto.closeZKClient();
        boolean isZkCuratorStarted2 = cto.client.isStarted();
        System.out.println("当前客户的状态:" + (isZkCuratorStarted2 ? "连接中" : "已关闭"));
    }
    
    public final static String ADD_PATH = "/super/imooc/d";
    
}

 

  • linux客户端修改节点(修改多次)
--启动linux客户端
zkCli.sh

--初始化节点
[zk: localhost:2181(CONNECTED) 2] create /workspace/super/imooc/d d
Created /workspace/super/imooc/d
[zk: localhost:2181(CONNECTED) 3] create /workspace/super/imooc/a a
Created /workspace/super/imooc/a
[zk: localhost:2181(CONNECTED) 4] create /workspace/super/imooc/b b
Created /workspace/super/imooc/b
[zk: localhost:2181(CONNECTED) 5] create /workspace/super/imooc/c c
Created /workspace/super/imooc/c
[zk: localhost:2181(CONNECTED) 6] ls /workspace/super/imooc        
[a, b, c, d]

--启动sts客户端后,进行修改节点
[zk: localhost:2181(CONNECTED) 7] set /workspace/super/imooc/d ddd
cZxid = 0x101
ctime = Wed Apr 10 06:32:42 CST 2024
mZxid = 0x106
mtime = Wed Apr 10 06:34:50 CST 2024
pZxid = 0x101
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
[zk: localhost:2181(CONNECTED) 8] set /workspace/super/imooc/a aaa
cZxid = 0x102
ctime = Wed Apr 10 06:32:47 CST 2024
mZxid = 0x107
mtime = Wed Apr 10 06:35:04 CST 2024
pZxid = 0x102
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0

--启动sts客户端后,进行删除节点
[zk: localhost:2181(CONNECTED) 9] delete /workspace/super/imooc/d  

--启动sts客户端后,进行创建节点
[zk: localhost:2181(CONNECTED) 10] create /workspace/super/imooc/d
[zk: localhost:2181(CONNECTED) 11] create /workspace/super/imooc/d d2
Created /workspace/super/imooc/d
[zk: localhost:2181(CONNECTED) 12] create /workspace/super/imooc/a a2
Node already exists: /workspace/super/imooc/a


--启动sts客户端后,验证不同目录操作打印
[zk: localhost:2181(CONNECTED) 13] create /workspace/super/imooc/e e2
Created /workspace/super/imooc/e

 

  • 打印日志(监听多次)
2024-04-10 06:34:34,150 [main] [org.apache.zookeeper.Environment.logEnv(Environment.java:100)] - [INFO] Client environment:os.version=10.0
2024-04-10 06:34:34,151 [main] [org.apache.zookeeper.Environment.logEnv(Environment.java:100)] - [INFO] Client environment:user.name=18612
2024-04-10 06:34:34,152 [main] [org.apache.zookeeper.Environment.logEnv(Environment.java:100)] - [INFO] Client environment:user.home=C:\Users\18612
2024-04-10 06:34:34,153 [main] [org.apache.zookeeper.Environment.logEnv(Environment.java:100)] - [INFO] Client environment:user.dir=D:\project\xin\zk_sts_imooc\imooc-zk-curator-maven
2024-04-10 06:34:34,153 [main] [org.apache.zookeeper.ZooKeeper.<init>(ZooKeeper.java:441)] - [INFO] Initiating client connection, connectString=172.26.139.4:2181 sessionTimeout=10000 watcher=org.apache.curator.ConnectionState@595b007d
2024-04-10 06:34:34,365 [main] [org.apache.curator.framework.imps.CuratorFrameworkImpl.start(CuratorFrameworkImpl.java:326)] - [INFO] Default schema
当前客户的状态:连接中
2024-04-10 06:34:34,371 [main-SendThread(172.26.139.4:2181)] [org.apache.zookeeper.ClientCnxn$SendThread.logStartConnect(ClientCnxn.java:1035)] - [INFO] Opening socket connection to server 172.26.139.4/172.26.139.4:2181. Will not attempt to authenticate using SASL (unknown error)
2024-04-10 06:34:34,381 [main-SendThread(172.26.139.4:2181)] [org.apache.zookeeper.ClientCnxn$SendThread.primeConnection(ClientCnxn.java:877)] - [INFO] Socket connection established to 172.26.139.4/172.26.139.4:2181, initiating session
2024-04-10 06:34:34,389 [main-SendThread(172.26.139.4:2181)] [org.apache.zookeeper.ClientCnxn$SendThread.onConnected(ClientCnxn.java:1302)] - [INFO] Session establishment complete on server 172.26.139.4/172.26.139.4:2181, sessionid = 0x1003feb17e7000d, negotiated timeout = 10000
当前数据节点的子节点数据列表:
2024-04-10 06:34:34,397 [main-EventThread] [org.apache.curator.framework.state.ConnectionStateManager.postState(ConnectionStateManager.java:237)] - [INFO] State change: CONNECTED
添加子节点:/super/imooc/d
子节点数据:d
子节点初始化ok...
修改子节点路径:/super/imooc/d
修改子节点数据:ddd
修改子节点路径:/super/imooc/a
修改子节点数据:aaa
删除子节点:/super/imooc/d
添加子节点:/super/imooc/d
子节点数据:d2
添加不正确...

 




 

 










posted on 2024-04-10 06:55  菜鸟乙  阅读(16)  评论(0编辑  收藏  举报