基础命令操作

  • 启动/停止/重启zk服务
./zkServer.sh start #启动
./zkServer.sh stop #停止
./zkServer.sh restart #重启
  • 查看zk的运行状态
./zkServer.sh status
  • 客户端链接zk
./zkCli.sh
  • help 查看客户端帮助命令
[zk: localhost:2181(CONNECTED) 0] help
  • ls 查看
[zk: localhost:2181(CONNECTED) 1] ls /
[niocoder, zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /zookeeper 
[quota]
[zk: localhost:2181(CONNECTED) 4] ls /zookeeper/quota
[]
  • get 获取节点数据和更新信息
[zk: localhost:2181(CONNECTED) 7] get /zookeeper #下面空行说明节点内容为空

cZxid = 0x0
ctime = Thu Jan 01 00:00:00 UTC 1970
mZxid = 0x0
mtime = Thu Jan 01 00:00:00 UTC 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
[zk: localhost:2181(CONNECTED) 8]
  • get内容为空
  • cZxid :创建节点的id
  • ctime : 节点的创建时间
  • mZxid :修改节点的id
  • mtime :修改节点的时间
  • pZxid :子节点的id
  • cversion : 子节点的版本
  • dataVersion : 当前节点数据的版本
  • aclVersion :权限的版本
  • ephemeralOwner :判断是否是临时节点
  • dataLength : 数据的长度
  • numChildren :子节点的数量
  • stat 获得节点的更新信息
[zk: localhost:2181(CONNECTED) 8] stat /zookeeper
cZxid = 0x0
ctime = Thu Jan 01 00:00:00 UTC 1970
mZxid = 0x0
mtime = Thu Jan 01 00:00:00 UTC 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
  • ls2(ls命令和stat命令的整合)
[zk: localhost:2181(CONNECTED) 10] ls2 /zookeeper
[quota]
cZxid = 0x0
ctime = Thu Jan 01 00:00:00 UTC 1970
mZxid = 0x0
mtime = Thu Jan 01 00:00:00 UTC 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
  • create 创建节点

create [-s] [-e] path data acl

#创建name节点,节点的内容为caoweixiong
[zk: localhost:2181(CONNECTED) 1] create /name caoweixiong
  • create -e 创建临时节点
#创建临时节点
[zk: localhost:2181(CONNECTED) 4] create -e  /name/temp caoweixiong
Created /name/temp
  • create -s 创建顺序节点 自动累加
# 创建顺序节点,顺序节点会自动累加
[zk: localhost:2181(CONNECTED) 2] create -s /name/sec seq
Created /name/sec0000000001
[zk: localhost:2181(CONNECTED) 3] create -s /name/sec seq
Created /name/sec0000000002
  • set path data [version] 修改节点
# 修改节点内容为new-name
[zk: localhost:2181(CONNECTED) 7] set /name new-name
  • delete path [version] 删除节点
[zk: localhost:2181(CONNECTED) 13] delete /name/sec0000000001
  • watcher通知机制

关于watcher机制大体的理解可以为,当每个节点发生变化,都会触发watcher事件,类似于mysql的触发器。zk中 watcher是一次性的,触发后立即销毁。

  • stat path [watch] 设置watch事件
  • get path [watch]设置watch事件
  • 子节点创建和删除时触发watch事件,子节点修改不会触发该事件
stat path [watch] 设置watch事件
#添加watch 事件
[zk: localhost:2181(CONNECTED) 18] stat /longfei watch
Node does not exist: /longfei
#创建longfei节点时触发watcher事件
[zk: localhost:2181(CONNECTED) 19] create /longfei test

WATCHER::

WatchedEvent state:SyncConnected type:NodeCreated path:/longfei
Created /longfei
get path [watch] 设置watch事件
#使用get命令添加watch事件
[zk: localhost:2181(CONNECTED) 20] get /longfei watch
test
cZxid = 0x20000000e
ctime = Sat Jun 02 14:43:15 UTC 2018
mZxid = 0x20000000e
mtime = Sat Jun 02 14:43:15 UTC 2018
pZxid = 0x20000000e
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
#修改节点触发watcher事件
[zk: localhost:2181(CONNECTED) 21] set /longfei new_test

WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/longfei
cZxid = 0x20000000e
ctime = Sat Jun 02 14:43:15 UTC 2018
mZxid = 0x20000000f
mtime = Sat Jun 02 14:45:06 UTC 2018
pZxid = 0x20000000e
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
[zk: localhost:2181(CONNECTED) 22] 
#删除触发watcher事件
[zk: localhost:2181(CONNECTED) 23] get /longfei watch
new_test
cZxid = 0x20000000e
ctime = Sat Jun 02 14:43:15 UTC 2018
mZxid = 0x20000000f
mtime = Sat Jun 02 14:45:06 UTC 2018
pZxid = 0x20000000e
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
[zk: localhost:2181(CONNECTED) 24] delete /longfei

WATCHER::

WatchedEvent state:SyncConnected type:NodeDeleted path:/longfei
[zk: localhost:2181(CONNECTED) 25]
  • ACL权限控制

ZK的节点有5种操作权限:CREATEREADWRITEDELETEADMIN 也就是 增、删、改、查、管理权限,这5种权限简写为crwda(即:每个单词的首字符缩写)。 注:这5种权限中,delete是指对子节点的删除权限,其它4种权限指对自身节点的操作权限

身份的认证有4种方式:

  • world:默认方式,相当于全世界都能访问
  • auth:代表已经认证通过的用户(cli中可以通过addauth digest user:pwd 来添加当前上下文中的授权用户)
  • digest:即用户名:密码这种方式认证,这也是业务系统中最常用的
  • ip:使用Ip地址认证

使用[scheme:id:permissions]来表示acl权限

getAcl:获取某个节点的acl权限信息
#获取节点权限信息默认为 world:cdrwa任何人都可以访问
[zk: localhost:2181(CONNECTED) 34] getAcl /merryyou
'world,'anyone
: cdrwa
setAcl 设置权限
[zk: localhost:2181(CONNECTED) 35] create /merryyou/test test
Created /merryyou/test
[zk: localhost:2181(CONNECTED) 36] getAcl /merryyou/test
'world,'anyone
: cdrwa
#设置节点权限 crwa 不允许删除
[zk: localhost:2181(CONNECTED) 37] setAcl /merryyou/test world:anyone:crwa
cZxid = 0x200000018
ctime = Sat Jun 02 16:18:18 UTC 2018
mZxid = 0x200000018
mtime = Sat Jun 02 16:18:18 UTC 2018
pZxid = 0x200000018
cversion = 0
dataVersion = 0
aclVersion = 1
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
#查询刚才设置的acl权限信息 crwa 没有删除权限
[zk: localhost:2181(CONNECTED) 38] getAcl /merryyou/test
'world,'anyone
: crwa
[zk: localhost:2181(CONNECTED) 39] 
[zk: localhost:2181(CONNECTED) 39] create /merryyou/test/abc abc
Created /merryyou/test/abc
#删除子节点的时候提交权限不足
[zk: localhost:2181(CONNECTED) 40] delete /merryyou/test/abc
Authentication is not valid : /merryyou/test/abc
#设置节点的权限信息为rda
[zk: localhost:2181(CONNECTED) 41] setAcl /merryyou/test world:anyone:rda 
cZxid = 0x200000018
ctime = Sat Jun 02 16:18:18 UTC 2018
mZxid = 0x200000018
mtime = Sat Jun 02 16:18:18 UTC 2018
pZxid = 0x20000001a
cversion = 1
dataVersion = 0
aclVersion = 2
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1
[zk: localhost:2181(CONNECTED) 42] getAcl /merryyou/test
'world,'anyone
: dra
#可以成功删除
[zk: localhost:2181(CONNECTED) 43] delete /merryyou/test/abc             
[zk: localhost:2181(CONNECTED) 46] ls /merryyou/test
[]
[zk: localhost:2181(CONNECTED) 47] 
#设置节点信息为a admin
[zk: localhost:2181(CONNECTED) 47] setAcl /merryyou/test world:anyone:a  
cZxid = 0x200000018
ctime = Sat Jun 02 16:18:18 UTC 2018
mZxid = 0x200000018
mtime = Sat Jun 02 16:18:18 UTC 2018
pZxid = 0x20000001d
cversion = 2
dataVersion = 0
aclVersion = 3
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
#获取 设置都提示权限不足
[zk: localhost:2181(CONNECTED) 49] get /merryyou/test
Authentication is not valid : /merryyou/test
[zk: localhost:2181(CONNECTED) 50] set /merryyou/test 123
Authentication is not valid : /merryyou/test
[zk: localhost:2181(CONNECTED) 51]