zookeeper的伪集群搭建模式

本文介绍如何在单机上模拟ZooKeeper集群环境。通过配置三个实例zoo1.cfg、zoo2.cfg和zoo3.cfg,并设置不同的端口和服务标识,实现本地伪集群部署。文中详细展示了配置步骤及启动命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

所谓伪集群就是在单机模拟集群模式zookeeper的运行。

配置文件

下面是我配置的伪集群分布模式,分别通过zoo1.cfg、zoo2.cfg、zoo3.cfg来模拟三台机器的zookeeper集群:

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=/usr/local/zookeeper-3.4.9/data/data_1
dataLogDir=/usr/local/zookeeper-3.4.9/dataLog/dataLog_1

# the port at which the clients will connect
clientPort=2181

server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389

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=/usr/local/zookeeper-3.4.9/data/data_2
dataLogDir=/usr/local/zookeeper-3.4.9/dataLog/dataLog_2

# the port at which the clients will connect
clientPort=2182

server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389

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=/usr/local/zookeeper-3.4.9/data/data_3
dataLogDir=/usr/local/zookeeper-3.4.9/dataLog/dataLog_3

# the port at which the clients will connect
clientPort=2183

server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389

配置完成上述三个文件后,还需在dataDir对应路径下添加myid文件,内容对应server.n的n值:

 echo 1 > data_1/myid
 echo 2 > data_2/myid
 echo 0 > data_3/myid

启动

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

./zkServer.sh start zoo1.cfg
./zkServer.sh start zoo2.cfg
./zkServer.sh start zoo3.cfg

在运行完第一条指令之后,会出现一些错误异常,产生异常信息的原因是由于Zookeeper 服务的每个实例都拥有全局配置信息,他们在启动的时候会随时随地的进行Leader选举操作。此时,第一个启动的Zookeeper需要和另外两个 Zookeeper实例进行通信。但是,另外两个Zookeeper实例还没有启动起来,因此就产生了这的异样信息。我们直接将其忽略即可,待把图中“2 号”和“3号”Zookeeper实例启动起来之后,相应的异常信息自然会消失。此时,可以通过下面三条命令,来查询:

./zkServer.sh status zoo1.cfg
./zkServer.sh status zoo2.cfg
./zkServer.sh status zoo3.cfg

结果如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值