系统环境:Ubuntu Xenial 16.04 (LTS)
ZooKeeper版本:Version: 3.5.1-alpha
下载:http://apache.fayea.com/zookeeper/zookeeper-3.5.1-alpha/
安装ZooKeeper
[zhaojq@virtual-machine]# mkdir ZooKeeper
[zhaojq@virtual-machine/ZooKeeper]# tar -zxvf zookeeper-3.5.1-alpha.tar.gz
[zhaojq@virtual-machine/ZooKeeper]# ln -s zookeeper-3.5.1-alpha zookeeper
创建目录存储ZooKeeper服务器相关数据
[zhaojq@virtual-machine]# mkdir -p /var/zookeeper/data
创建一个myid文件,写入ID号,1代表集群中的第一台服务器
[zhaojq@virtual-machine]# cd /var/zookeeper/data/myid
[zhaojq@virtual-machine/var/zookeeper/data]# sudo vi myid
创建编辑conf/zoo.cfg文件
[zhaojq@virtual-machine/ZooKeeper]# cd zookeeper/conf/
[zhaojq@virtual-machine/ZooKeeper/zookeeper/conf]# cp zoo_sample.cfg zoo.cfg
[zhaojq@virtual-machine/ZooKeeper/zookeeper/conf]# vi zoo.cfg
tickTime=2000
initLimit=10
dataDir=/var/zookeeper/data
clientPort=2181
server.1=127.0.0.1:2888:3888
完成配置,运行zkServer.sh脚本启动服务
[zhaojq@virtual-machine/ZooKeeper]# cd zookeeper/bin/
[zhaojq@virtual-machine/ZooKeeper/zookeeper/bin]# sudo ./zkServer.sh start
ZooKeeper JMX enabled by default
/usr/bin/java
Using
config: /home/zhaojq/Workspace/ZooKeeper/zookeeper-3.5.1-alpha/bin/../conf/zoo.cfg
Starting
zookeeper ... already running as process 52647.
查看ZooKeeper服务器的状态
[zhaojq@virtual-machine/ZooKeeper/zookeeper/bin]# sudo ./zkServer.sh status
ZooKeeper JMX enabled by default
/usr/bin/java
Using config: /home/zhaojq/Workspace/ZooKeeper/zookeeper-3.5.1-alpha/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
在一台正在运行ZooKeeper服务器的机器上启动一个CTL客户端
[zhaojq@virtual-machine/ZooKeeper/zookeeper/bin]# sudo ./zkCli.sh
/usr/bin/java
Connecting to localhost:2181
......
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
创建、编辑和删除znode
[zk: localhost:2181(CONNECTED) 0] create /factory helloword
Created /factory
[zk: localhost:2181(CONNECTED) 1] get -s /factory
helloword
cZxid = 0x6
ctime = Wed Jul 06 11:30:48 CST 2016
mZxid = 0x6
mtime = Wed Jul 06 11:30:48 CST 2016
pZxid = 0x6
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0