环境:CentOS Linux release 7.3.1611 (Core) 64位
JDK
因为ZooKeeper服务器需要运行在JVM之上,所以首先需要部署JDK,这里我们选取jdk-8u161-linux-x64.tar.gz
下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
pwd /app/workspace
tar -zxvf jdk-8u161-linux-x64.tar.gz
然后配置java环境变量
vim /etc/profile
#jdk
export JAVA_HOME=/app/workspace/jdk1.8.0_161
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ZoKeeper安装
下载ZooKeeper安装包
wget http://apache.claz.org/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
tar -zxvf zookeeper-3.4.11.tar.gz
设置zookeeper环境变量,在~/.bashrc
后边追加
export ZOOKEEPER_HOME=/app/workspace/zookeeper-3.4.11
export PATH=$PATH:$ZOOKEEPER_HOME/bin
之后就可以在终端启动ZooKeeper了
ZooKeeper部署
一般部署一个ZooKeeper高可用和可靠的系统,需要部署单个节点,三、五或七是最典型的节点数量。
下边以三个ZooKeeper服务器组成一个集合体为例介绍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=/app/zookeeper/data
dataLogDir=/app/zookeeper/logs
# 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
server.1=192.168.179.118:2888:3888
server.2=192.168.177.195:2888:3888
server.3=192.168.177.194:2888:3888
值得重点注意的一点是,所有三个机器都应该打开端口 2181、2888 和 3888。在本例中,端口 2181 由 ZooKeeper 客户端使用,用于连接到 ZooKeeper 服务器;端口 2888 由对等 ZooKeeper 服务器使用,用于互相通信;而端口 3888 用于领导者选举。您可以选择自己喜欢的任何端口。通常建议在所有 ZooKeeper 服务器上使用相同的端口。
创建一个/app/zookeeper/data/myid
server.1的myid里填1并保存,server.2和server.3里分别填2、3并保存。
依次启动改三个ZooKeeper服务器实例:
zkServer.sh start
输出:
ZooKeeper JMX enabled by default
Using config: /app/workspace/zookeeper-3.4.11/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
停止服务
zkServer.sh stop
输出:
ZooKeeper JMX enabled by default
Using config: /app/workspace/zookeeper-3.4.11/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper使用
运行zkCli.sh
默认连接本地的ZooKeeper服务器
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
查看节点:
[zk: localhost:2181(CONNECTED) 0] ls /
[ zookeeper]
创建节点
[zk: localhost:2181(CONNECTED) 2] create /mynode hello
Created /mynode
设置节点data
[zk: localhost:2181(CONNECTED) 3] set /mynode hello2
cZxid = 0x10000000f
ctime = Mon Mar 19 14:34:09 CST 2018
mZxid = 0x100000010
mtime = Mon Mar 19 14:35:11 CST 2018
pZxid = 0x10000000f
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
获取节点data
[zk: localhost:2181(CONNECTED) 4] get /mynode
hello2
cZxid = 0x10000000f
ctime = Mon Mar 19 14:34:09 CST 2018
mZxid = 0x100000010
mtime = Mon Mar 19 14:35:11 CST 2018
pZxid = 0x10000000f
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
删除节点
[zk: localhost:2181(CONNECTED) 5] rmr /mynode