原文:https://blog.csdn.net/pengweismile/article/details/115444734
作者:伟娃娃
Zookeeper是Apache Hadoop的一个子项目,它主要用来解决分布式系统中经常遇到的一些数据管理问题,例如,统一命名,状态同步,集群管理,分布式应用配置管理。
Zookeeper的定位,这个动物园的管理者,如下图
ookeeper又2个特性
特性一 树形结构,每个子目录可以被增加删除
特性二 事件监听机制
特性一
zookeeper可以对目录树中的任意一个节点进行监听。监听是只监听目录下面的目录变化。
zookeeper类似一个文件系统,每个子目录被称为目录节点,允许我们自由的增加删除目录节点。
znode节点的类型:
持久化目录节点(PERSISTENT), 创建好之后就永久存在
持久化顺序编号节点(PERSISTENT_SEQUENTIAL),创建好节点后还可以默认带个自增的编号
临时目录节点(EPHEMERAL),和sessionId绑定的,当客户端被关闭之后,对于的临时目录节点会被删除
历史顺序编号目录节点(EPHEMERAL_SEQUENTIAL),临时节点带个自增的编号
容器节点(Container),3.5.3新增的特性,没有子节点的容器节点会被清除掉。
TTL节点,3.5.3新增的特性,位节点设定了失效时间。具体失效时间却决于后台检测失效线程的轮询频率。
临时节点被删除过程:
长连接和短连接的区别,根据自己的业务来定的。
Zookeeper的安装
Step1:配置JAVA环境,检验环境:
java ‐version
Step2: 下载解压 zookeeper
wget https://mirror.bit.edu.cn/apache/zookeeper/zookeeper‐3.5.8/apache‐zookeeper‐3.5.8‐bin.tar.gz
https://zookeeper.apache.org/releases.html#download
tar ‐zxvf apache‐zookeeper‐3.5.8‐bin.tar.gz cd apache‐zookeeper‐3.5.8‐bin
Step5: 检测是否启动成功
ps -ef| grep zookeeper
Step6: 连接服务器
bin/zkCli.sh ‐server ip:port
创建不同的节点
-s: 顺序节点-
e: 临时节点
-c: 容器节点
-t: 可以给节点添加过期时间,默认禁用,需要通过系统参数启用
zoomkeeper在目录上的增删改查
[zk: localhost:2181(CONNECTED) 4] create /test "hello" Created /test [zk: localhost:2181(CONNECTED) 5] get /test hello [zk: localhost:2181(CONNECTED) 6] set /test "hello world" [zk: localhost:2181(CONNECTED) 7] get /test hello world [zk: localhost:2181(CONNECTED) 8] delete /test [zk: localhost:2181(CONNECTED) 9] get /test org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /test [zk: localhost:2181(CONNECTED) 10]
ZK中没有相对路径一说, 所有路径都是绝对路径。
创建子目录,操作
[zk: localhost:2181(CONNECTED) 10] create /test1/sub1 hello Node does not exist: /test1/sub1 [zk: localhost:2181(CONNECTED) 11] create /test1 hello1 Created /test1 [zk: localhost:2181(CONNECTED) 12] create /test1/sub1 "hello sub" Created /test1/sub1 [zk: localhost:2181(CONNECTED) 13] ls -R /test1 /test1 /test1/sub1
创建顺序节点 -s
[zk: localhost:2181(CONNECTED) 22] create /seq Created /seq [zk: localhost:2181(CONNECTED) 24] create -s /seq/Allen- "hello" Created /seq/Allen-0000000001 [zk: localhost:2181(CONNECTED) 25] create -s /seq/Allen- "hello" Created /seq/Allen-0000000002 [zk: localhost:2181(CONNECTED) 26] create -s /seq/Allen- "hello" Created /seq/Allen-0000000003 [zk: localhost:2181(CONNECTED) 27] create -s /seq/Allen- "hello" Created /seq/Allen-0000000004 [zk: localhost:2181(CONNECTED) 28] create -s /seq/Allen- "hello" Created /seq/Allen-0000000005 [zk: localhost:2181(CONNECTED) 30] ls -R /seq /seq /seq/Allen-0000000001 /seq/Allen-0000000002 /seq/Allen-0000000003 /seq/Allen-0000000004 /seq/Allen-0000000005 [zk: localhost:2181(CONNECTED) 31]
创建临时节点 -e:
[zk: localhost:2181(CONNECTED) 31] create -e /ephemeral "hello ephemeral" Created /ephemeral [zk: localhost:2181(CONNECTED) 32] get -s /ephemeral hello ephemeral cZxid = 0x15 ctime = Mon Apr 05 03:57:30 PDT 2021 mZxid = 0x15 mtime = Mon Apr 05 03:57:30 PDT 2021 pZxid = 0x15 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x1001f373c3a0001 dataLength = 15 numChildren = 0
临时节点与持久节点的区别在于上面节点元数据信息的ephemeraOwner的值是不一样的。当客户端关掉,临时节点就没有了。临时节点下面是不能有子节点的。
[zk: localhost:2181(CONNECTED) 34] create -e /ephemeral/sub Ephemerals cannot have children: /ephemeral/sub [zk: localhost:2181(CONNECTED) 35]
[zk: localhost:2181(CONNECTED) 33] get -s /seq null cZxid = 0xe ctime = Mon Apr 05 03:54:16 PDT 2021 mZxid = 0xe mtime = Mon Apr 05 03:54:16 PDT 2021 pZxid = 0x14 cversion = 6 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 6 [zk: localhost:2181(CONNECTED) 34]
创建临时顺序节点 -e -s
[zk: localhost:2181(CONNECTED) 37] create -e -s /seq-eph Created /seq-eph0000000005 [zk: localhost:2181(CONNECTED) 38] create -e -s /seq-eph Created /seq-eph0000000006 [zk: localhost:2181(CONNECTED) 39] create -e -s /seq-eph
创建container节点 -c, 唯一的区别是,当删除掉container节点下的所有子节点后, container节点本身也会被清除掉
[zk: localhost:2181(CONNECTED) 3] create -c /container Created /container [zk: localhost:2181(CONNECTED) 4] create /container/sub1 Created /container/sub1 [zk: localhost:2181(CONNECTED) 5] create /container/sub2 Created /container/sub2 [zk: localhost:2181(CONNECTED) 6] create /container/sub3 Created /container/sub3 [zk: localhost:2181(CONNECTED) 7] ls -R /container /container /container/sub1 /container/sub2 /container/sub3 [zk: localhost:2181(CONNECTED) 8] delete /container/sub1 [zk: localhost:2181(CONNECTED) 9] delete /container/sub2 [zk: localhost:2181(CONNECTED) 10] delete /container/sub3 [zk: localhost:2181(CONNECTED) 11] ls / [container, seq, seq0000000002, test1, zookeeper] [zk: localhost:2181(CONNECTED) 12] ls / [container, seq, seq0000000002, test1, zookeeper] [zk: localhost:2181(CONNECTED) 16] ls / [seq, seq0000000002, test1, zookeeper]
创建就ttl节点 -t ,ttl节点的特性是可以创建一个打失效时间的节点,失效时间过来之后节点会被自动删除。
1. 关闭服务
[allen@localhost bin]$ ./zkServer.sh stop ../zoo.cfg /usr/bin/java ZooKeeper JMX enabled by default Using config: ../zoo.cfg Stopping zookeeper ... ./zkServer.sh: line 213: kill: (61723) - No such process STOPPED [allen@localhost bin]$
2. 添加扩展的业务,在zkServer.sh 中增加如下配置
-Dzookeeper.extendedTypesEnabled=true
3. 重新启动服务
[allen@localhost bin]$ ./zkServer.sh start ../zoo.cfg /usr/bin/java ZooKeeper JMX enabled by default Using config: ../zoo.cfg Starting zookeeper ... STARTED [allen@localhost bin]$ vi zkServer.sh [allen@localhost bin]$ ./zkCli.sh /usr/bin/java Connecting to localhost:2181
如下, ttl-note在一段时间后自己就没有了,不用手动删除。
[zk: localhost:2181(CONNECTED) 0] create -t 5000 /ttl-node ttttt Created /ttl-node [zk: localhost:2181(CONNECTED) 1] ls / [seq, seq0000000002, test1, ttl-node, zookeeper] [zk: localhost:2181(CONNECTED) 2] ls / [seq, seq0000000002, test1, ttl-node, zookeeper] [zk: localhost:2181(CONNECTED) 9] ls / [seq, seq0000000002, test1, ttl-node, zookeeper] [zk: localhost:2181(CONNECTED) 10] ls / [seq, seq0000000002, test1, zookeeper] [zk: localhost:2181(CONNECTED) 11]
标签:seq,zk,Zookeeper,2181,实例,CONNECTED,节点,localhost From: https://www.cnblogs.com/ajianbeyourself/p/17784623.html