zk安装与初体验

安装zk

  1. brew install zookeeper
  2. 安装后默认目录为/usr/local/Cellar/zookeeper/3.4.9
  3. 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一些简单的命令

  1. conf 输出相关服务配置的详细信息。
  2. cons 列出所有连接到服务器的客户端的完全的连接 / 会话的详细信息。包括“接受 / 发送”的包数量、会话 id 、操作延迟、最后的操作执行等等信息。
  3. dump 列出未经处理的会话和临时节点。
  4. envi 输出关于服务环境的详细信息(区别于 conf 命令)
  5. reqs 列出未经处理的请求
  6. ruok 测试服务是否处于正确状态。如果确实如此,那么服务返回“imok ”,否则不做任何相应。
  7. stat 输出关于性能和连接的客户端的列表。
  8. wchs 列出服务器 watch 的详细信息。
  9. wchc 通过 session 列出服务器 watch 的详细信息,它的输出是一个与watch 相关的会话的列表。
  10. 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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值