如何启动ZooKeeper(中)

(2). Module2 伪分布式模式

上面演示了如何启动单机模式,现在我们来演示设置伪分布式模式。

我们可以在一台机器上创建模拟的ZooKeeper集群服务,假如我们需要3个节点,需要创建3个cfg文件,分别命名为zoo1.cfg,zoo2.cfg,zoo3.cfg,此外我们还需要创建3个不同的数据文件夹,分别是zoo1,zoo2和zoo3,目录位于/var/lib/zookeeper,如1-16、1-17和1-18三个配置清单所示。


清单1-16 配置文件zoo1内容

[root@localhost conf]# cat zoo1.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/lib/zookeeper/zoo1

# 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.1=localhost:2666:3666

server.2=localhost:2667:3667

server.3=localhost:2668:3668


清单1-17 配置文件zoo2内容

[root@localhost conf]# cat zoo2.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=dataDir=/var/lib/zookeeper/zoo2

# 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

server.1=localhost:2666:3666

server.2=localhost:2667:3667

server.3=localhost:2668:3668


清单1-18 配置文件zoo3内容

[root@localhost conf]# cat zoo3.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/lib/zookeeper/zoo3

# 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

server.1=localhost:2666:3666

server.2=localhost:2667:3667

server.3=localhost:2668:3668


注意,每一个虚拟机器都对应一个自己的zoo{$}.cfg,其中的{$}需要通过清单1-19所示命令来进行设置。

Tips

由于采用的是“>”,所以命令会创建文件,如果采用“>>”,则Linux会在原有文件基础上新增内容。


清单1-19 设置myid

[root@localhost conf]# echo 1 > /var/lib/zookeeper/zoo1/myid

[root@localhost conf]# echo 2 > /var/lib/zookeeper/zoo2/myid

[root@localhost conf]# echo 3 > /var/lib/zookeeper/zoo3/myid

接下来我们开始启动ZooKeeper的3个实例(虚拟的3台机器),需要调用三次zkServer.sh的Start命令,采用不同的配置文件,如清单1-20所示命令及输出。


清单1-20 启动伪分布式集群服务

[root@localhost bin]# ./zkServer.sh start/home/zhoumingyao/zookeeper/zookeeper-3.4.7/conf/zoo1.cfg

ZooKeeper JMX enabled by default

Using config: /home/zhoumingyao/zookeeper/zookeeper-3.4.7/conf/zoo1.cfg

Starting zookeeper ... STARTED

[root@localhost bin]# ./zkServer.sh start/home/zhoumingyao/zookeeper/zookeeper-3.4.7/conf/zoo2.cfg

ZooKeeper JMX enabled by default

Using config: /home/zhoumingyao/zookeeper/zookeeper-3.4.7/conf/zoo2.cfg

Starting zookeeper ... STARTED

[root@localhost bin]# ./zkServer.sh start/home/zhoumingyao/zookeeper/zookeeper-3.4.7/conf/zoo3.cfg

ZooKeeper JMX enabled by default

Using config: /home/zhoumingyao/zookeeper/zookeeper-3.4.7/conf/zoo3.cfg

Starting zookeeper ... STARTED


清单1-21 查看服务

[root@localhost bin]# jps

5537 QuorumPeerMain

5617 Jps

5585 QuorumPeerMain

确认服务都正常启动,我们就可以通过zkCli.sh脚本方式连接到ZooKeeper集群,命令为./zkCli.sh -server localhost:2181,localhost:2182,localhost:2183,效果和单机模式一样。

欢迎关注麦克叔叔每晚十点说,感兴趣的朋友可以关注公众号,一起学习与交流,

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值