CentOS 7.9 安装 zookeeper-3.7.0

1. 下载 Zookeeper

Zookeeper 官网

使用 wget 下载:

wget //mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz

2. 解压资源

# 解压资源到指定目录下
tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz 
 
# 资源目录重命名
mv apache-zookeeper-3.7.0-bin/ zookeeper

# 移动到你想要安装的目录
mv zookeeper /usr/local

3. 启动 Zookeeper

Zookeeper 依赖 Java 环境,所以启动之前请配置好 JDK 或者 JRE

# 进入到之前解压后的zookeeper资源目录
cd /usr/local/zookeeper

# 重命名 Zookeeper 的配置文件
cp ./conf/zoo_sample.cfg ./conf/zoo.cfg

# 启动 Zookeeper
./bin/zkServer.sh start 

注意:Zookeeper在启动时会找 conf 下的 zoo.cfg 作为默认配置文件


4. 检查 Zookeeper 是否成功启动

ps -ef |grep zookeeper

# 如果可以查询到 Zookeeper 的进程,则说明成功启动了
# 需要注意的是,这样启动 Zookeeper 并不是开机自启的服务。

5. 设置 Zookeeper 服务开机自启

# 配置开机自启时,需要先停用 zookeeper 
./bin/zkServer.sh stop

cd /etc/systemd/system

# 在该目录下创建 zookeeper.service 
vi zookeeper.service 

# 在 zookeeper.service 输入以下内容,注意 zookeeper 的安装路径
[Unit]
Description=zookeeper.service
After=network.target
[Service]
Type=forking
Environment=/usr/local/zookeeper
ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
ExecReload=/usr/local/zookeeper/bin/zkServer.sh restart
[Install]
WantedBy=multi-user.target

# 保存并退出 vi 

# 重新加载服务配置
systemctl daemon-reload

# 设置开机自启
systemctl enable zookeeper.service

# 启动 zookeeper
systemctl start zookeeper.service

查看 zookeeper 状态

systemctl status zookeeper.service

关闭 zookeeper

systemctl stop zookeeper.service

关闭开机自启

systemctl disable zookeeper.service