Zookeeper的单机模式与伪分布式模式的搭建

一、Zookeeper的搭建方式

Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式。

1.单机模式:Zookeeper只运行在一台服务器上,适合测试环境;

2.伪集群模式:就是在一台物理机上运行多个Zookeeper 实例。

3.集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble)。

Zookeeper通过复制来实现高可用性,只要集合体中半数以上的机器处于可用状态,它就能够保证服务继续。为什么一定要超过半数呢?这跟Zookeeper的复制策略有关:zookeeper确保对znode 树的每一个修改都会被复制到集合体中超过半数的机器上。


单机模式搭建:

(1)下载zookeeper

 (2)解压安装包

(3)修改配置文件,即在根目录下的conf下有一个zoo_sample.cfg文件,将其复制一份到当前文件夹下并更改名称为zoo.cfg;

(4)修改其中的配置

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=/usr/local/services/zookeeper/data       在根目录创建文件夹data,用于存放数据
 
#日志文件夹
dataLogDir=/usr/local/services/zookeeper/logs    在根目录创建文件夹logs,用于存放日志
# the port at which the clients will connect
clientPort=2181
    (5)在/etc/profile下添加zookeeper的配置信息

export ZOOKEEPER_HOME=/usr/local/services/zookeeper/zookeeper-3.4.9/
    export PATH=$ZOOKEEPER_HOME/bin:$PATH
    export PATH  


此时单机模式的配置完成,在zookeeper根目录下启动

 bin/zkServer.sh start    开启

bin/zkServer.sh stop     关闭



伪分布集群模式建立文件夹  

mkdir /usr/local/services/zookeeper/server1   

mkdir /usr/local/services/zookeeper/server2   

mkdir /usr/local/services/zookeeper/server3


将zookeeper解压后分别复制到server1,server2,server3下,再在server1,server2,server3下创建data,log文件夹,在data文件夹下分别创建文件 myid ,在里面分别添加内容:1,2,3   对用与分布式服务器中配置文件  server.number=A:B:C 中的 number。接着修改server1,server2,server3下zookeeper的配置文件,此时配置文件根单机模式下不同,

server1下配置文件:

# 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=/tmp/zookeeper

#数据文件夹
dataDir=/usr/local/services/zookeeper/server1/data
 
#dataDir=./data
#日志文件夹
dataLogDir=/usr/local/services/zookeeper/server1/log
server.1=127.0.0.1:2222:2223       
server.2=127.0.0.1:3333:3334
server.3=127.0.0.1:4444:4445
# the port at which the clients will connect
clientPort=2181       //由于是在同一台机器上启动多个服务器,因此每个客户端端口必须不同 server2中设置为2182 ,server3中设置为2183
# 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

     server2下配置文件:

# 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=/tmp/zookeeper

#数据文件夹
dataDir=/usr/local/services/zookeeper/server1/data
 
#dataDir=./data
#日志文件夹
dataLogDir=/usr/local/services/zookeeper/server1/log
server.1=127.0.0.1:2222:2223

server.2=127.0.0.1:3333:3334
server.3=127.0.0.1:4444:4445
# the port at which the clients will connect
clientPort=2182
# 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



server3配置文件:

# 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=/tmp/zookeeper

#数据文件夹
dataDir=/usr/local/services/zookeeper/server1/data
 
#dataDir=./data
#日志文件夹
dataLogDir=/usr/local/services/zookeeper/server1/log
server.1=127.0.0.1:2222:2223

server.2=127.0.0.1:3333:3334
server.3=127.0.0.1:4444:4445
# the port at which the clients will connect
clientPort=2183
# 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

注意事项

    配置文件中参数含义:

其中各配置项的含义,解释如下:
1.tickTime:CS通信心跳时间
Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。tickTime以毫       秒为单位。tickTime=2000 
2.initLimit:LF初始通信时限
集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)。
initLimit=5 
3.syncLimit:LF同步通信时限
集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数(tickTime的数量)。
syncLimit=2 
4.dataDir:数据文件目录
Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也保存在这个目录里。
dataDir=/home/michael/opt/zookeeper/data  
5.clientPort:客户端连接端口
客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
clientPort=2181 
6.服务器名称与地址:集群信息(服务器编号,服务器地址,LF通信端口,选举端口)
这个配置项的书写格式比较特殊,规则如下:
server.N=YYY:A:B 


在一台机器上部署了3个server,需要注意的是在集群为分布式模式下我们使用的每个配置文档模拟一台机器,也就是说单台机器及上运行多个Zookeeper实例。但是,必须保证每个配置文档的各个端口号不能冲突,除了clientPort不同之外,dataDir也不同。另外,还要在dataDir所对应的目录中创建myid文件来指定对应的Zookeeper服务器实例。

(1)clientPort端口:如果在1台机器上部署多个server,那么每台机器都要不同的 clientPort,比如 server1是2181,server2是2182,server3是2183,

(2)dataDir和dataLogDir:dataDir和dataLogDir也需要区分下,将数据文件和日志文件分开存放,同时每个server的这两变量所对应的路径都是不同的。

(3)server.X和myid: server.X 这个数字就是对应,data/myid中的数字。在3个server的myid文件中分别写入了0,1,2,那么每个server中的zoo.cfg都配 server.0 server.2,server.3就行了。因为在同一台机器上,后面连着的2个端口,3个server都不要一样,否则端口冲突。

启动zookeeper集群

    启动在集群伪分布式下,我们只有一台机器,按时要运行三个Zookeeper实例。此时,如果在使用单机模式的启动命令是行不通的。此时,只要通过下面三条命令就能运行前面所配置的Zookeeper服务。

在每个server下面启动各自的服务器:

server1下:bin/zkServer.sh start

server2下:bin/zkServer.sh start

server3下:bin/zkServer.sh start


通过 jps 来查看是否启动成功:

[root@localhost server1]# jps -l
9121 org.apache.zookeeper.server.quorum.QuorumPeerMain
8963 org.apache.zookeeper.server.quorum.QuorumPeerMain
9064 org.apache.zookeeper.server.quorum.QuorumPeerMain
12107 sun.tools.jps.Jps



可以查看节点运行状态:


[root@localhost zookeeper-3.4.9]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/server1/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: leader

[root@localhost zookeeper-3.4.9]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/server2/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: follower

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值