Zookeeper基础使用

部署和运行

脚本

ZooKeeper的bin目录下的脚本见下表

脚本 说明
zkCleanup 清理ZooKeeper历史数据,包括事务日志文件和快照数据文件
zkCli ZooKeeper的一个简易客户端
zkEnv 设置ZooKeeper的环境变量
zkServer ZooKeeper服务器的启动、停止和重启

启动服务

常见的启动方式有两种:

  1. Java命令行, 在ZooKeeper目录下执行如下命令

    java -cp zookeeper-3.4.6.jar:lib
  2. 使用自带脚本启动,在ZooKeeper目录下执行如下命令:

    ./bin/zkServer.sh

停止服务

可以使用zkServer脚本的stop命令来停止服务。

sh bin/zkServer.sh stop

基本指令

客户端连接

Zookeeper自带的脚本里有个zkCli,是ZooKeeper的一个简易客户端, 使用方式:

zkCli.sh [-server ip:port]

运行如下指令启动客户端可以连接到默认服务器localhost:2181

bin/zkCli.sh

看到下面输表明连接正常:

Connecting to localhost:2181
2019-03-27 17:05:34,602 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2019-03-27 17:05:34,605 [myid:] - INFO [main:Environment@100] - Client environment:host.name=WANJP3
2019-03-27 17:05:34,605 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_20
2019-03-27 17:05:34,607 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2019-03-27 17:05:34,608 [myid:] - INFO [main:Environment@100] - Client environment:java.home=C:\Program Files\Java\jre1.8.0_20
2019-03-27 17:05:34,608 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=D:\zookeeper-3.4.6\bin\..\build\classes;D:\zookeeper-3.4.6\bin\..\build\lib\*;D:\zookeeper-3.4.6\bin\..\zookeeper-3.4.6.jar;D:\zookeeper-3.4.6\bin\..\lib\jline-0.9.94.jar;D:\zookeeper-3.4.6\bin\..\lib\log4j-1.2.16.jar;D:\zookeeper-3.4.6\bin\..\lib\netty-3.7.0.Final.jar;D:\zookeeper-3.4.6\bin\..\lib\slf4j-api-1.6.1.jar;D:\zookeeper-3.4.6\bin\..\lib\slf4j-log4j12-1.6.1.jar;D:\zookeeper-3.4.6\bin\..\conf
2019-03-27 17:05:34,609 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:\Oracle\product\11.2.0\client_2\bin;D:\Oracle\Oracle64;D:\Oracle;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\node\;D:\VSCode\bin;d:\eclipse\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\TortoiseSVN\bin;D:\gradle-4.10\bin;D:\gradle-4.10\;D:\node\node_global_modules\;C:\Program Files\Git\bin\;D:\apache-maven-3.5.4\bin;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Users\nan\AppData\Local\Microsoft\WindowsApps;C:\Users\nan\AppData\Roaming\npm;;.
2019-03-27 17:05:34,610 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=C:\Users\nan\AppData\Local\Temp\
2019-03-27 17:05:34,610 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2019-03-27 17:05:34,611 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Windows 8.1
2019-03-27 17:05:34,611 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2019-03-27 17:05:34,611 [myid:] - INFO [main:Environment@100] - Client environment:os.version=6.3
2019-03-27 17:05:34,612 [myid:] - INFO [main:Environment@100] - Client environment:user.name=nan
2019-03-27 17:05:34,612 [myid:] - INFO [main:Environment@100] - Client environment:user.home=C:\Users\nan
2019-03-27 17:05:34,621 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=D:\zookeeper-3.4.6
2019-03-27 17:05:34,623 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@277050dc
Welcome to ZooKeeper!
2019-03-27 17:05:34,714 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@975] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2019-03-27 17:05:34,716 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@852] - Socket connection established
to 127.0.0.1/127.0.0.1:2181, initiating session
JLine support is enabled
[zk: localhost:2181(CONNECTING) 0] 2019-03-27 17:05:34,856 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x169be6525760000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null
ls
[zk: localhost:2181(CONNECTED) 1]

创建

通过zkCli连接服务器后,使用```create``可以创建一个ZooKeeper节点,使用方式:

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

-s 顺序节点, -e临时节点,如果补添加创建的就是持久节点

acl是权限控制,缺省情况下,不做任何权限控制

读取

读取指令有两种,ls列出指定节点下的子节点, get获取指定节点的数据内容和属性信息,两个指令的使用方式如下:

ls path [wacth]
get path [watch]
[zk: localhost:2181(CONNECTED) 21] create /test 123
Created /test
[zk: localhost:2181(CONNECTED) 22] ls /test
[]
[zk: localhost:2181(CONNECTED) 23] get /test
123
cZxid = 0x28
ctime = Wed Mar 27 17:42:45 CST 2019
mZxid = 0x28
mtime = Wed Mar 27 17:42:45 CST 2019
pZxid = 0x28
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0

cZxid创建该节点的事务ID,mZxid最后一次更新该节点的事务ID

更新

set命令用于更新指定节点的数据内容,使用方式如下:

set path data [version]
[zk: localhost:2181(CONNECTED) 24] set /test 111
cZxid = 0x28
ctime = Wed Mar 27 17:42:45 CST 2019
mZxid = 0x29
mtime = Wed Mar 27 17:49:59 CST 2019
pZxid = 0x28
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
[zk: localhost:2181(CONNECTED) 25] get /test
111
cZxid = 0x28
ctime = Wed Mar 27 17:42:45 CST 2019
mZxid = 0x29
mtime = Wed Mar 27 17:49:59 CST 2019
pZxid = 0x28
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
[zk: localhost:2181(CONNECTED) 26]

执行更新命令后,节点/test的数据已经变成111,````cZxid没有变化,mZxid变成了0x29,dataVersion```从0变成了1。

删除

delete命令用于删除指定节点,使用方式如下:

delete path [version]

删除节点时,该节点不能有子节点。如下:

[zk: localhost:2181(CONNECTED) 27] create /test/test-1 101
Created /test/test-1
[zk: localhost:2181(CONNECTED) 28] ls /test
[test-1]
[zk: localhost:2181(CONNECTED) 29] delete /test
Node not empty: /test
[zk: localhost:2181(CONNECTED) 30] delete /test/test-1
[zk: localhost:2181(CONNECTED) 31] ls
[zk: localhost:2181(CONNECTED) 32] ls /test
[]
[zk: localhost:2181(CONNECTED) 33] delete /test
[zk: localhost:2181(CONNECTED) 34] ls /test
Node does not exist: /test
上一篇:使用node.js 脚手架搭建Vue项目


下一篇:SV学习笔记—子程序