0 课程地址
https://coding.imooc.com/lesson/201.html#mid=12725
1 重点关注
1.1 本节内容
获取zk子节点数据demo,有其余一些比较简单的demo没有演练,用到这块的话,也可以再看下视频
1.2 关键代码
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { for (String s : children) { System.out.println(s); } System.out.println("ChildrenCallback:" + path); System.out.println((String)ctx); System.out.println(stat.toString()); }
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.List; import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.Watcher.Event.EventType; /** * @Description: zookeeper 获取子节点数据的demo演示 */ public class ZKGetChildrenList implements Watcher { private ZooKeeper zookeeper = null; public static final String zkServerPath = "172.26.139.4:2181"; public static final Integer timeout = 5000; public ZKGetChildrenList() {} public ZKGetChildrenList(String connectString) { try { zookeeper = new ZooKeeper(connectString, timeout, new ZKGetChildrenList()); } 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 { ZKGetChildrenList zkServer = new ZKGetChildrenList(zkServerPath); /** * 参数: * path:父节点路径 * watch:true或者false,注册一个watch事件 */ // List<String> strChildList = zkServer.getZookeeper().getChildren("/imooc", true); // for (String s : strChildList) { // System.out.println(s); // } // 异步调用 String ctx = "{'callback':'ChildrenCallback'}"; zkServer.getZookeeper().getChildren("/imooc", true, new Children2CallBack(), ctx); countDown.await(); } @Override public void process(WatchedEvent event) { try { if(event.getType()==EventType.NodeChildrenChanged){ System.out.println("NodeChildrenChanged"); ZKGetChildrenList zkServer = new ZKGetChildrenList(zkServerPath); List<String> strChildList = zkServer.getZookeeper().getChildren(event.getPath(), false); for (String s : strChildList) { System.out.println(s); } countDown.countDown(); } else if(event.getType() == EventType.NodeCreated) { System.out.println("NodeCreated"); } else if(event.getType() == EventType.NodeDataChanged) { System.out.println("NodeDataChanged"); } else if(event.getType() == EventType.NodeDeleted) { System.out.println("NodeDeleted"); } } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } public ZooKeeper getZookeeper() { return zookeeper; } public void setZookeeper(ZooKeeper zookeeper) { this.zookeeper = zookeeper; } }
- 回调类:
package com.imooc.zk.demo; import java.util.List; import org.apache.zookeeper.AsyncCallback.Children2Callback; import org.apache.zookeeper.data.Stat; public class Children2CallBack implements Children2Callback { @Override public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { for (String s : children) { System.out.println(s); } System.out.println("ChildrenCallback:" + path); System.out.println((String)ctx); System.out.println(stat.toString()); } }
- 打印日志
aaa
abc
ChildrenCallback:/imooc
{'callback':'ChildrenCallback'}
25,147,1704774619142,1712011283195,2,8,0,0,3,2,50
- linux客户端更新数据
--启动客户端
zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[imooc2, names, zookeeper, imooc3, imooc, testnode, imooc5]
--查看节点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客户端标红部分和日志打印部分能对应上,时间转化后也可以看下
诸葛