如何启动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,效果和单机模式一样。

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

发布了428 篇原创文章 · 获赞 197 · 访问量 82万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章