写在前面
zookeeper在分布式应用中运用的比较广泛,了解zookeeper的原理对理解分布架构的应用具有很大的帮助,学会安装zookeeper是学习zookeeper的前提,自己刚刚学习zookeeper,记录一下安装过程,增加自己印象,方便其它人学习。
下载zookeeper
zookeeper官网下载地址 : http://mirrors.hust.edu.cn/apache/zookeeper/stable/
获取稳定版本地址: http://mirrors.hust.edu.cn/apache/zookeeper/stable/zookeeper-3.4.8.tar.gz
通过wget命令下载到服务器上。
解压zookeeper,并重命名为zookeeper
复制配置文件并配置zookeeper集群
进入zookeeper/conf目录下
配置如下 :
在配置文件中主要关注三个地方,
1. clientPort=2181 这个是连接zookeeper的端口
2. dataDir=/var/zookeeper 这是zookeeper在启动过程中存放文件的目录
3. server.1=192.168.1.105:2888:3888 这里是集群服务器的配置,zookeeper运行有三个模式
- 集群模式,上面配置的是集群模式,
- 伪集群模式就是用单机来模仿集群模式,只需要将server.id后面的ip地址配置成一样的,然后用不同的端口
- 单机模式,不需要配置server.id
在建立var/zookeeper/myid文件,输入对应的server的ID
同样的安装及配置复制到其它二台服务器上,zoo.cfg配置都一样,唯一不一样的是/var/zookeeper/myid文件中的值,例如我的配置中
启动zookeeper集群
分别进入三台服务器的 zookeeper/bin目录下启动zookeeper
检查zookerper启动状态
利用telnet命令连接到zookeeper
telnet连接成功后,利用stat命令查看zookeeper状态
192.168.1.106 服务器zookeeper状态
192.168.1.107 服务器zookeeper状态
我们可以看到107的Mode:leader说明这是leader服务器,106的mode:follower说明是follower服务器,这说明zookeeper集群安装配置成功了。