1. 安装

  1. brew install zookeeper

安装后 zookeeper 配置文件位置:/usr/local/etc/zookeeper

配置文件zookeeper详解

  1. f milliseconds of each tick
  2. tickTime=2000
  3. # The number of ticks that the initial
  4. # synchronization phase can take
  5. initLimit=10
  6. # The number of ticks that can pass between
  7. # sending a request and getting an acknowledgement
  8. syncLimit=5
  9. # the directory where the snapshot is stored.
  10. # do not use /tmp for storage, /tmp here is just
  11. # example sakes.
  12. dataDir=/usr/local/var/run/zookeeper/data/zk1
  13. # the port at which the clients will connect
  14. clientPort=2182
  15. server.1=127.0.0.1:2888:3888
  16. # the maximum number of client connections.
  17. # increase this if you need to handle more clients
  18. #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

单机环境

配置配置文件

  1. tickTime=2000
  2. initLimit=10
  3. syncLimit=5
  4. dataDir=/usr/local/var/run/zookeeper/data/zk1
  5. clientPort=2182
  6. server.1=127.0.0.1:2888:3888

添加myid文件

  1. cd /usr/local/var/run/zookeeper/data/zk1(配置文件中的dataDir)
  2. vim myid 输入 1 保存,1代表机器编号。

启动服务

  1. zkServer start zk0.cfg

查看是否启动

  1. zkServer status zk0.cfg
  2. 或
  3. telnet 127.0.0.1 2181
  4. stat

关闭

  1. zkServer stop zk0.cfg

连接到客户端

  1. zkCli

伪集群

伪集群就是在一台机器上部署多个 zookeeper 应用,部署之前需要有 jdk 环境

步骤

  1. 下载好 zookeeper-3.4.9.tar.gz,然后解压 tar zxvf zookeeper-3.4.9.tar.gz 配置文件中配置多个服务器。
  2. 进入 zk 中的 conf 目录下输入cp zoo-sample.cfg zoo1.cfg 、cp zoo-sample.cfg zoo2.cfg、cp zoo-sample.cfg zoo3.cfg
  3. 分别对zoo1、2、3文件进行编辑
  1. # The number of milliseconds of each tick
  2. tickTime=2000
  3. # The number of ticks that the initial
  4. # synchronization phase can take
  5. initLimit=10
  6. # The number of ticks that can pass between
  7. # sending a request and getting an acknowledgement
  8. syncLimit=5
  9. # the directory where the snapshot is stored.
  10. # do not use /tmp for storage, /tmp here is just
  11. # example sakes.
  12. dataDir=/apps/servers/data/d_1
  13. dataLogDir=/apps/servers/logs/logs_1
  14. # the port at which the clients will connect
  15. clientPort=2181
  16. #不同zoo.cfg修改自己的属性和端口号
  17. # the maximum number of client connections.
  18. # increase this if you need to handle more clients
  19. #maxClientCnxns=60
  20. #
  21. # Be sure to read the maintenance section of the
  22. # administrator guide before turning on autopurge.
  23. #
  24. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  25. #
  26. # The number of snapshots to retain in dataDir
  27. #autopurge.snapRetainCount=3
  28. # Purge task interval in hours
  29. # Set to "0" to disable auto purge feature
  30. #autopurge.purgeInterval=1
  31. server.1=localhost:2187:2887
  32. server.2=localhost:2188:2888
  33. server.3=localhost:2189:2889
  1. 修改之后分别创建data目录和日志目录
  1. mkdir /apps/servers/data/d_1
  2. Mkdir /apps/servers/data/d_1
  3. mkdir /apps/servers/data/d_1
  4. mkdir /apps/servers/logs/logs_1
  5. mkdir /apps/servers/logs/logs_1
  6. mkdir /apps/servers/logs/logs_1
  7. echo "1" > /apps/servers/data/d_1/myid
  8. echo "2" >/apps/servers/data/d_2/myid
  9. echo "3" >/apps/servers/data/d_3/myid
  1. 进入bin目录下输入命令 分别进行启动
  1. zkServer.sh start ../conf/zoo1.cfg
  2. zkServer.sh start ../conf/zoo2.cfg
  3. zkServer.sh start ../conf/zoo3.cfg

通过命令检测是否成功:
注意防火墙和配置是否成功,这个是部署成功的关键

zkServer.sh status 或者 zkCli.sh -server localhost:2181``,`` localhost:218``2,`` localhost:218``3 是否可以连接成功