一.安装部署
1.集群规划
在ecs-az3-yc-0020、ecs-az3-yc-0021和ecs-az3-yc-0022三个节点上部署Zookeeper。Zookeeper版本为3.4.10
2.解压安装
(1)解压Zookeeper安装包到/opt/module/目录下
[root@ecs-az3-yc-0020 software]# tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/
3.配置服务器编号
(1)在/opt/module/zookeeper-3.4.10/这个目录下创建zkData
[root@ecs-az3-yc-0020 zookeeper-3.4.10]# mkdir -p zkData
(2)在/opt/module/zookeeper-3.4.10/zkData目录下创建一个myid的文件
[root@ecs-az3-yc-0020 zkData]# touch myid
添加myid文件,注意一定要在linux里面创建,在notepad++里面很可能乱码
(3)编辑myid文件
[root@ecs-az3-yc-0020 zkData]# vi myid
在文件中添加与server对应的编号:1
4.配置zoo.cfg文件
(1)重命名/opt/module/zookeeper-3.4.10/conf这个目录下的zoo_sample.cfg为zoo.cfg
[root@ecs-az3-yc-0020 conf]# mv zoo_sample.cfg zoo.cfg
(2)打开zoo.cfg文件
[root@ecs-az3-yc-0020 conf]# vim zoo.cfg
#修改数据存储路径配置
dataDir=/opt/module/zookeeper-3.4.10/zkData
##################增加如下配置
#######################cluster##########################
server.1=ecs-az3-yc-0020:2888:3888
server.2=ecs-az3-yc-0021:2888:3888
server.3=ecs-az3-yc-0022:2888:3888
(3)同步配置好的/opt/module/zookeeper-3.4.10目录内容到ecs-az3-yc-0021、ecs-az3-yc-0022
[root@ecs-az3-yc-0020 module]# scp zookeeper-3.4.10 –r root@ecs-az3-yc-0021:/opt/module
[root@ecs-az3-yc-0020 module]# scp zookeeper-3.4.10 –r root@ecs-az3-yc-0022:/opt/moudle
并分别在ecs-az3-yc-0021、ecs-az3-yc-0022上修改myid文件中内容为2、3
(4)配置参数解读
server.A=B:C:D
A是一个数字,表示这个是第几号服务器;
集群模式下配置一个文件myid,这个文件在dataDir目录下,这个文件里面有一个数据就是A的值,Zookeeper启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server。
B是这个服务器的ip地址;
C是这个服务器与集群中的Leader服务器交换信息的端口;
D是万一集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口。
(5)修改host(如果配置中用的主机名例如ecs-az3-yc-0021必须修改)使用ip则不必修改
[云内网ip].188 ecs-az3-yc-0021
[云内网ip].197 ecs-az3-yc-0022
[云内网ip].213 ecs-az3-yc-0020
4.集群操作
(1)分别启动Zookeeper
[root@ecs-az3-yc-0020 zookeeper-3.4.10]# bin/zkServer.sh start
[root@ecs-az3-yc-0021 zookeeper-3.4.10]# bin/zkServer.sh start
[root@ecs-az3-yc-0022 zookeeper-3.4.10]# bin/zkServer.sh start
(2)查看状态
[root@ecs-az3-yc-0020 zookeeper-3.4.10]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
[root@ecs-az3-yc-0021 zookeeper-3.4.10]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
[root@ecs-az3-yc-0022 zookeeper-3.4.10]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
二.客户端命令行操作
命令基本语法 | 功能描述 |
---|---|
help | 显示所有操作命令 |
ls path [watch] | 使用 ls 命令来查看当前znode中所包含的内容 |
ls2 path [watch] | 查看当前节点数据并能看到更新次数等数据 |
create | 普通创建 -s 含有序列 -e 临时(重启或者超时消失) |
get path [watch] | 获得节点的值 |
set | 设置节点的具体值 |
stat | 查看节点状态 |
delete | 删除节点 |
rmr | 递归删除节点 |
1.启动客户端
[root@ecs-az3-yc-0020 zookeeper-3.4.10]# bin/zkCli.sh
2.显示所有操作命令
[zk: localhost:2181(CONNECTED) 1] help
[zk: localhost:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
3.查看当前znode中所包含的内容
[zk: localhost:2181(CONNECTED) 0] ls /
4.查看当前节点详细数据
[zk: localhost:2181(CONNECTED) 1] ls2 /
[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 /greetest "hbq"
Created /greetest
[zk: localhost:2181(CONNECTED) 4] create /greetest/test "hbqtest"
Created /greetest/test
6.获得节点的值
[zk: localhost:2181(CONNECTED) 5] get /greetest
hbq
cZxid = 0x900000133
ctime = Thu Apr 23 15:24:54 CST 2020
mZxid = 0x900000133
mtime = Thu Apr 23 15:24:54 CST 2020
pZxid = 0x900000134
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 1
[zk: localhost:2181(CONNECTED) 6] get /greetest/test
hbqtest
cZxid = 0x900000134
ctime = Thu Apr 23 15:26:42 CST 2020
mZxid = 0x900000134
mtime = Thu Apr 23 15:26:42 CST 2020
pZxid = 0x900000134
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
7.创建短暂节点
[zk: localhost:2181(CONNECTED) 8] create -e /greetest/temp "tempnode"
Created /greetest/temp
(1)在当前客户端是能查看到的
[zk: localhost:2181(CONNECTED) 9] ls /greetest
[temp, test]
(2)退出当前客户端然后再重启客户端
[zk: localhost:2181(CONNECTED) 12] quit
[hadoop@ecs-az3-yc-0022 zookeeper-3.4.10]# bin/zkCli.sh
(3)再次查看根目录下短暂节点已经删除
[zk: localhost:2181(CONNECTED) 0] ls /greetest
[test]
8.创建带序号的节点
(1)先创建一个普通的根节点/sanguo/weiguo
[zk: localhost:2181(CONNECTED) 12] create /greetest/seq ""
Created /greetest/seq
(2)创建带序号的节点
[zk: localhost:2181(CONNECTED) 13] create -s /greetest/seq/seqtest "seqnode_1"
Created /greetest/seq/seqtest0000000000
[zk: localhost:2181(CONNECTED) 1] create -s /greetest/seq/seqtest_2 "seqnode_2"
Created /greetest/seq/seqtest_20000000001
[zk: localhost:2181(CONNECTED) 2] create -s /greetest/seq/seqtest_3 "seqnode_3"
Created /greetest/seq/seqtest_30000000002
如果原来没有序号节点,序号从0开始依次递增。如果原节点下已有2个节点,则再排序时从2开始,以此类推。
9.修改节点数据值
[zk: localhost:2181(CONNECTED) 3] set /greetest/test "hbqtest-new"
cZxid = 0x900000134
ctime = Thu Apr 23 15:26:42 CST 2020
mZxid = 0x900000142
mtime = Thu Apr 23 15:51:59 CST 2020
pZxid = 0x900000134
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 11
numChildren = 0
修改后
[zk: localhost:2181(CONNECTED) 4] get /greetest/test
hbqtest-new
cZxid = 0x900000134
ctime = Thu Apr 23 15:26:42 CST 2020
mZxid = 0x900000142
mtime = Thu Apr 23 15:51:59 CST 2020
pZxid = 0x900000134
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 11
numChildren = 0
10.节点的值变化监听
(1)在ecs-az3-yc-0021主机上注册监听/greetest节点数据变化
[zk: localhost:2181(CONNECTED) 0] get /greetest watch
hbq
cZxid = 0x900000133
ctime = Thu Apr 23 15:24:54 CST 2020
mZxid = 0x900000133
mtime = Thu Apr 23 15:24:54 CST 2020
pZxid = 0x90000013c
cversion = 4
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 2
(2)在ecs-az3-yc-0020主机上修改/greetest节点的数据
[zk: localhost:2181(CONNECTED) 5] set /greetest "hbq-new"
cZxid = 0x900000133
ctime = Thu Apr 23 15:24:54 CST 2020
mZxid = 0x900000144
mtime = Thu Apr 23 16:12:26 CST 2020
pZxid = 0x90000013c
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 2
(4)观察ecs-az3-yc-0021主机收到数据变化的监听
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/greetest
11.节点的子节点变化监听(路径变化)
(1)在ecs-az3-yc-0021主机上注册监听/sanguo节点的子节点变化
[zk: localhost:2181(CONNECTED) 2] ls /greetest watch
[test-2, test, seq]
[zk: localhost:2181(CONNECTED) 3]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/greetest
(2)在ecs-az3-yc-0020主机/sanguo节点上创建子节点
[zk: localhost:2181(CONNECTED) 8] create /greetest/test-3 "test-3"
Created /greetest/test-3
(3)观察ecs-az3-yc-0021主机收到子节点变化的监听
[zk: localhost:2181(CONNECTED) 3]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/greetest
12.删除节点
[zk: localhost:2181(CONNECTED) 9] delete /greetest/test-3
13.递归删除节点
[zk: localhost:2181(CONNECTED) 15] rmr /greetest/test
14.查看节点状态
[zk: localhost:2181(CONNECTED) 17] stat /greetest
cZxid = 0x90000014f
ctime = Thu Apr 23 17:17:35 CST 2020
mZxid = 0x90000014f
mtime = Thu Apr 23 17:17:35 CST 2020
pZxid = 0x90000014f
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0