一、Zookeeper的安装
1.安装jdk1.8
2.解压xxxx.tar.gz
3.配置环境变量
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_131
export PATH=$PATH:$JAVA_HOME/bin
更新修改的配置#source /etc/profile
4.查看$PATH输出
二、Zookeeper的配置
1.独立模式
修改conf下zoo_sample.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=/opt/data/zookeeper
# 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
主要修改dataDir的值。
启动zookeeper:bin/zkServer.sh start
查看zookeeper状态:bin/zkServer.sh status
2.集群模式
zookeeper集群被称为群组。zookeeper使用的是一致性协议,所以建议每个群组里应该包含奇数个节点(比如3个、5个等),因为只有当群组里的大多数节点(也就是法定人数)处于可用状态,zookeeper才能处理外部的请求。也就是说,如果群组里包含3个节点,那么它允许1个节点失效。如果群组包含5个节点,那么它允许2个节点失效。
假设我们在node01上安装了独立模式的zookeeper,现在将node01下解压后的安装包复制到其他机器(比如node02、node03)
分别在node01、node02、node03三台机器上的dataDir目录即/opt/data/zookeeper下新建名为myid的文件,用于指明自己的ID。文件内容分别为1、2、3;
分别将conf/zoo.cfg配置改为如下,添加了三行
server.1=node01:2888:3888
server.2=node02:2888:3888
server.3=node03:2888:3888
# 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=/opt/data/zookeeper
# 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=node01:2888:3888
server.2=node02:2888:3888
server.3=node03:2888:3888
服务器地址遵循server.X=hostname:peerPort:leaderPort格式,各个参数说明如下:
X:服务器的ID,也就是myid文件内容,它必须是个整数,但不一定要从0开始,也不要求是连续的;
hostname:服务器的机器名或IP地址;
peerPort:用于节点间通信的TCP端口;
leaderPort:用于首领选举的TCP端口。
客户端只需通过clientPort就能连接到群组,而群组节点间通信则需要同时用到这3个端口(peerPort、leaderPort、clientPort)
分别启动zk,至此集群搭建完毕。
客户端连接到指定的服务端:zkCli.sh -server ip:port