单机版本搭建

创建文件夹

mkdir /usr/local/zookeeper

解压安装包

tar zxvf apache-zookeeper-3.9.2-bin.tar.gz

移动安装包

mv apache-zookeeper-3.9.2-bin apache-zookeeper-3.9.2

配置 zoo_sample.cfg

cp zoo_sample.cfg zoo.cfg

启动服务

cd /usr/local/zookeeper/apache-zookeeper-3.9.2-bin

./bin/zkServer.sh start

检查状态

bin/zkServer.sh status

ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.9.2-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone

搭建集群版本(在一台服务器上搭建 3 个节点)

创建文件夹

mkdir /usr/local/zookeeper

解压安装包

tar zxvf apache-zookeeper-3.9.2-bin.tar.gz

拷贝多份文件

cp apache-zookeeper apache-zookeeper-1

cp apache-zookeeper apache-zookeeper-2

cp apache-zookeeper apache-zookeeper-3

编辑配置文件

进入 apache-zookeeper-1/conf

cp zoo_sample.cfg zoo.cfg

tickTime=2000
initLimit=10
syncLimit=5
# 注意这个地址每个应用的 Data 地址都不一样
dataDir=/usr/local/zookeeper1/data
clientPort=2181
# 连接服务地址
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

进入 apache-zookeeper-2/conf

cp zoo_sample.cfg zoo.cfg

tickTime=2000
initLimit=10
syncLimit=5
# 注意这个地址每个应用的 Data 地址都不一样
dataDir=/usr/local/zookeeper2/data
clientPort=2182
# 连接服务地址
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389

进入 apache-zookeeper-3/conf

cp zoo_sample.cfg zoo.cfg

tickTime=2000
initLimit=10
syncLimit=5
# 注意这个地址每个应用的 Data 地址都不一样
dataDir=/usr/local/zookeeper3/data
clientPort=2183
# 连接服务地址
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
 

在每个 dataDir 文件下创建 myid

/usr/local/zookeeper1/data

1

/usr/local/zookeeper2/data

2

/usr/local/zookeeper3/data

3

启动服务

/usr/local/zookeeper/apache-zookeeper-1/bin/zkServer.sh start

/usr/local/zookeeper/apache-zookeeper-2/bin/zkServer.sh start

/usr/local/zookeeper/apache-zookeeper-3/bin/zkServer.sh start

检查服务

/usr/local/zookeeper/apache-zookeeper-1/bin/zkServer.sh status

/usr/local/zookeeper/apache-zookeeper-2/bin/zkServer.sh status

/usr/local/zookeeper/apache-zookeeper-3/bin/zkServer.sh status