程序地带

Zookeeper配置及运行


Zookeeper配置及运行
本地模式安装部署
1)安装前准备
1.安装jdk
2.拷贝Zookeeper安装包到Linux系统下
3.解压

tar -zxvf zookeeper-3.5.7.tar.gz -c /opt/module/


4.配置环境变量(可选)

[lxt@hadoop102 ~]$ cd etc/profile.d/


#创建ZOOKEEPER_HOME
export ZOOKEEPER_HOME=/opt/module/zookeeper-3.5.7
export PATH=$PATH:$ZOOKEEPER_HOME/bin

如果不是第一次安装zk,那么就把zk根目录下创建的zkdata目录下内容清空,并且删除logs目录。


2)配置修改
1.将/opt/module/zookeeper-3.5.7/conf这个路径下的zoo_sample.cfg修改为zoo.cfg;
[lxt@hadoop102 conf]$ mv zoo_sample.cfg zoo.cfg
2.打开zoo.cfg文件 修改dataDir路径
[lxt@hadoop102 zookeeper-3.5.7]$ vim zoo.cfg

修改如下


dataDir=/opt/module/zookeeper-3.5.7/zkData
3.在/opt/module/zookeeper-3.5.7/这个目录上创建zkData文件夹
[lxt@hadoop102 zookeeper-3.5.7]$ mkdir zkData
3)操作Zookeeper
1.启动Zookeeper
[lxt@hadoop102 zookeeper-3.5.7]$ zkServer.sh start
2.查看进程是否成功
[lxt@hadoop102 zookeeper-3.5.7]$ jps
4020 Jps
4001 QuorumPeerMain
3.查看状态
[lxt@hadoop102 zookeeper-3.5.7]$ bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Mode: standalone
4.启动客户端
[lxt@hadoop102 zookeeper-3.5.7]$ zkCli.sh
5.退出客户端
[zk: localhost:2181(CONNECTED) 0] quit
6.停止Zookeeper
[lxt@hadoop102 zookeeper-3.5.7]$ zkServer.sh stop
4)分布式安装部署
1.集群规划

在hadoop102 hadoop103 hadoop104三个节点上部署Zookeeper


2.解压安装

解压Zookeeper安装包到/opt/module/目录下


[lxt@hadoop102 software]$ tar -zxvf zookeeper-3.5.7.tar.gz -C /opt/module/
3.配置服务器编号

在/opt/module/zookeeper-3.5.7/这个目录下创建zkData


[atguigu@hadoop102 zookeeper-3.5.7]$ mkdir -p zkData

在/opt/module/zookeeper-3.5.7/zkData目录下创建一个myid的文件


[lxt@hadoop102 zkData]$ touch myid

编辑myid文件


[lxt@hadoop102 zkData]$ vi myid
2 (输入对应机器编号)

并分别在hadoop103、hadoop104上修改myid文件中内容为3、4


4.配置zoo.cfg文件

重命名/opt/module/zookeeper-3.5.7/conf这个目录下的zoo_sample.cfg为zoo.cfg


[lxt@hadoop102 conf]$ mv zoo_sample.cfg zoo.cfg

打开zoo.cfg文件


[lxt@hadoop102 conf]$ vim zoo.cfg

修改数据存储路径配置


dataDir=/opt/module/zookeeper-3.5.7/zkData

在其中增加如下配置


#######################cluster##########################
server.2=hadoop102:2888:3888
server.3=hadoop103:2888:3888
server.4=hadoop104:2888:3888

同步zoo.cfg配置文件


[lxt@hadoop102 conf]$ xsync zoo.cfg
[lxt@hadoop102 module]$ xsync zookeeper-3.5.7/

配置参数解读


server.A=B:C:D。
5.集群操作

分别启动Zookeeper


[lxt@hadoop102 zookeeper-3.5.7]$ bin/zkServer.sh start
[lxt@hadoop103 zookeeper-3.5.7]$ bin/zkServer.sh start
[lxt@hadoop104 zookeeper-3.5.7]$ bin/zkServer.sh start

查看状态


