Zookeeper 伪集群搭建

1. zookeeper下载及解压

从官网下载zookeeper安装包 zookeeper-3.4.10.tar.gz 解压至以下文件夹下

/opt/application/zookeeper/

解压文件并拷贝以及重命名得到三个 zookeeper 目录

tar -zxvf zookeeper-3.4.10.tar.gz

zookeeper01 zookeeper02 zookeeper03

2. 修改zookeeper配置

将 zookeeper01,zookeeper02,zookeeper03 以下 conf 目录下的 zoo_sample.cfg 拷贝并重命名为 zoo.cfg

cp zoo_sample.cfg zoo.cfg

修改三个目录下的 zoo.cfg 文件内容如下:

# zookeeper01/conf/zoo.cfg

tickTime=2000

initLimit=10

syncLimit=5

dataDir=/opt/application/zookeeper/data/zookeeper01

clientPort=12181

server.1=127.0.0.1:12888:13888

server.2=127.0.0.1:22888:23888

server.3=127.0.0.1:32888:33888

# zookeeper02/conf/zoo.cfg

tickTime=2000

initLimit=10

syncLimit=5

dataDir=/opt/application/zookeeper/data/zookeeper02

clientPort=22181

server.1=127.0.0.1:12888:13888

server.2=127.0.0.1:22888:23888

server.3=127.0.0.1:32888:33888

# zookeeper03/conf/zoo.cfg

tickTime=2000

initLimit=10

syncLimit=5

dataDir=/opt/application/zookeeper/data/zookeeper03

clientPort=32181

server.1=127.0.0.1:12888:13888

server.2=127.0.0.1:22888:23888

server.3=127.0.0.1:32888:33888

3. 分别添加 myid 三个文件至 dataDir 目录下

cat /opt/application/zookeeper/data/zookeeper03/myid

3

4. 启动 zookeeper

分别启动三个 zookeeper,进入 zookeeper0X/bin 目录下,执行以下指令:

./zkServer.sh start

5. 客户端读写测试

任意进入其中一个 zookeeper 的 bin 目录,如 zookeeper01/bin,执行命令:

./zkCli.sh -server 127.0.0.1:12181

添加 zookeeper 节点,如下,观察到节点 /zk-book 已创建成功,且存储 HelloWorld 的字符串

[zk: 127.0.0.1:12181(CONNECTED) 2] create /zk-book HelloWorldCreated /zk-book

[zk: 127.0.0.1:12181(CONNECTED) 3] get /zk-book

HelloWorld

cZxid = 0x200000011

ctime = Tue Dec 12 13:57:34 CST 2017

mZxid = 0x200000011

mtime = Tue Dec 12 13:57:34 CST 2017

pZxid = 0x200000011

cversion = 0dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 10

numChildren = 0

退出后链接另外一个 zookeeper

./zkCli.sh -server 127.0.0.1:22181

获取之前创建的节点 /zk-book

[zk: 127.0.0.1:22181(CONNECTED) 0] get /zk-book

HelloWorld

cZxid = 0x200000011

ctime = Tue Dec 12 13:57:34 CST 2017

mZxid = 0x200000011

mtime = Tue Dec 12 13:57:34 CST 2017

pZxid = 0x200000011

cversion = 0

dataVersion = 0

aclVersion = 0

ephemeralOwner = 0x0

dataLength = 10

numChildren = 0

观察到能够读取到从另外一个节点写入的数据,确认单台机器下的集群环境搭建成功。

你可能感兴趣的:(Zookeeper 伪集群搭建)