0    课程地址

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

 

1    重点关注

1.1    本节内容

获取zk节点数据demo

 

1.2    关键代码

注意看:注册watch事件,下边process方法进行watch事件。在watch执行完前CountDownLatch是阻塞的

    /**
         * 参数:
         * path:节点路径
         * watch:true或者false,注册一个watch事件
         * stat:状态
         */
        byte[] resByte = zkServer.getZookeeper().getData("/imooc", true, stat);

String result = new String(resByte);

System.out.println("更改后的值:" +result);

System.out.println("版本号变化dversion:" + stat.getVersion());



  @Override
    public void process(WatchedEvent event) {
        
    }

 

 

2    课程内容

2.1    额外学习之byte转String方法

                byte[] resByte = zkServer.getZookeeper().getData("/imooc", false, stat);
                String result = new String(resByte);

 

 

 

3    Coding

3.1    获取zk节点数据

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

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

 

  • 主类
package com.imooc.zk.demo;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
 * 
 * @Description: zookeeper 获取节点数据的demo演示
 */
public class ZKGetNodeData implements Watcher {

    private ZooKeeper zookeeper = null;
    
    public static final String zkServerPath = "172.26.139.4:2181";
    public static final Integer timeout = 5000;
    private static Stat stat = new Stat();
    
    public ZKGetNodeData() {}
    
    public ZKGetNodeData(String connectString) {
        try {
            zookeeper = new ZooKeeper(connectString, timeout, new ZKGetNodeData());
        } catch (IOException e) {
            e.printStackTrace();
            if (zookeeper != null) {
                try {
                    zookeeper.close();
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    
    private static CountDownLatch countDown = new CountDownLatch(1);
    
    public static void main(String[] args) throws Exception {
    
        ZKGetNodeData zkServer = new ZKGetNodeData(zkServerPath);
        
        /**
         * 参数:
         * path:节点路径
         * watch:true或者false,注册一个watch事件
         * stat:状态
         */
        byte[] resByte = zkServer.getZookeeper().getData("/imooc", true, stat);
        String result = new String(resByte);
        System.out.println("当前值:" + result);
        countDown.await();
    }
    
    @Override
    public void process(WatchedEvent event) {
        try {
            if(event.getType() == EventType.NodeDataChanged){
                ZKGetNodeData zkServer = new ZKGetNodeData(zkServerPath);
                byte[] resByte = zkServer.getZookeeper().getData("/imooc", false, stat);
                String result = new String(resByte);
                System.out.println("更改后的值:" + result);
                System.out.println("版本号变化dversion:" + stat.getVersion());
                countDown.countDown();
            } else if(event.getType() == EventType.NodeCreated) {
                
            } else if(event.getType() == EventType.NodeChildrenChanged) {
                
            } else if(event.getType() == EventType.NodeDeleted) {
                
            } 
        } catch (KeeperException e) { 
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public ZooKeeper getZookeeper() {
        return zookeeper;
    }
    public void setZookeeper(ZooKeeper zookeeper) {
        this.zookeeper = zookeeper;
    }
}

 

  • linux客户端更新数据前打印日志
当前值:532

 

  • linux客户端更新数据
--启动客户端
zkCli.sh

[zk: localhost:2181(CONNECTED) 0] ls /
[imooc2, names, zookeeper, imooc3, imooc, testnode, imooc5]

--修改节点imooc
[zk: localhost:2181(CONNECTED) 2] set /imooc 520
cZxid = 0x19
ctime = Tue Jan 09 12:30:19 CST 2024
mZxid = 0x93
mtime = Tue Apr 02 06:41:23 CST 2024
pZxid = 0x32
cversion = 8
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 2

--查看节点imooc
[zk: localhost:2181(CONNECTED) 3] get /imooc
520
cZxid = 0x19
ctime = Tue Jan 09 12:30:19 CST 2024
mZxid = 0x93
mtime = Tue Apr 02 06:41:23 CST 2024
pZxid = 0x32
cversion = 8
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 2

 

 

  • linux客户端更新数据后打印日志
当前值:532
更改后的值:520
版本号变化dversion:2

 

posted on 2024-04-02 07:08  菜鸟乙  阅读(13)  评论(0编辑  收藏  举报