[lxt@hadoop102 zookeeper-3.5.7]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Mode: follower
[lxt@hadoop103 zookeeper-3.5.7]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Mode: leader
[lxt@hadoop104 zookeeper-3.5.7]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg
Mode: follower
5)客户端命令操作
1.启动客户端
[lxt@hadoop103 zookeeper-3.5.7]$ bin/zkCli.sh
2.显示所有操作命令
[zk: localhost:2181(CONNECTED) 1] help
3.查看znode中包含的内容
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
4.查看当前节点详细数据
[zk: localhost:2181(CONNECTED) 1] ls -s /
[zookeeper]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
5.分别创建2个普通节点
[zk: localhost:2181(CONNECTED) 3] create /sanguo "diaochan"
Created /sanguo
[zk: localhost:2181(CONNECTED) 4] create /sanguo/shuguo "liubei"
Created /sanguo/shuguo
6.获得节点的值
[zk: localhost:2181(CONNECTED) 5] get /sanguo
diaochan
[zk: localhost:2181(CONNECTED) 6] get -s /sanguo
diaochan
7.创建临时节点
[zk: localhost:2181(CONNECTED) 7] create -e /sanguo/wuguo "zhouyu"
Created /sanguo/wuguo

1)在当前客户端是能查到的


[zk: localhost:2181(CONNECTED) 3] ls /sanguo
[wuguo, shuguo]

2)退出当前客户端再重启客户端


[zk: localhost:2181(CONNECTED) 12] quit
[lxt@hadoop104 zookeeper-3.5.7]$ bin/zkCli.sh

3)再次查看根目录下短暂节点已经被删除


[zk: localhost:2181(CONNECTED) 0] ls /sanguo
[shuguo]
8.创建带序号的节点

(1)先创建一个普通的根节点/sanguo/weiguo


[zk: localhost:2181(CONNECTED) 1] create /sanguo/weiguo "caocao"
Created /sanguo/weiguo

(2)创建带序号的节点


[zk: localhost:2181(CONNECTED) 2] create /sanguo/weiguo "caocao"
Node already exists: /sanguo/weiguo
[zk: localhost:2181(CONNECTED) 3] create -s /sanguo/weiguo "caocao"
Created /sanguo/weiguo0000000000
[zk: localhost:2181(CONNECTED) 4] create -s /sanguo/weiguo "caocao"
Created /sanguo/weiguo0000000001
[zk: localhost:2181(CONNECTED) 5] create -s /sanguo/weiguo "caocao"
Created /sanguo/weiguo0000000002
[zk: localhost:2181(CONNECTED) 6] ls /sanguo
[shuguo, weiguo, weiguo0000000000, weiguo0000000001, weiguo0000000002, wuguo]
[zk: localhost:2181(CONNECTED) 6]

如果节点下原来没有子节点,序号从0开始依次递增。如果原节点下已有2个节点,则再排序时从2开始,以此类推。


9.修改节点数据值
[zk: localhost:2181(CONNECTED) 6] set /sanguo/weiguo "caopi"
10.节点的值变化监听

(1)在hadoop104主机上注册监听/sanguo节点数据变化


[zk: localhost:2181(CONNECTED) 26] [zk: localhost:2181(CONNECTED) 8] get -w /sanguo

(2)在hadoop103主机上修改/sanguo节点的数据


[zk: localhost:2181(CONNECTED) 1] set /sanguo "xishi"

(3)观察hadoop104主机收到数据变化的监听


WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
11.节点的子节点变化监听(路径变化)

(1)在hadoop104主机上注册监听/sanguo节点的子节点变化


[zk: localhost:2181(CONNECTED) 1] ls -w /sanguo
[aa0000000001, server101]

(2)在hadoop103主机/sanguo节点上创建子节点


[zk: localhost:2181(CONNECTED) 2] create /sanguo/jin "simayi"
Created /sanguo/jin

​ (3)观察hadoop104主机收到子节点变化的监听


WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
12.删除节点
[zk: localhost:2181(CONNECTED) 4] delete /sanguo/jin
13.递归删除节点
[zk: localhost:2181(CONNECTED) 15] deleteall /sanguo/shuguo
14.查看节点状态
[zk: localhost:2181(CONNECTED) 17] stat /sanguo
cZxid = 0x100000003
ctime = Wed Aug 29 00:03:23 CST 2018
mZxid = 0x100000011
mtime = Wed Aug 29 00:21:23 CST 2018
pZxid = 0x100000014
cversion = 9
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_43597431/article/details/112689505

随机推荐

计算物理作业python代码_计算物理第二次作业

计算物理第二次作业作业一、摘要本次作业将练习使用Python语言,并编写一个简单程序,二、背景介绍刚刚结束第一次Python语法课,许多内容需要消化和吸收&#...

weixin_39747511 阅读(620)

modal vue 关闭_开发Vue组件系列之模态框

modal vue 关闭_开发Vue组件系列之模态框

开发Vue组件系列之模态框,主要有标题、内容、定时器、按钮文案、按钮事件回调、遮罩层这些可配置项。本次开发得组件是本系列的第一个组件,后期也会有更多系列教程推出。使用命令行安装或下载npminstal...

weixin_39587029 阅读(456)