ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
从设计模式角度来理解:是一个基于观察者模式设计的分布式服务管理框架,它负责存储和管理大家都关心的数据,然后接受观察者的注册,一旦这些数据的状态发生变化,Zookeeper就将负责通知已经在Zookeeper上注册的那些观察者做出相应的反应,从而实现集群中类似Master/Slave管理模式。
提供的服务包括:
zookeeper的数据模型的结构和unix文件系统很相似,整体上看是一颗目录树,每一个节点称为ZNode(每个节点不但有目录名称,还必须要有值,类似于键值对)。
zookeeper集群自身维护了一套数据结构。这个存储结构是一个树形结构,其上的每一个节点,我们称之为”znode”,每一个znode默认能够存储1MB的数据,每个ZNode都可以通过其路径唯一标识。
Znode有四种类型:
假如有5台服务器,编号分别为server1~5,
通过命令行的zkCli连入zkServer:
[root@hadoop001 bin]# ./zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[jess, servers, zookeeper, sanguo]
[zk: localhost:2181(CONNECTED) 2] get /servers
9999
cZxid = 0x10000002d
ctime = Sun Aug 19 02:33:19 PDT 2018
mZxid = 0x10000002d
mtime = Sun Aug 19 02:33:19 PDT 2018
pZxid = 0x10000003a
cversion = 8
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
上述的get /servers就是获得了servers节点的stat:
启动客户端
[root@hadoop001 bin]# ./zkCli.sh
显示所有操作命令
[zk: localhost:2181(CONNECTED) 3] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
查看当前znode中所包含的子znode
[zk: localhost:2181(CONNECTED) 4] ls /
[jess, servers, zookeeper, sanguo]
查看当前节点数据并能看到stat数据
[zk: localhost:2181(CONNECTED) 5] ls2 /servers
[]
cZxid = 0x10000002d
ctime = Sun Aug 19 02:33:19 PDT 2018
mZxid = 0x10000002d
mtime = Sun Aug 19 02:33:19 PDT 2018
pZxid = 0x10000003a
cversion = 8
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
创建普通节点
[zk: localhost:2181(CONNECTED) 6] create /blog "blog"
Created /blog
[zk: localhost:2181(CONNECTED) 7] ls /
[jess, servers, zookeeper, blog, sanguo]
获得节点的值
[zk: localhost:2181(CONNECTED) 8] get /blog
blog
cZxid = 0x200000002
ctime = Sun Aug 19 20:04:32 PDT 2018
mZxid = 0x200000002
mtime = Sun Aug 19 20:04:32 PDT 2018
pZxid = 0x200000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
创建短暂节点
[zk: localhost:2181(CONNECTED) 9] create -e /blog-temp "temp"
Created /blog-temp
[zk: localhost:2181(CONNECTED) 10] ls /
[jess, servers, zookeeper, blog, sanguo, blog-temp]
创建普通带序号节点
[jess, servers, zookeeper, blog, sanguo, blog-temp]
[zk: localhost:2181(CONNECTED) 11] create -s /blog-sequence "sequence"
Created /blog-sequence0000000010
[zk: localhost:2181(CONNECTED) 12] ls /
[servers, zookeeper, sanguo, blog-sequence0000000010, blog-temp, jess, blog]
创建临时带序号的节点
[zk: localhost:2181(CONNECTED) 13] create -s -e /blog-ts "ts"
Created /blog-ts0000000011
[zk: localhost:2181(CONNECTED) 14] ls /
[servers, zookeeper, sanguo, blog-sequence0000000010, blog-temp, jess, blog, blog-ts0000000011]
节点的值变化监听
[zk: localhost:2181(CONNECTED) 19] get /servers watch
节点的子节点变化监听
[zk: localhost:2181(CONNECTED) 19] ls /servers watch
删除节点(不能删除带有子节点的节点)
[zk: localhost:2181(CONNECTED) 19] delete /blog
[zk: localhost:2181(CONNECTED) 20] ls /
[servers, zookeeper, sanguo, blog-sequence0000000010, blog-temp, jess, blog-ts0000000011]
递归删除节点(可以删除带有子节点的节点)
[zk: localhost:2181(CONNECTED) 21] rmr /sanguo
[zk: localhost:2181(CONNECTED) 22] ls /
[servers, zookeeper, blog-sequence0000000010, blog-temp, jess, blog-ts0000000011]
查看节点状态
[zk: localhost:2181(CONNECTED) 23] stat /blog-temp
cZxid = 0x200000003
ctime = Sun Aug 19 20:05:58 PDT 2018
mZxid = 0x200000003
mtime = Sun Aug 19 20:05:58 PDT 2018
pZxid = 0x200000003
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x165551e0a820000
dataLength = 4
numChildren = 0