1)集群规划
在hadoop102、hadoop103 和 hadoop104 三个节点上都部署Zookeeper。
2)解压安装
(1)在hadoop102解压Zookeeper安装包到/opt/module/目录下
[yudan@hadoop102 software]$ tar -zxvf apache-zookeeper-3.5.7bin.tar.gz -C /opt/module/
(2)修改apache-zookeeper-3.5.7-bin 名称为 zookeeper-3.5.7
[yudan@hadoop102 module]$ mv apache-zookeeper-3.5.7-bin/ zookeeper-3.5.7
3)配置服务器编号
(1)在/opt/module/zookeeper-3.5.7/这个目录下创建 zkData
[yudan@hadoop102 zookeeper-3.5.7]$ mkdir zkData
(2)在/opt/module/zookeeper-3.5.7/zkData 目录下创建一个 myid 的文件
[yudan@hadoop102 zkData]$ vim myid
在文件中添加与server对应的编号(注意:上下不要有空行,左右不要有空格)
2
注意:添加myid文件,一定要在Linux里面创建,在notepad++里面很可能乱码
(3)拷贝配置好的zookeeper到其他机器上
[yudan@hadoop102 module ]$ xsync zookeeper-3.5.7
将hadoop103、hadoop104的myid分别修改为3、4
4)配置zoo.cfg文件
(1)重命名/opt/module/zookeeper-3.5.7/conf 这个目录下的 zoo_sample.cfg 为 zoo.cfg
[yudan@hadoop102 conf]$ mv zoo_sample.cfg zoo.cfg
(2)打开zoo.cfg文件
[yudan@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
(3)配置参数解读
server.A=B:C:D。
集群模式下配置一个文件myid,这个文件在dataDir目录下,这个文件里面有一个数据就是A的值,Zookeeper 启动时读取此文件,拿到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server。
Leader,而这个端口就是用来执行选举时服务器相互通信的端口。
(4)同步zoo.cfg配置文件
[yudan@hadoop102 conf]$ xsync zoo.cfg
5)集群操作
(1)分别启动Zookeeper
[yudan@hadoop102 zookeeper-3.5.7]$ bin/zkServer.sh start [yudan@hadoop103 zookeeper-3.5.7]$ bin/zkServer.sh start [yudan@hadoop104 zookeeper-3.5.7]$ bin/zkServer.sh start
(2)查看状态
[yudan@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 [yudan@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 [yudan@hadoop104 zookeeper-3.4.5]# bin/zkServer.sh status JMX enabled by default Using config: /opt/module/zookeeper-3.5.7/bin/../conf/zoo.cfg Mode: follower
1)在hadoop102的/home/yudan/bin目录下创建脚本
[yudan@hadoop102 bin]$ vim zk.sh
在脚本中编写如下内容
#!/bin/bash case $1 in "start"){ for i in hadoop102 hadoop103 hadoop104 do echo ---------- zookeeper $i 启动 ------------ ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh start" done };; "stop"){ for i in hadoop102 hadoop103 hadoop104 do echo ---------- zookeeper $i 停止 ------------ ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh stop" done };; "status"){ for i in hadoop102 hadoop103 hadoop104 do echo ---------- zookeeper $i 状态 ------------ ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh status" done };; esac
2)增加脚本执行权限
[yudan@hadoop102 bin]$ chmod 777 zk.sh
3)Zookeeper集群启动脚本
[yudan@hadoop102 module]$ zk.sh start
4)Zookeeper集群停止脚本
[yudan@hadoop102 module]$ zk.sh stop
命令基本语法 | 功能描述 |
---|---|
help | 显示所有操作命令 |
ls path | 使用 ls 命令来查看当前znode的子节点 [可监听] -w 监听子节点变化 -s 附加次级信息 |
create | 普通创建 -s 含有序列 -e 临时(重启或者超时消失) |
get path | 获得节点的值 [可监听] -w 监听节点内容变化 -s 附加次级信息 |
set | 设置节点的具体值 |
stat | 查看节点状态 |
delete | 删除节点 |
deleteall | 递归删除节点 |
1)启动客户端
[yudan@hadoop102 zookeeper-3.5.7]$ bin/zkCli.sh -server hadoop102:2181
2)显示所有操作命令
[zk: hadoop102:2181(CONNECTED) 1] help
1)查看当前znode中所包含的内容
[zk: hadoop102:2181(CONNECTED) 0] ls / [zookeeper]
2)查看当前节点详细数据
[zk: hadoop102:2181(CONNECTED) 5] 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
(1)czxid:创建节点的事务zxid
每次修改ZooKeeper状态都会产生一个ZooKeeper事务ID。事务ID是ZooKeeper中所有修改总的次序。每次修改都有唯一的zxid,如果zxid1小于zxid2,那么zxid1在zxid2之前发生。
(2)ctime:znode 被创建的毫秒数(从1970年开始)
(3)mzxid:znode 最后更新的事务zxid
(4)mtime:znode 最后修改的毫秒数(从1970年开始)
(5)pZxid:znode 最后更新的子节点zxid
(6)cversion:znode 子节点变化号,znode子节点修改次数
(7)dataversion:znode 数据变化号
(8)aclVersion:znode 访问控制列表的变化号
(9)ephemeralOwner:如果是临时节点,这个是znode拥有者的session id。如果不是
临时节点则是0。
(10)dataLength:znode 的数据长度
(11)numChildren:znode 子节点数量
1)分别创建2个普通节点(永久节点 + 不带序号)
[zk: localhost:2181(CONNECTED) 3] create /sanguo "diaochan" Created /sanguo [zk: localhost:2181(CONNECTED) 4] create /sanguo/shuguo "liubei" Created /sanguo/shuguo 注意:创建节点时,要赋值
2)获得节点的值
[zk: localhost:2181(CONNECTED) 5] get -s /sanguo diaochan cZxid = 0x100000003 ctime = Wed Aug 29 00:03:23 CST 2018 mZxid = 0x100000003 mtime = Wed Aug 29 00:03:23 CST 2018 pZxid = 0x100000004 cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 1 [zk: localhost:2181(CONNECTED) 6] get -s /sanguo/shuguo liubei cZxid = 0x100000004 ctime = Wed Aug 29 00:04:35 CST 2018 mZxid = 0x100000004 mtime = Wed Aug 29 00:04:35 CST 2018 pZxid = 0x100000004 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 6 numChildren = 0
3)创建带序号的节点(永久节点 + 带序号)
(1)先创建一个普通的根节点/sanguo/weiguo
[zk: localhost:2181(CONNECTED) 1] create /sanguo/weiguo "caocao" Created /sanguo/weiguo
(2)创建带序号的节点
[zk: localhost:2181(CONNECTED) 2] create -s /sanguo/weiguo/zhangliao "zhangliao" Created /sanguo/weiguo/zhangliao0000000000 [zk: localhost:2181(CONNECTED) 3] create -s /sanguo/weiguo/zhangliao "zhangliao" Created /sanguo/weiguo/zhangliao0000000001 [zk: localhost:2181(CONNECTED) 4] create -s /sanguo/weiguo/xuchu "xuchu" Created /sanguo/weiguo/xuchu0000000002
如果原来没有序号节点,序号从0开始依次递增。如果原节点下已有2个节点,则再排
序时从2开始,以此类推。
4)创建短暂节点(短暂节点 + 不带序号 or 带序号)
(1)创建短暂的不带序号的节点
[zk: localhost:2181(CONNECTED) 7] create -e /sanguo/wuguo "zhouyu" Created /sanguo/wuguo
(2)创建短暂的带序号的节点
[zk: localhost:2181(CONNECTED) 2] create -e -s /sanguo/wuguo "zhouyu" Created /sanguo/wuguo0000000001
(3)在当前客户端是能查看到的
[zk: localhost:2181(CONNECTED) 3] ls /sanguo [wuguo, wuguo0000000001, shuguo]
(4)退出当前客户端然后再重启客户端
[zk: localhost:2181(CONNECTED) 12] quit [yudan@hadoop104 zookeeper-3.5.7]$ bin/zkCli.sh
(5)再次查看根目录下短暂节点已经删除
[zk: localhost:2181(CONNECTED) 0] ls /sanguo [shuguo]
5)修改节点数据值
[zk: localhost:2181(CONNECTED) 6] set /sanguo/weiguo "simayi"
客户端注册监听它关心的目录节点,当目录节点发生变化(数据改变、节点删除、子目录节点增加删除)时,ZooKeeper会通知客户端。监听机制保证ZooKeeper保存的任何的数据的任何改变都能快速的响应到监听了该节点的应用程序。
1)节点的值变化监听
(1)在hadoop104主机上注册监听/sanguo节点数据变化
[zk: localhost:2181(CONNECTED) 26] get -w /sanguo
(2)在hadoop103主机上修改/sanguo节点的数据
[zk: localhost:2181(CONNECTED) 1] set /sanguo "xisi"
(3)观察hadoop104主机收到数据变化的监听
WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo 注意:在hadoop103再多次修改/sanguo的值,hadoop104上不会再收到监听。因为注册一次,只能监听一次。想再次监听,需要再次注册。
2)节点的子节点变化监听(路径变化)
(1)在hadoop104主机上注册监听/sanguo节点的子节点变化
[zk: localhost:2181(CONNECTED) 1] ls -w /sanguo [shuguo, weiguo]
(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 注意:节点的路径变化,也是注册一次,生效一次。想多次生效,就需要多次注册。
1)删除节点
[zk: localhost:2181(CONNECTED) 4] delete /sanguo/jin
2)递归删除节点
[zk: localhost:2181(CONNECTED) 15] deleteall /sanguo/shuguo
3)查看节点状态
[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
前提:保证hadoop102、hadoop103、hadoop104服务器上Zookeeper集群服务端启动。
1)创建一个maven工程:zookeeper
2)添加pom文件
junit junit RELEASE org.apache.logging.log4j log4j-core 2.8.2 org.apache.zookeeper zookeeper 3.5.7
3)拷贝log4j.properties文件到项目根目录
需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在
文件中填入。
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c]- %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
4)创建包名com.yudan.zk
5)创建类名称zkClient
import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.List; public class zkClient { // 注意:逗号左右不能有空格 private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181"; // 连接客户端的超时时间(SessionTimeout)起码要大于原来zookeeper服务端设置的延迟时间(tickTime*initLimit),否则就会因为超时而报错 // tickTime:通信心跳时间,ZooKeeper服务器与客户端心跳时间(单位:毫秒 // initLimit:初次通信时限,LF(Leader,Follower)初始连接时能容忍的最多心跳数(tickTime的个数) // 在zoo.cfg配置文件中 tickTime = 2000 initLimit = 10 zookeeper服务端的延迟时间 = 20000 // sessionTimeout > LF > 20000 private int sessionTimeout = 100000; private ZooKeeper zkClient; @Before public void init() throws IOException { zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { Listchildren = null; try { children = zkClient.getChildren("/", true); System.out.println("---------------------------------"); for(String child : children) { System.out.println(child); } } catch (KeeperException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } } }); }
// 创建子节点 @Test public void create() throws InterruptedException, KeeperException { // 参数1:要创建的节点的路径; 参数2:节点数据 ; 参数3:节点权限 ;参数4:节点的类型 String nodeCreated = zkClient.create("/yudan", "yudan1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); }
测试:在hadoop102的zk客户端上查看创建节点情况
[zk: localhost:2181(CONNECTED) 16] get -s /yudan yudan1
// 获取子节点 @Test public void getChildren() throws InterruptedException, KeeperException { // 延时阻塞 Thread.sleep(Long.MAX_VALUE); }
(1)在IDEA控制台上看到如下节点:
zookeeper sanguo yudan
(2)在hadoop102的客户端上创建再创建一个节点/yudan1,观察IDEA控制台
[zk: localhost:2181(CONNECTED) 3] create /yudan1 "yudan1"
(3)在hadoop102的客户端上删除节点/atguigu1,观察IDEA控制台
[zk: localhost:2181(CONNECTED) 4] delete /yudan1
// 判断znode是否存在 @Test public void exist() throws InterruptedException, KeeperException { Stat stat = zkClient.exists("/yudan", false); System.out.println(stat==null?"not exist":"exist"); }
上一篇:数学博士张德馨