ZooKeeper快速上手(复制集群)

原文链接:http://www.dubby.cn/detail.html?id=9025

如果你还没有看到ZooKeeper快速上手,那么建议你先速快浏览一遍。

复制集群的zk和单机的zk配置需要有一些新的,举个例子:

tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888

其中,initLimit是指follower连接并同步到leader的初始化连接,它是通过tickTime的倍数表示,例如我们上面的配置就是10倍的tickTime,当初始化连接时间超过设置的倍数时候则连接失败;syncLimit是指follower和leader之间发送消息时请求和应答的时间长度,如果follower在设置的时间范围内不能喝leader通信,那么该follower将会被丢弃,它也是按tickTime的倍数进行设置的;server.x是这个复制集群中所有实例,这样一旦zk启动,它会读取这个配置,就可以知道当前集群下有哪些zk实例了,这里的x就是服务的id,需要在每个zk的dataDir下增加一个myid的文件,内容就是当前zk实例在整个复制集群中的id。

在这里需要注意2888:3888,这个是每个zk实例参与leader选举时需要用到端口。

注意:如果你在多个机器上测试,那么这个端口不需要改变,但是考虑到如果只有一台机器测试,那么你需要改变端口,避免冲突

这里给出我的测试样例。

目录结构:

这里是把zk的压缩文件拷贝了三份,并且新建了三个data目录,zk1_data

zk1/conf/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=/Users/teeyoung/Desktop/middleware/zk_rep/zk1_data
# the port at which the clients will connect
clientPort=2181

server.1=zoo1:2888:3888
server.2=zoo1:3888:4888
server.3=zoo1:5888:6888

zk2/conf/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=/Users/teeyoung/Desktop/middleware/zk_rep/zk2_data
# the port at which the clients will connect
clientPort=2182

server.1=zoo1:2888:3888
server.2=zoo1:3888:4888
server.3=zoo1:5888:6888

zk1/conf/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=/Users/teeyoung/Desktop/middleware/zk_rep/zk3_data
# the port at which the clients will connect
clientPort=2183

server.1=zoo1:2888:3888
server.2=zoo1:3888:4888
server.3=zoo1:5888:6888

然后在三个data目录下建立myid文件:

zk1_data/myid:

1

zk2_data/myid:

2

zk3_data/myid:

3

然后分别启动三个实例,成功启动后,可以执行zkServer.sh status来查看状态:

➜  zk_rep ./zk1/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/teeyoung/Desktop/middleware/zk_rep/zk1/bin/../conf/zoo.cfg
Mode: follower
➜  zk_rep ./zk2/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/teeyoung/Desktop/middleware/zk_rep/zk2/bin/../conf/zoo.cfg
Mode: follower
➜  zk_rep ./zk3/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/teeyoung/Desktop/middleware/zk_rep/zk3/bin/../conf/zoo.cfg
Mode: leader

连接任一一个zk服务端,修改信息后,再连接另一个zk服务端,会发现修改信息是同步的,这就完成了最基本的zk复制集群的安装了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值