在ecs-az3-yc-0020、ecs-az3-yc-0021和ecs-az3-yc-0022三个节点上部署Zookeeper。Zookeeper版本为3.4.10
(1)解压Zookeeper安装包到/opt/module/目录下
[root@ecs-az3-yc-0020 software]# tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/
(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
(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
(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 | 递归删除节点 |
[root@ecs-az3-yc-0020 zookeeper-3.4.10]# bin/zkCli.sh
[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
[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
[zk: localhost:2181(CONNECTED) 3] create /greetest "hbq"
Created /greetest
[zk: localhost:2181(CONNECTED) 4] create /greetest/test "hbqtest"
Created /greetest/test
[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
[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]
(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开始,以此类推。
[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
(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
(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
[zk: localhost:2181(CONNECTED) 9] delete /greetest/test-3
[zk: localhost:2181(CONNECTED) 15] rmr /greetest/test
[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