1. 安装
brew install zookeeper
安装后 zookeeper 配置文件位置:/usr/local/etc/zookeeper
配置文件zookeeper详解
f milliseconds of each ticktickTime=2000# The number of ticks that the initial# synchronization phase can takeinitLimit=10# The number of ticks that can pass between# sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just# example sakes.dataDir=/usr/local/var/run/zookeeper/data/zk1# the port at which the clients will connectclientPort=2182server.1=127.0.0.1:2888:3888# the maximum number of client connections.# increase this if you need to handle more clients#maxClientCnxns=60
参数详解:
**tickTime**:ZK 中的一个时间单元。ZK 中所有时间都是以这个时间单元为基础,进行整数倍配置的。例如,session 的最小超时时间是 2*tickTime。**initLimit**:Follower 在启动过程中,会从 Leader 同步所有最新数据,然后确定自己能够对外服务的起始状态。Leader 允许 Fower 在initLimit时间内完成这个工作。通常情况下,我们不用太在意这个参数的设置。如果 ZK 集群的数据量确实很大了,Fower 在启动的时候,从 Leader上同步数据的时间也会相应变长,因此在这种情况下,有必要适当调大这个参数了。(No Java system property)**syncLimit**:在运行过程中,Leader 负责与 ZK 集群中所有机器进行通信,例如通过一些心跳检测机制,来检测机器的存活状态。如果 Leader 发出心跳包在syncLimit之后,还没有从F那里收到响应,那么就认为这个F已经不在线了。注意:不要把这个参数设置得过大,否则可能会掩盖一些问题。(No Java system property)**dataDir**:存储快照文件 snapshot 的目录。默认情况下,事务日志也会存储在这里。建议同时配置参数dataLogDir, 事务日志的写性能直接影响 zk 性能。**clientPort**: 客户端连接 server 的端口,即对外服务端口,一般设置为 2181 吧。**server.x=[hostname]:nnnnn[:nnnnn]**:这里的 x 是一个数字,与 myid 文件中的 id 是一致的。右边可以配置两个端口,第一个端口用于 Fower 和 Leader 之间的数据同步和其它通信,第二个端口用于 Leader 选举过程中投票通信。
2. 配置zookeeper
单机环境
配置配置文件
tickTime=2000initLimit=10syncLimit=5dataDir=/usr/local/var/run/zookeeper/data/zk1clientPort=2182server.1=127.0.0.1:2888:3888
添加myid文件
cd /usr/local/var/run/zookeeper/data/zk1(配置文件中的dataDir)vim myid 输入 1 保存,1代表机器编号。
启动服务
zkServer start zk0.cfg
查看是否启动
zkServer status zk0.cfg或telnet 127.0.0.1 2181stat
关闭
zkServer stop zk0.cfg
连接到客户端
zkCli
伪集群
伪集群就是在一台机器上部署多个 zookeeper 应用,部署之前需要有 jdk 环境
步骤
- 下载好
zookeeper-3.4.9.tar.gz,然后解压tar zxvf zookeeper-3.4.9.tar.gz配置文件中配置多个服务器。 - 进入 zk 中的 conf 目录下输入
cp zoo-sample.cfg zoo1.cfg、cp zoo-sample.cfg zoo2.cfg、cp zoo-sample.cfg zoo3.cfg - 分别对zoo1、2、3文件进行编辑
# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial# synchronization phase can takeinitLimit=10# The number of ticks that can pass between# sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just# example sakes.dataDir=/apps/servers/data/d_1dataLogDir=/apps/servers/logs/logs_1# the port at which the clients will connectclientPort=2181#不同zoo.cfg修改自己的属性和端口号# 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=1server.1=localhost:2187:2887server.2=localhost:2188:2888server.3=localhost:2189:2889
- 修改之后分别创建data目录和日志目录
mkdir /apps/servers/data/d_1Mkdir /apps/servers/data/d_1mkdir /apps/servers/data/d_1mkdir /apps/servers/logs/logs_1mkdir /apps/servers/logs/logs_1mkdir /apps/servers/logs/logs_1echo "1" > /apps/servers/data/d_1/myidecho "2" >/apps/servers/data/d_2/myidecho "3" >/apps/servers/data/d_3/myid
- 进入bin目录下输入命令 分别进行启动
zkServer.sh start ../conf/zoo1.cfgzkServer.sh start ../conf/zoo2.cfgzkServer.sh start ../conf/zoo3.cfg
通过命令检测是否成功:
注意防火墙和配置是否成功,这个是部署成功的关键
zkServer.sh status 或者 zkCli.sh -server localhost:2181``,`` localhost:218``2,`` localhost:218``3 是否可以连接成功
