安装zookeeper(一台机子,只能做伪集群):
(1)从apache的镜像网站下载zookeeper-3.4.7,解压到 /usr/zookeeper/ 目录,文件夹名为 zk3_4_7s1。
(2)修改/usr/zookeeper/zk3_4_7s1/conf/zook_sample.cfg,改名为zoo.cfg,内容如下:
# The number of milliseconds of each tick,单位:毫秒 tickTime=2000 # # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just example sakes. # 数据目录. 可以是任意目录. dataDir=/usr/zookeeper/zk3_4_7s2/data # # log目录, 同样可以是任意目录. 如果没有设置该参数, 将使用和dataDir相同的设置 dataLogDir=/usr/zookeeper/zk3_4_7s2/logs # # the port at which the clients will connect # 监听client连接的端口号 clientPort=2182 # # ===========(伪)集群配置================= ## 注:每个zookeeper server需更改dataDir, dataLogDir, clientPort参数即可. # # The number of ticks that the initial synchronization phase can take # zookeeper集群中包含多台server, 其中一台为leader, 集群中其余的server为follower, # initLimit参数配置初始化连接时, follower和leader之间的最长心跳时间. # 此时该参数设置为5, 说明时间限制为5倍tickTime, 即5*2000=10000ms=10s initLimit=5 # # The number of ticks that can pass between sending a request and getting an acknowledgement # 该参数配置leader和follower之间发送消息, 请求和应答的最大时间长度. # 此时该参数设置为2, 说明时间限制为2倍tickTime, 即4000ms syncLimit=5 # server.X=A:B:C 其中X是一个数字, 表示这是第几号server. # A是该server所在的IP地址. B配置该server和集群中的leader交换消息所使用的端口. C配置选举leader时所使用的端口. # 由于配置的是伪集群模式, 所以各个server的B, C参数必须不同. # 在前面设置的dataDir中新建myid文件, 写入一个数字, 该数字表示这是第几号server. # 该数字必须和zoo.cfg文件中的server.X中的X一一对应 server.0=127.0.0.1:8880:7770 server.1=127.0.0.1:8881:7771 server.2=127.0.0.1:8882:7772 # # # 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
(3)zoo.cfg配置好后,在配置的$dataDir路径中(/usr/zookeeper/zk3_4_7s1/data/)新建myid文件, 第一行写入数字0, 该数字表示这是第几号server. 该数字必须和zoo.cfg文件中的server.0中的0对应。
(4)配置另外两个zookeeper server:把/usr/zookeeper/zk3_4_7s1/分别复制到/usr/zookeeper/zk3_4_7s2和/usr/zookeeper/zk3_4_7s3中,分别修改/usr/zookeeper/zk3_4_7s2/conf/zoo.cfg和/usr/zookeeper/zk3_4_7s3/conf/zoo.cfg里的dataDir和dataLogDir以及clientPort三项,使三个server的配置里此三项的值彼此不同(如果是不同的主机,可以相同。本例是同一台主机,所以不能相同)。修改/usr/zookeeper/zk3_4_7s2/data/myid文件中的0为1,修改/usr/zookeeper/zk3_4_7s3/data/myid文件中的0为2 (myid文件中的数字必须和zoo.cfg文件中的server.x中的x对应);
(5)分别启动三个server的zookeeper服务:
./bin/zkServer.sh start
(6)选择任意一个server启动客户端连接server,比如选择第一个server启动客户端:
./bin/zkCli.sh -server localhost:2181