目录
1. 什么是zookeeper
2. zookeeper下载与安装
3. Zookeeper 测试
1. 什么是zookeeper
zookeeper实际上是yahoo开发的,用于分布式中一致性处理的框架。最初其作为研发Hadoop时的副产品。
由于分布式系统中一致性处理较为困难,其他的分布式系统没有必要费劲重复造轮子,故随后的分布式系统中大量应用了zookeeper,以至于zookeeper成为了各种分布式系统的基础组件,其地位之重要,可想而知。著名的hadoop、kafka、dubbo 都是基于zookeeper而构建。
2. zookeeper下载与安装
下载地址:https://archive.apache.org/dist/zookeeper/ (本案例的zookeeper是3.6.2)
2.1 上传zookeeper安装包到linux
rz apache-zookeeper-3.6.2-bin.tar.gz
2.2 解压zookeeper到/usr/local
tar zxvf apache-zookeeper-3.6.2-bin.tar.gz -C /usr/local/
2.3 复制zookeeper
mv /usr/local/apache-zookeeper-3.6.2-bin/ /usr/local/zookeeper
2.4 重命名zookeeper配置文件
cd /usr/local/zookeeper/
cd conf/
cp zoo_sample.cfg zoo.cfg
2.5 返回上级目录
cd ..
2.6 创建logs和data目录
mkdir logs data
2.7 进入conf/目录
cd conf/
2.8 修改zoo.cfg配置
vim zoo.cfg
在zoo.cfg添加以下两行代码:
#注释之前的dataDir即可复制
dataDir=/usr/local/zookeeper/data
dataLogDir=/usr/local/zookeeper/logs
2.9 zookeeper自启配置
cd /usr/lib/systemd/system
3.0 编写 zookeeper.service 文件
- 注!里面需要修改虚拟机的java环境变量地址即可 (JAVA_HOME)
[Unit]
Description=zookeeper.service
After=network.target
[Service]
Type=forking
Environment=ZOO_LOG_DIR=/usr/local/zookeeper/logs/
Environment=JAVA_HOME=/usr/local/jdk1.8.0_151
#Environment=PATH=/usr/local/jdk/bin:/usr/local/jdk/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
ExecReload=/usr/local/zookeeper/bin/zkServer.sh restart
PIDFile=/usr/local/zookeeper/data/zookeeper_server.pid
User=root
[Install]
WantedBy=multi-user.target
3.1 上传zookeeper.service文件
rz zookeeper.service
- 注:将资料中的zookeeper.service上传到/usr/lib/systemd/system目录下
3.2 设置开机自启
systemctl daemon-reload
systemctl enable zookeeper
systemctl start zookeeper
3. Zookeeper 测试
3.1 进入启动目录
cd /usr/local/zookeeper/bin/
3.2 启动
./zkCli.sh
- 新增
create /jmh ok
- 查询
get /jmh
- 修改
set /jmh no
- 删除
delete /jmh