目录
本节概览:介绍了部署zookeeper的单点和集群两种模式。
1 部署zookeeper
1.1 单机部署
1、下载
zookeeper-3.4.10.tar.gz
2、解压文件
3、修改conf/zoo.cfg
1 2 | dataDir=/Users/hang/zookeeper clientPort=2181 |
4、启动
1 2 3 4 5 | $ sh bin/zkServer.sh start ZooKeeper JMX enabled by default Using config: /Users/hang/local/zookeeper-3.4.10/bin/../conf/zoo.cfg -n Starting zookeeper ... STARTED |
5、测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $ telnet localhost 2181 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. srvr Zookeeper version: 3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT Latency min/avg/max: 0/0/0 Received: 1 Sent: 0 Connections: 1 Outstanding: 0 Zxid: 0x0 Mode: standalone Node count: 4 Connection closed by foreign host |
1.2 集群部署
和单点部署一样,只是配置文件不同,如下:
1、第一步 假设存在3台zookeeper节点的配置文件内容为:
1 2 3 4 5 | dataDir=/home/zookeeper clientPort=2181 server.1=zk1:2999:3999 server.2=zk2:2999:3999 server.3=zk3:2999:3999 |
2、第二步 在dataDir对应的目录下创建一个名为myid的文件,内容只有1行,就是上面配置中的server.x中的x,记录的是节点id。
2 常用命令
2.1 启动客户端
1 | sh zkCli.sh |
2.2 创建节点
通过create命令创建
1 | create [-s] [-e] path data acl |
必须指定data,否则无法创建成功,如下创建一个名字为learnzk的节点:
1 | create /learnzk 123 |
2.3 读取节点
1、ls命令
1 | ls / |
2、get命令
1 2 3 4 5 6 7 8 9 10 11 12 13 | [zk: localhost:2181(CONNECTED) 14] get /learnzk 123 cZxid = 0x17e ctime = Mon Mar 19 23:11:48 CST 2018 mZxid = 0x17e mtime = Mon Mar 19 23:11:48 CST 2018 pZxid = 0x17e cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0 |
2.4 更新节点
通过set 命令进行更新
1 | set path data [version] |
如下
1 2 3 4 5 6 7 8 9 10 11 12 | [zk: localhost:2181(CONNECTED) 15] set /learnzk 456 cZxid = 0x17e ctime = Mon Mar 19 23:11:48 CST 2018 mZxid = 0x17f mtime = Mon Mar 19 23:28:50 CST 2018 pZxid = 0x17e cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0 |
2.5 删除节点
delete删除节点命令
1 | delete path [version] |
如下删除learnzk的子节点firstNode:
1 | delete /learnzk/firstNode |