1. 下载
-
或者去这里现在你需要的版本
root@debian:~/soft/bk# wget http://mirrors.hust.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz
2. 安装和配置
限于测试环境,只在单机上部署3个实例,生产环境需要所有实例分散在不同的机器上
-
解压缩
# 解压缩 root@debian:~/soft/bk# tar zxvf zookeeper-3.4.12.tar.gz root@debian:~/soft/bk# cd zookeeper-3.4.12/ # 删除一些不要的文件,最后只留下面这些文件 root@debian:~/soft/bk/zookeeper-3.4.12# ls -l total 1464 drwxr-xr-x 2 psy psy 4096 Mar 27 12:32 bin drwxr-xr-x 2 psy psy 4096 Mar 27 12:32 conf drwxr-xr-x 4 psy psy 4096 Mar 27 12:32 lib -rw-rw-r-- 1 psy psy 1483366 Mar 27 12:32 zookeeper-3.4.12.jar
-
创建配置文件
root@debian:~/soft/bk/zookeeper-3.4.12# cd conf/ root@debian:~/soft/bk/zookeeper-3.4.12/conf# ls configuration.xsl log4j.properties zoo_sample.cfg root@debian:~/soft/bk/zookeeper-3.4.12/conf# cp zoo_sample.cfg zoo.cfg root@debian:~/soft/bk/zookeeper-3.4.12/conf# vim zoo.cfg
修改配置文件zoo.cfg的内容
# 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=/data/zookeeper/data/z1 # the port at which the clients will connect clientPort=2281 server.1=localhost:2888:3888 server.2=localhost:2889:3889 server.3=localhost:2890:3890 # 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
-
安装
root@debian:~/soft/bk# mkdir -p /usr/local/zookeeper_cluster root@debian:~/soft/bk# cp -r zookeeper-3.4.12 /usr/local/zookeeper_cluster/z1 root@debian:~/soft/bk# cp -r zookeeper-3.4.12 /usr/local/zookeeper_cluster/z2 root@debian:~/soft/bk# cp -r zookeeper-3.4.12 /usr/local/zookeeper_cluster/z3
z1, z2, z3 分别是三个实例
- 修改z2/conf/zoo.cfg, 修改dataDir和clientPort项
dataDir=/data/zookeeper/data/z2 # the port at which the clients will connect clientPort=2282
- 修改z3/conf/zoo.cfg, 修改dataDir和clientPort项
dataDir=/data/zookeeper/data/z3 # the port at which the clients will connect clientPort=2283
- 修改z2/conf/zoo.cfg, 修改dataDir和clientPort项
-
创建数据存储目录
root@debian:~/soft/bk/zookeeper-3.4.12# mkdir -p /data/zookeeper/data root@debian:~/soft/bk/zookeeper-3.4.12# cd /data/zookeeper/data/ root@debian:/data/zookeeper/data# mkdir z1 z2 z3
-
创建myid, id与zoo.cfg中server.X的x对应
root@debian:/data/zookeeper/data# echo 1 > z1/myid root@debian:/data/zookeeper/data# echo 2 > z2/myid root@debian:/data/zookeeper/data# echo 3 > z3/myid
3. 启动和测试
- 创建一个启动三个实例的sh, start.sh
./z1/bin/zkServer.sh start & ./z2/bin/zkServer.sh start & ./z3/bin/zkServer.sh start &
- 测试
一些操作命令root@debian:/usr/local/zookeeper_cluster/z1/bin# ./zkCli.sh -server 127.0.0.1:2281
[zk: 127.0.0.1:2281(CONNECTED) 2] create /zk_test hello Created /zk_test [zk: 127.0.0.1:2281(CONNECTED) 3] get /zk_test hello cZxid = 0x100000003 ctime = Wed Sep 19 18:04:00 CST 2018 mZxid = 0x100000003 mtime = Wed Sep 19 18:04:00 CST 2018 pZxid = 0x100000003 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 5 numChildren = 0 [zk: 127.0.0.1:2281(CONNECTED) 4] set /zk_test fuck cZxid = 0x100000007 ctime = Wed Sep 19 18:09:32 CST 2018 mZxid = 0x100000008 mtime = Wed Sep 19 18:09:40 CST 2018 pZxid = 0x100000007 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 4 numChildren = 0 [zk: 127.0.0.1:2281(CONNECTED) 5] get /zk_test fuck cZxid = 0x100000007 ctime = Wed Sep 19 18:09:32 CST 2018 mZxid = 0x100000008 mtime = Wed Sep 19 18:09:40 CST 2018 pZxid = 0x100000007 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 4 numChildren = 0 [zk: 127.0.0.1:2281(CONNECTED) 6] delete /zk_test [zk: 127.0.0.1:2281(CONNECTED) 7] ls / [zookeeper] [zk: 127.0.0.1:2281(CONNECTED) 8]