您的位置:首页 > 其它

zookeeper的伪集群搭建模式

2017-09-26 23:47 375 查看
所谓伪集群就是在单机模拟集群模式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


结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  zookeeper 集群