背景

最近打算好好学习一下zookeeper,那么第一步就是要搭建一个环境。
单机版的比较简单,这里就不介绍了。
集群版本的,看网上的教程也蛮简单的,但是自己动起手来还是会遇到各种问题,这里做一下记录。

环境介绍

1、3个阿里云服务器,其中2个ECS,1个轻量服务器。
2、zookeeper使用最新版本 https://archive.apache.org/dist/zookeeper/stable/apache-zookeeper-3.5.5-bin.tar.gz
3、3台云主机的Java使用jdk1.8版本。

步骤

1、检查各个主机的Java环境是否正常

2、下载最新版本zookeeper,并解压,3台机器最好采用统一的目录,我采用的目录

/enzi/zk/apache-zookeeper-3.5.5-bin

3、3个机器,分别创建zookeeper的数据目录和日志目录

/enzi/zk/data/enzi/zk/log

4、进入程序目录下,3个机器操作都一样

/enzi/zk/apache-zookeeper-3.5.5-bin/conf

复制zoo_sample.cfg为zoo.cfg,并修改zoo.cfg内容如下:

# 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=/enzi/zk/datadataLogDir=/enzi/zk/log# the port at which the clients will connectclientPort=2181#admin.serverPort=8089# 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=ip1:2888:3888server.2=ip2:2888:3888server.3=ip3:2888:3888quorumListenOnAllIPs=true

5、创建myid文件
分别在对应的data目录下,为自己的服务指定myid,内容就是zoo.cfg里面server.后面的序号,分别是1,2,3,ip1机器下myid内容为1,依次类推。

6、依次启动3个机器zookeeper服务

bin/zkServer.sh start

如果想看有没有什么错误信息可以使用如下命令,在调试阶段建议使用这个命令。

bin/zkServer.sh start-foreground

7、查看是否启动成功

mode:follower,代表这个服务是follower节点。
这里server.1是leader节点。

8、使用客户端进行连接,这里采用的windows的客户端

zkCli.cmd -server ip1:2181,ip2:2181,ip3:2181

使用命令,创建临时节点如下

注意:对于阿里云,因为防火墙或者ECS的安全策略来说,会导致集群之间的接口连接失败。

1、轻量服务器来说需要把2181,2888,3888增加到防火墙上。

2、对于ESC需要把2181,2888,3888的安全规则,入口和出口都设置为允许。