伪集群:
一、zookeeper前提
1、zk安装包来源
a、官网下载
https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210222203138947.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L05laWdoYm9yX0w=,size_16,color_FFFFFF,t_70)
b、其他途径下载
本人直接用公司SVN上的zk 3.5.8版本: apache-zookeeper-3.5.8-bin.zip
2、基础环境
安装目录/app下
基于办公本的虚拟机,软硬件如图
3、解压&复制
unzip apache-zookeeper-3.5.8-bin.zip -d /app
echo "apache-zookeeper-3.5.8-bin-2 apache-zookeeper-3.5.8-bin-3"|xargs -n 1 cp -vr apache-zookeeper-3.5.8-bin/ #复制伪集群三节点
二、zookeeper配置
1、修改myid文件
[lee@localhost app]$ find /app/apache-zookeeper-3.5.8-bin* -name "myid"
/app/apache-zookeeper-3.5.8-bin/data/myid
/app/apache-zookeeper-3.5.8-bin-2/data/myid
/app/apache-zookeeper-3.5.8-bin-3/data/myid
echo "1" > /app/apache-zookeeper-3.5.8-bin/data/myid
echo "2" > /app/apache-zookeeper-3.5.8-bin-2/data/myid
echo "3" > /app/apache-zookeeper-3.5.8-bin-3/data/myid
2、修改zoo.cfg
cd apache-zookeeper-3.5.8-bin/conf/
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg #修改zk配置项
find /app/apache-zookeeper-3.5.8-bin* -name "zoo.cfg" | xargs -n1 cat |grep -Ev '^$|#' #查看伪集群配置文件
注意:
1、修改dataDir和dataLogDir配置目录
2、修改server.id和地址(id的值和myid中的值一致)
3、注意clientPort 伪集群的端口占用情况(2181\2182\2183)
三、zookeeper启动
1、编辑伪集群启动文件
touch zkStart.sh
echo "/app/apache-zookeeper-3.5.8-bin/bin/zkServer.sh start" >> zkStart.sh
echo "sleep 5" >>zkStart.sh
echo "/app/apache-zookeeper-3.5.8-bin-2/bin/zkServer.sh start" >> zkStart.sh
echo "sleep 5" >>zkStart.sh
echo "/app/apache-zookeeper-3.5.8-bin-3/bin/zkServer.sh start" >> zkStart.sh
2、启动
[lee@localhost app]$ sh zkStart.sh
ZooKeeper JMX enabled by default
Using config: /app/apache-zookeeper-3.5.8-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
ZooKeeper JMX enabled by default
Using config: /app/apache-zookeeper-3.5.8-bin-2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
ZooKeeper JMX enabled by default
Using config: /app/apache-zookeeper-3.5.8-bin-3/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
3、编辑伪集群状态文件
[lee@localhost app]$ sed -i 's/start/status/g' zkStatus.sh
/app/apache-zookeeper-3.5.8-bin/bin/zkServer.sh status
sleep 5
/app/apache-zookeeper-3.5.8-bin-2/bin/zkServer.sh status
sleep 5
/app/apache-zookeeper-3.5.8-bin-3/bin/zkServer.sh status
[lee@localhost app]$ sh zkStatus.sh
ZooKeeper JMX enabled by default
Using config: /app/apache-zookeeper-3.5.8-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
ZooKeeper JMX enabled by default
Using config: /app/apache-zookeeper-3.5.8-bin-2/bin/../conf/zoo.cfg
Client port found: 2182. Client address: localhost.
Mode: follower
ZooKeeper JMX enabled by default
Using config: /app/apache-zookeeper-3.5.8-bin-3/bin/../conf/zoo.cfg
Client port found: 2183. Client address: localhost.
Mode: follower
四、zookeeper常用命令行
1.ls -- 查看zk节点:
[zk: 127.0.0.1:2181(CONNECTED) 1] ls /
2.ls -s 查看zk节点,可查看到time、version等信息:
[zk: 127.0.0.1:2181(CONNECTED) 1] ls2 /
3.create -- 创建znode,并设置初始内容:
[zk: 127.0.0.1:2181(CONNECTED) 1] create /test "test"
Created /test
4.get -- 获取znode的数据:
[zk: 127.0.0.1:2181(CONNECTED) 1] get /test
5.set -- 修改znode内容:
[zk: 127.0.0.1:2181(CONNECTED) 1] set /test "ricky"
6.delete -- 删除znode:
[zk: 127.0.0.1:2181(CONNECTED) 1] delete /test
7.quit -- 退出客户端```