安装zk
- brew install zookeeper
- 安装后默认目录为
/usr/local/Cellar/zookeeper/3.4.9
zk启动 停止
1.cd /usr/local/Cellar/zookeeper/3.4.9/bin
2../zkServer start
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Starting zookeeper … STARTED停止zk
./zkServer stop
终端如下显示➜ bin ./zkServer stop
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Stopping zookeeper ... STOPPED
➜ bin
3.默认配置
cd /usr/local/etc/zookeeper
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/var/run/zookeeper/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
4.zk一些简单的命令
- conf 输出相关服务配置的详细信息。
- cons 列出所有连接到服务器的客户端的完全的连接 / 会话的详细信息。包括“接受 / 发送”的包数量、会话 id 、操作延迟、最后的操作执行等等信息。
- dump 列出未经处理的会话和临时节点。
- envi 输出关于服务环境的详细信息(区别于 conf 命令)
- reqs 列出未经处理的请求
- ruok 测试服务是否处于正确状态。如果确实如此,那么服务返回“imok ”,否则不做任何相应。
- stat 输出关于性能和连接的客户端的列表。
- wchs 列出服务器 watch 的详细信息。
- wchc 通过 session 列出服务器 watch 的详细信息,它的输出是一个与watch 相关的会话的列表。
- wchp 通过路径列出服务器 watch 的详细信息。它输出一个与 session相关的路径。
用法 echo conf | nc localhost 2181
clientPort=2181
dataDir=/usr/local/var/run/zookeeper/data/version-2
dataLogDir=/usr/local/var/run/zookeeper/data/version-2
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=0
echo dump | nc 127.0.0.1 2181
SessionTracker dump:
Session Sets (0):
ephemeral nodes dump:
Sessions with Ephemerals (0):
启动zk后,输入以下命令
./zkCli.sh -server localhost:2181
终端输出:
./zkCli -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
Welcome to ZooKeeper!
JLine support is enabled
[zk: 127.0.0.1:2181(CONNECTING) 0]
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
此时连接到 zk服务器, 输入help 得到以下内容
[zk: 127.0.0.1:2181(CONNECTED) 1] 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
[zk: 127.0.0.1:2181(CONNECTED) 2]
用ls查看zk服务器的目录
[zk: 127.0.0.1:2181(CONNECTED) 5] ls/
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
创建新的节点
create /zk myData
再次输入 发现此时zk节点已经被创建
[zk: 127.0.0.1:2181(CONNECTED) 6] ls /
[zk, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 7]
通过 get /zk 来查看刚才创建的是否包含myData 字符串
[zk: 127.0.0.1:2181(CONNECTED) 7] get /zk
myData
cZxid = 0x6
ctime = Sun Nov 20 19:16:04 CST 2016
mZxid = 0x6
mtime = Sun Nov 20 19:16:04 CST 2016
pZxid = 0x6
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
通过 set /zk naonao 可以对刚才的关联字符串进行修改
[zk: 127.0.0.1:2181(CONNECTED) 8] set /zk naonao
cZxid = 0x6
ctime = Sun Nov 20 19:16:04 CST 2016
mZxid = 0x7
mtime = Sun Nov 20 19:36:52 CST 2016
pZxid = 0x6
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
再次查看 get /zk
[zk: 127.0.0.1:2181(CONNECTED) 10] get /zk
naonao
cZxid = 0x6
ctime = Sun Nov 20 19:16:04 CST 2016
mZxid = 0x7
mtime = Sun Nov 20 19:36:52 CST 2016
pZxid = 0x6
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
删除zk delete /zk
[zk: 127.0.0.1:2181(CONNECTED) 11] delete /zk
[zk: 127.0.0.1:2181(CONNECTED) 12] ls /
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 13]