Zookeeper 学习(三)Zookeeper 分布式应用
1、分布式安装部署
1、集群规划
在虚拟 hadoop103、hadoop104、hadoop105 三个节点上部署 Zookeeper;
2、解压安装
解压安装文件到指定路径
[hui@hadoop103 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/
同步安装文件到 Hadoop104 Hadoop105 节点
[hui@hadoop103 module]$ sh xsync.sh zookeeper-3.4.10/
3、配置服务器编号
在/opt/module/zookeeper-3.4.10/这个目录下创建zkData
[hui@hadoop103 zookeeper-3.4.10]$ mkdir -p zkData
创建编辑 myid 文件
[hui@hadoop103 zkData]$ vi myid
3
分发 myid 文件到 其余节点
集群内的服务器编号不能重复,分发myid 后需要进行修改;
4、配置 zoo.cfg 文件
重命名/opt/module/zookeeper-3.4.10/conf这个目录下的zoo_sample.cfg为zoo.cfg
[hui@hadoop103 conf]$ mv zoo_sample.cfg zoo.cfg
打开zoo.cfg文件,添加配置
dataDir=/opt/module/zookeeper-3.4.10/zkData #######################cluster########################## server.2=hadoop102:2888:3888 server.3=hadoop103:2888:3888 server.4=hadoop104:2888:3888
分发配置
[hui@hadoop103 conf]$ sh xsync.sh zoo.cfg
配置文件说明
server.A=B:C:D。
A是一个数字,表示这个是第几号服务器;
集群模式下配置一个文件myid,这个文件在dataDir目录下,这个文件里面有一个数据就是A的值,Zookeeper启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server。
B是这个服务器的ip地址;
C是这个服务器与集群中的Leader服务器交换信息的端口;
D是万一集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口。
5、集群操作
分别在三个节点上启动 Zookeeper
[hui@hadoop103 zookeeper-3.4.10]$ bin/zkServer.sh start [hui@hadoop104 zookeeper-3.4.10]$ bin/zkServer.sh start [hui@hadoop105 zookeeper-3.4.10]$ bin/zkServer.sh start
查看状态
[hui@hadoop103 zookeeper-3.4.10]$ bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: follower [hui@hadoop104 zookeeper-3.4.10]$ bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: leader [hui@hadoop105 zookeeper-3.4.10]$ bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg Mode: follower
编写群起脚本
启动脚本
[hui@hadoop103 ~]$ more /home/hui/bin/zkstart.sh #!/bin/sh for i in hui@hadoop103 hui@hadoop104 hui@hadoop105 do echo ------------------- $i -------------- ssh $i 'source /etc/profile && /opt/module/zookeeper-3.4.10/bin/zkServer.sh start' done
停止脚本
[hui@hadoop103 ~]$ more /home/hui/bin/zkstop.sh #!/bin/sh for i in hui@hadoop103 hui@hadoop104 hui@hadoop105 do echo ------------------- $i -------------- ssh $i 'source /etc/profile && /opt/module/zookeeper-3.4.10/bin/zkServer.sh stop'
2、客户端命令操作
命令基本语法
|
功能描述
|
help
|
显示所有操作命令
|
ls path [watch]
|
使用 ls 命令来查看当前znode中所包含的内容
|
ls2 path [watch]
|
查看当前节点数据并能看到更新次数等数据
|
create
|
普通创建
-s 含有序列
-e 临时(重启或者超时消失)
|
get path [watch]
|
获得节点的值
|
set
|
设置节点的具体值
|
stat
|
查看节点状态
|
delete
|
删除节点
|
rmr
|
递归删除节点
|
1、启动客户端、
[hui@hadoop105 zookeeper-3.4.10]$ bin/zkCli.sh
2、显示常用命令
[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) 1] ls / [zookeeper, xajh]
4、查看详细内容
[zk: localhost:2181(CONNECTED) 2] ls2 / [zookeeper, xajh] cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x200000002 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 2
5、创建普通节点
[zk: localhost:2181(CONNECTED) 5] create /xajh hausan [zk: localhost:2181(CONNECTED) 6] ls /xajh [huashan]
6、创建短暂节点
[zk: localhost:2181(CONNECTED) 18] create -e /xajh/songshan zuolc Created /xajh/songshan [zk: localhost:2181(CONNECTED) 2] ls /xajh [huashan]
短暂节点创建后,推出客户端再次启动客户端 短暂节点丢失;
7、创建带有序号的节点
[zk: localhost:2181(CONNECTED) 3] create -s /xajh/songshan zuolc Created /xajh/songshan0000000003 [zk: localhost:2181(CONNECTED) 4] create -s /xajh/songshan zuolc Created /xajh/songshan0000000004
8、修改节点值
[zk: localhost:2181(CONNECTED) 5] get /xajh/huashan linghc cZxid = 0x200000003 ctime = Fri Oct 09 18:27:36 CST 2020 mZxid = 0x200000003 mtime = Fri Oct 09 18:27:36 CST 2020 pZxid = 0x200000003 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 6 numChildren = 0 [zk: localhost:2181(CONNECTED) 6] set /xajh/huashan linpz cZxid = 0x200000003 ctime = Fri Oct 09 18:27:36 CST 2020 mZxid = 0x30000000c mtime = Fri Oct 09 19:40:38 CST 2020 pZxid = 0x200000003 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 5 numChildren = 0 [zk: localhost:2181(CONNECTED) 7] get /xajh/huashan linpz cZxid = 0x200000003 ctime = Fri Oct 09 18:27:36 CST 2020 mZxid = 0x30000000c mtime = Fri Oct 09 19:40:38 CST 2020 pZxid = 0x200000003 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 5 numChildren = 0
9、节点值变化监听
在 hadoop104 上注册监听 /xajh 监听
[zk: localhost:2181(CONNECTED) 3] get /xajh/huashan watch
hadoop103 修改值
[zk: localhost:2181(CONNECTED) 8] set /xajh/huashan longhc cZxid = 0x200000003 ctime = Fri Oct 09 18:27:36 CST 2020 mZxid = 0x30000000e mtime = Fri Oct 09 19:45:48 CST 2020 pZxid = 0x200000003 cversion = 0 dataVersion = 2 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 6 numChildren = 0 [zk: localhost:2181(CONNECTED) 9]
hadoop104 客户端提示
WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/xajh/huashan
说明:监听程序注册一次只有效一次,需要再次有效,需要再次注册;
10、删除节点
[zk: localhost:2181(CONNECTED) 8] delete /xajh/songshan0000000004 [zk: localhost:2181(CONNECTED) 10] ls /xajh [songshan0000000003, huashan]
11、递归删除节点
[zk: localhost:2181(CONNECTED) 11] rmr /xajh [zk: localhost:2181(CONNECTED) 12] ls / [zookeeper]