分布式Web应用----Linux环境下zookeeper集群环境的安装与配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/whzhaochao/article/details/50993428

写在前面

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命令下载到服务器上。

wget http://mirrors.hust.edu.cn/apache/zookeeper/stable/zookeeper-3.4.8.tar.gz

解压zookeeper,并重命名为zookeeper

tar -zxvf  zookeeper-3.4.8.tar.gz
mv  zookeeper-3.4.8 zookeeper

复制配置文件并配置zookeeper集群

进入zookeeper/conf目录下

cp zoo_sample.cfg zoo.cfg

配置如下 :

# 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=/var/zookeeper
# the port at which the clients will connect
clientPort=2181
# 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.id(服务器ID)=host(主机地址):port(follower与leader服务器通信端口):port(选取leader端口)
server.1=192.168.1.105:2888:3888
server.2=192.168.1.106:2888:3888
server.3=192.168.1.107:2888:3888

在配置文件中主要关注三个地方,
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文件中的值,例如我的配置中

192.168.1.105的 /var/zookeeper/myid中写1
192.168.1.106的/var/zookeeper/myid中写2
192.168.1.107的/var/zookeeper/myid中写3

启动zookeeper集群

分别进入三台服务器的 zookeeper/bin目录下启动zookeeper

./zkServer.sh start  #启动
./zkServer.sh stop   #停止
./zkServer.sh restart  #重启

检查zookerper启动状态

利用telnet命令连接到zookeeper

telnet 192.168.1.106 2181

telnet连接成功后,利用stat命令查看zookeeper状态

stat

192.168.1.106 服务器zookeeper状态

Zookeeper version: 3.4.8--1, built on 02/06/2016 03:18 GMT
Clients:
 /127.0.0.1:45681[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 3
Sent: 2
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: follower
Node count: 4
Connection closed by foreign host.

192.168.1.107 服务器zookeeper状态

Zookeeper version: 3.4.8--1, built on 02/06/2016 03:18 GMT
Clients:
 /121.41.84.103:36718[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0/0
Received: 5
Sent: 4
Connections: 1
Outstanding: 0
Zxid: 0x100000000
Mode: leader
Node count: 4
Connection closed by foreign host.

我们可以看到107的Mode:leader说明这是leader服务器,106的mode:follower说明是follower服务器,这说明zookeeper集群安装配置成功了。

猜你喜欢

转载自blog.csdn.net/whzhaochao/article/details/50993428