5.1 常见命令行操作
常见操作。
启动客户端。
[wangzhou@zookeeper01 bin]$ ./zkCli.sh Connecting to localhost:2181 2022-04-05 07:37:54,764 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.7-f0fdd52973d373ffd9c86b81d99842dc2c7f660e, built on 02/10/2020 11:30 GMT ... WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0]
不过,客户端连接的服务端是localhost,我们也可以通过: sh zkCli.sh -server host:port实现客户端与远程的服务端相连。
[zk: localhost:2181(CONNECTED) 0] quit [wangzhou@zookeeper01 bin]$ ./zkCli.sh -server zookeeper02:2181
在后续中,我们可以使用一台主机作为客户端、一台主机作为服务端。
查看帮助文档
[zk: zookeeper01:2181(CONNECTED) 0] help ZooKeeper -server host:port cmd args addauth scheme auth close config [-c] [-w] [-s] connect host:port create [-s] [-e] [-c] [-t ttl] path [data] [acl] delete [-v version] path deleteall path delquota [-n|-b] path get [-s] [-w] path getAcl [-s] path history listquota path ls [-s] [-w] [-R] path ls2 path [watch] printwatches on|off quit reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*] redo cmdno removewatches path [-c|-d|-a] [-l] rmr path set [-s] [-v version] path data setAcl [-s] [-v version] [-R] path acl setquota -n|-b val path stat [-w] path sync path
我们重点学习里面增删改查节点信息的操作就可以了。
5.2 查看节点信息
先查看当前监听的服务端的节点信息看看。
[zk: zookeeper01:2181(CONNECTED) 1] ls / [zookeeper] [zk: zookeeper01:2181(CONNECTED) 2] ls -s / [zookeeper]cZxid = 0x0 ctime = Wed Dec 31 16:00:00 PST 1969 mZxid = 0x0 mtime = Wed Dec 31 16:00:00 PST 1969 pZxid = 0x0 cversion = -1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1
解释如下:
czxid:
创建节点的事务的zxid。
每次修改 ZooKeeper 状态都会产生一个 ZooKeeper 事务 ID。事务 ID 是 ZooKeeper 中所有修改总的次序。
ctime: znode 被创建的毫秒数(从 1970 年开始) mzxid: znode 最后更新的事务 zxid mtime: znode 最后修改的毫秒数(从 1970 年开始) pZxid: znode 最后更新的子节点 zxid cversion: znode 子节点变化号,znode 子节点修改次数 dataversion: znode 数据变化号 aclVersion: znode 访问控制列表的变化号 ephemeralOwner: 如果是临时节点,这个是 znode 拥有者的 session id。如果不是临时节点则是 0。 dataLength: znode 的数据长度 numChildren: znode 子节点数量
5.3 节点类型与对应操作
5.3.1 节点类型
注意,之前在讲集群时,节点指代的都是服务器主机节点。而这里只是znode下存放信息以节点形式进行组织,请读者注意区别
5.3.2 创建节点
创建永久节点
[zk: zookeeper01:2181(CONNECTED) 3] ls / [zookeeper] [zk: zookeeper01:2181(CONNECTED) 4] create /sanguo "lvbu" Created /sanguo [zk: zookeeper01:2181(CONNECTED) 5] ls / [sanguo, zookeeper] [zk: zookeeper01:2181(CONNECTED) 6] create /sanguo/shuguo "liubei" Created /sanguo/shuguo [zk: zookeeper01:2181(CONNECTED) 7] ls /sanguo [shuguo]
获取该节点信息
[zk: zookeeper01:2181(CONNECTED) 8] get -s /sanguo lvbu cZxid = 0x700000004 ctime = Tue Apr 05 08:02:45 PDT 2022 mZxid = 0x700000004 mtime = Tue Apr 05 08:02:45 PDT 2022 pZxid = 0x700000005 cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 4 numChildren = 1
创建带序列号的永久节点
[zk: zookeeper01:2181(CONNECTED) 10] create -s /sanguo/weiguo "caocao" Created /sanguo/weiguo0000000001 [zk: zookeeper01:2181(CONNECTED) 11] ls /sanguo [shuguo, weiguo0000000001] [zk: zookeeper01:2181(CONNECTED) 12] create -s /sanguo/weiguo "caocao" Created /sanguo/weiguo0000000002 [zk: zookeeper01:2181(CONNECTED) 13] ls /sanguo [shuguo, weiguo0000000001, weiguo0000000002]
创建临时节点。客户端重启后临时节点的信息会丢失
create -e /sanguo/wuguo"zhouyu"
修改节点值
[zk: zookeeper01:2181(CONNECTED) 15] set /sanguo/shuguo "zz" [zk: zookeeper01:2181(CONNECTED) 16] get -s /sanguo/shuguo zz cZxid = 0x700000005 ctime = Tue Apr 05 08:04:01 PDT 2022 mZxid = 0x700000008 mtime = Tue Apr 05 08:21:34 PDT 2022 pZxid = 0x700000005 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 2 numChildren = 0
5.3.3 监听器原理
总结一句话就是,客户端在服务端进行注册,当客户端关心的数据发生变化,或者服务器节点上、下线了,服务端通过回调方法通知到客户端。其实现的方法其实就是观察者模式。如果读者不了解观察者模式,建议您阅读文章:从零开始学习Java设计模式 | 行为型模式篇:观察者模式_李阿昀的博客-CSDN博客,讲解的特别透彻了
[zk: zookeeper01:2181(CONNECTED) 17] get -w /sanguo lvbu [zk: zookeeper01:2181(CONNECTED) 18] set /sanguo zhubajie WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
值得注意的是,只注册一次监听,只能监听一次节点信息的变化,下一次就不会通知了