最近打算好好学习一下zookeeper,那么第一步就是要搭建一个环境。
单机版的比较简单,这里就不介绍了。
集群版本的,看网上的教程也蛮简单的,可是本身动起手来仍是会遇到各类问题,这里作一下记录。html
一、3个阿里云服务器,其中2个ECS,1个轻量服务器。
二、zookeeper使用最新版本 https://archive.apache.org/dist/zookeeper/stable/apache-zookeeper-3.5.5-bin.tar.gz
三、3台云主机的Java使用jdk1.8版本。apache
一、检查各个主机的Java环境是否正常
二、下载最新版本zookeeper,并解压,3台机器最好采用统一的目录,我采用的目录windows
/enzi/zk/apache-zookeeper-3.5.5-bin
三、3个机器,分别建立zookeeper的数据目录和日志目录安全
/enzi/zk/data /enzi/zk/log
四、进入程序目录下,3个机器操做都同样服务器
/enzi/zk/apache-zookeeper-3.5.5-bin/conf
复制zoo_sample.cfg为zoo.cfg,并修改zoo.cfg内容以下:ide
# 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=/enzi/zk/data dataLogDir=/enzi/zk/log # the port at which the clients will connect clientPort=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=1 server.1=ip1:2888:3888 server.2=ip2:2888:3888 server.3=ip3:2888:3888 quorumListenOnAllIPs=true
五、建立myid文件
分别在对应的data目录下,为本身的服务指定myid,内容就是zoo.cfg里面server.后面的序号,分别是1,2,3,ip1机器下myid内容为1,依次类推。学习
六、依次启动3个机器zookeeper服务ui
bin/zkServer.sh start
若是想看有没有什么错误信息可使用以下命令,在调试阶段建议使用这个命令。this
bin/zkServer.sh start-foreground
七、查看是否启动成功
mode:follower,表明这个服务是follower节点。
这里server.1是leader节点。阿里云
八、使用客户端进行链接,这里采用的windows的客户端
zkCli.cmd -server ip1:2181,ip2:2181,ip3:2181
使用命令,建立临时节点以下
注意:对于阿里云,由于防火墙或者ECS的安全策略来讲,会致使集群之间的接口链接失败。
一、轻量服务器来讲须要把2181,2888,3888增长到防火墙上。
二、对于ESC须要把2181,2888,3888的安全规则,入口和出口都设置为容许。