运行 zkCli.sh –server <ip>进入命令行工具
1、使用 ls 命令来查看当前 ZooKeeper 中所包含的内容:
[zk: 202.115.36.251:2181(CONNECTED) 1] ls /
2、创建一个新的 znode ,使用 create /zk myData 。这个命令创建了一个新的 znode 节点“ zk ”以及与它关联的字符串:
[zk: 202.115.36.251:2181(CONNECTED) 2] create /zk "myData“
3、我们运行 get 命令来确认 znode 是否包含我们所创建的字符串:
[zk: 202.115.36.251:2181(CONNECTED) 3] get /zk
#监听这个节点的变化,当另外一个客户端改变/zk时,它会打出下面的
#WATCHER::
#WatchedEvent state:SyncConnected type:NodeDataChanged path:/zk
[zk: localhost:2181(CONNECTED) 4] get /zk watch
4、下面我们通过 set 命令来对 zk 所关联的字符串进行设置:
[zk: 202.115.36.251:2181(CONNECTED) 4] set /zk "zsl“
5、下面我们将刚才创建的 znode 删除:
[zk: 202.115.36.251:2181(CONNECTED) 5] delete /zk
6、删除节点:rmr
[zk: 202.115.36.251:2181(CONNECTED) 5] rmr /zk
客户端
- 使用客户端连接
[root@hadoop bin]# cd /apps/zookeeper/zookeeper-3.4.5/bin
[root@hadoop bin]# ./zkCli.sh
Connecting to localhost:2181
2017-08-28 22:28:28,598 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2017-08-28 22:28:28,601 [myid:] - INFO [main:Environment@100] - Client environment:host.name=google-public-dns-a.google.com
2017-08-28 22:28:28,601 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_45
2017-08-28 22:28:28,602 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-08-28 22:28:28,602 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.7.0_45/jre
2017-08-28 22:28:28,602 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/apps/zookeeper/zookeeper-3.4.5/bin/../build/classes:/apps/zookeeper/zookeeper-3.4.5/bin/../build/lib/*.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../conf:
2017-08-28 22:28:28,603 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-08-28 22:28:28,603 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-08-28 22:28:28,603 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2017-08-28 22:28:28,604 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2017-08-28 22:28:28,604 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2017-08-28 22:28:28,604 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-573.el6.x86_64
2017-08-28 22:28:28,605 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2017-08-28 22:28:28,605 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2017-08-28 22:28:28,605 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/apps/zookeeper/zookeeper-3.4.5/bin
2017-08-28 22:28:28,607 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@10636a7e
Welcome to ZooKeeper!
2017-08-28 22:28:28,922 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@966] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2017-08-28 22:28:28,961 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@849] - Socket connection established to localhost/127.0.0.1:2181, initiating session
JLine support is enabled
[zk: localhost:2181(CONNECTING) 0] 2017-08-28 22:28:29,064 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x15e2bcce47b0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
连接成功,连接到本机的zookeeper
- 连接到指定机器的zookeeper
[zk: localhost:2181(CONNECTED) 0] connect mini2:2181
2017-08-28 22:31:56,498 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@509] - EventThread shut down
2017-08-28 22:31:56,499 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x15e2bcce47b0000 closed
2017-08-28 22:31:56,503 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=mini2:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@c04fe90
ZooKeeper -server host:port cmd args
connect host:port
get path [watch]
ls path [watch]
set path data [version]
rmr path
delquota [-n|-b] path
quit
printwatches on|off
create [-s] [-e] path data acl
stat path [watch]
close
ls2 path [watch]
history
listquota path
setAcl path acl
getAcl path
sync path
redo cmdno
addauth scheme auth
delete path [version]
setquota -n|-b val path
[zk: mini2:2181(CONNECTING) 1] 2017-08-28 22:31:56,509 [myid:] - INFO [main-SendThread(mini2:2181):ClientCnxn$SendThread@966] - Opening socket connection to server mini2/192.168.25.202:2181. Will not attempt to authenticate using SASL (unknown error)
2017-08-28 22:31:56,510 [myid:] - INFO [main-SendThread(mini2:2181):ClientCnxn$SendThread@849] - Socket connection established to mini2/192.168.25.202:2181, initiating session
2017-08-28 22:31:56,517 [myid:] - INFO [main-SendThread(mini2:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server mini2/192.168.25.202:2181, sessionid = 0x25e2bc9b9bb0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: mini2:2181(CONNECTED) 1]
- 查看客户端使用帮助
[zk: mini2:2181(CONNECTED) 1] help
ZooKeeper -server host:port cmd args
connect host:port
get path [watch]
ls path [watch]
set path data [version]
rmr path
delquota [-n|-b] path
quit
printwatches on|off
create [-s] [-e] path data acl
stat path [watch]
close
ls2 path [watch]
history
listquota path
setAcl path acl
getAcl path
sync path
redo cmdno
addauth scheme auth
delete path [version]
setquota -n|-b val path
[zk: mini2:2181(CONNECTED) 2]
zookeeper特性
zookeeper特性
1、Zookeeper:一个leader,多个follower组成的集群
2、全局数据一致:每个server保存一份相同的数据副本,client无论连接到哪个server,数据都是一致的
3、分布式读写,更新请求转发,由leader实施
4、更新请求顺序进行,来自同一个client的更新请求按其发送顺序依次执行
5、数据更新原子性,一次数据更新要么成功,要么失败
6、实时性,在一定时间范围内,client能读到最新数据zookeeper数据结构
1、层次化的目录结构,命名符合常规文件系统规范(见下图)
2、每个节点在zookeeper中叫做znode,并且其有一个唯一的路径标识
3、节点Znode可以包含数据和子节点(但是EPHEMERAL类型的节点不能有子节点,下一页详细讲解)
4、客户端应用可以在节点上设置监视器(后续详细讲解)
- 创建数据节点并设置值
[zk: mini2:2181(CONNECTED) 2] create /app1 "this is app1 node"
Created /app1
[zk: mini2:2181(CONNECTED) 4] create /app1/server01 "hello192.168.25.202"
Created /app1/server01
[zk: mini2:2181(CONNECTED) 6] ls /
[app1, zookeeper]
[zk: mini2:2181(CONNECTED) 7] ls /app1
[server01]
- 获取节点的值
[zk: mini2:2181(CONNECTED) 8] get /app1
"this
cZxid = 0x200000004
ctime = Mon Aug 28 22:49:47 EDT 2017
mZxid = 0x200000004
mtime = Mon Aug 28 22:49:47 EDT 2017
pZxid = 0x200000005
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 1
[zk: mini2:2181(CONNECTED) 9] get /app1/server01
"hello192.168.25.202"
cZxid = 0x200000005
ctime = Mon Aug 28 22:50:36 EDT 2017
mZxid = 0x200000005
mtime = Mon Aug 28 22:50:36 EDT 2017
pZxid = 0x200000005
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 21
numChildren = 0
[zk: mini2:2181(CONNECTED) 10]
节点类型
- Znode有两种类型:
短暂(ephemeral)(断开连接自己删除)
持久(persistent)(断开连接不删除) - Znode有四种形式的目录节点(默认是persistent )
PERSISTENT
PERSISTENT_SEQUENTIAL
(持久序列/test0000000019 )
EPHEMERAL
EPHEMERAL_SEQUENTIAL
- 创建znode时设置顺序标识,znode名称后会附加一个值,顺序号是一个单调递增的计数器,由父节点维护
- 在分布式系统中,顺序号可以被用于为所有的事件进行全局排序,这样客户端可以通过顺序号推断事件的顺序
短暂节点
- 创建短暂的节点
[zk: mini2:2181(CONNECTED) 10] create -e /app-e "nihao"
Created /app-e
[zk: mini2:2181(CONNECTED) 11] ls /
[app1, app-e, zookeeper]
- 退出客户端再重新连接
[zk: mini2:2181(CONNECTED) 13] quit
[root@hadoop bin]# ./zkCli.sh
- 查看数据
[zk: localhost:2181(CONNECTED) 0] ls /
[app1, zookeeper]
app-e
节点已经不存在
- 短暂节点下不能创建永久节点
[zk: localhost:2181(CONNECTED) 1] create -e /abd nihao
Created /abd
[zk: localhost:2181(CONNECTED) 2] create /abd/abc nihao
Ephemerals cannot have children: /abd/abc
[zk: localhost:2181(CONNECTED) 3]
带序号的节点
- 创建带序号的节点
[zk: localhost:2181(CONNECTED) 1] create /test 888
Created /test
[zk: localhost:2181(CONNECTED) 2] create -s /test/aa 888
Created /test/aa0000000000
[zk: localhost:2181(CONNECTED) 3] create -s /test/bb 888
Created /test/bb0000000001
[zk: localhost:2181(CONNECTED) 4] create -s /test/aa 888
Created /test/aa0000000002
[zk: localhost:2181(CONNECTED) 5]
- 修改目录,序号重新开始计算
[zk: localhost:2181(CONNECTED) 5] create -s /app1/aa 888
Created /app1/aa0000000001
[zk: localhost:2181(CONNECTED) 6] create -s /app1/bb 888
Created /app1/bb0000000002
[zk: localhost:2181(CONNECTED) 7] create -s /app1/aa 888
Created /app1/aa0000000003
[zk: localhost:2181(CONNECTED) 8]
修改节点的值
[zk: localhost:2181(CONNECTED) 8] set /app1 yyyy
cZxid = 0x200000004
ctime = Mon Aug 28 22:49:47 EDT 2017
mZxid = 0x200000010
mtime = Mon Aug 28 23:09:54 EDT 2017
pZxid = 0x20000000f
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
[zk: localhost:2181(CONNECTED) 9] get /app1
yyyy
cZxid = 0x200000004
ctime = Mon Aug 28 22:49:47 EDT 2017
mZxid = 0x200000010
mtime = Mon Aug 28 23:09:54 EDT 2017
pZxid = 0x20000000f
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
[zk: localhost:2181(CONNECTED) 10]
注意:在集群中通过连接到一台主机的客户端修改了节点的值,在连接到另一台主机的客户端就能马上查询到修改后的值
节点监听
- 在连接mini1的客户端上执行
[zk: localhost:2181(CONNECTED) 10] get /app1 watch
yyyy
cZxid = 0x200000004
ctime = Mon Aug 28 22:49:47 EDT 2017
mZxid = 0x200000010
mtime = Mon Aug 28 23:09:54 EDT 2017
pZxid = 0x20000000f
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
- 在连接mini2的客户端上执行
[zk: localhost:2181(CONNECTED) 0] set /app1 uuyy
cZxid = 0x200000004
ctime = Mon Aug 28 22:49:47 EDT 2017
mZxid = 0x200000012
mtime = Mon Aug 28 23:15:51 EDT 2017
pZxid = 0x20000000f
cversion = 4
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
- 那么在连接mini1的客户端上会有输出
[zk: localhost:2181(CONNECTED) 11]
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/app1
注意:监听是在获取数据的时候设置,监听只能是生效一次
监听子节点
- 在连接mini1的客户端上执行
[zk: localhost:2181(CONNECTED) 11] ls /app1 watch
[bb0000000002, aa0000000001, aa0000000003, server01]
- 在连接mini2的客户端上执行
[zk: localhost:2181(CONNECTED) 2] create /app1/aa nihao
Created /app1/aa
- 那么在连接mini1的客户端上会有输出
[zk: localhost:2181(CONNECTED) 12]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/app1