02_Zookeeper客户端、节点

运行 zkCli.sh –server <ip>进入命令行工具

1、使用 ls 命令来查看当前 ZooKeeper 中所包含的内容:
[zk: 202.115.36.251:2181(CONNECTED) 1] ls /
2、创建一个新的 znode ,使用 create /zk myData 。这个命令创建了一个新的 znode 节点“ zk ”以及与它关联的字符串:
[zk: 202.115.36.251:2181(CONNECTED) 2] create /zk "myData“
3、我们运行 get 命令来确认 znode 是否包含我们所创建的字符串:

[zk: 202.115.36.251:2181(CONNECTED) 3] get /zk
#监听这个节点的变化,当另外一个客户端改变/zk时,它会打出下面的
#WATCHER::
#WatchedEvent state:SyncConnected type:NodeDataChanged path:/zk
[zk: localhost:2181(CONNECTED) 4] get /zk watch

4、下面我们通过 set 命令来对 zk 所关联的字符串进行设置:
[zk: 202.115.36.251:2181(CONNECTED) 4] set /zk "zsl“
5、下面我们将刚才创建的 znode 删除:
[zk: 202.115.36.251:2181(CONNECTED) 5] delete /zk
6、删除节点:rmr
[zk: 202.115.36.251:2181(CONNECTED) 5] rmr /zk

客户端

  • 使用客户端连接
[root@hadoop bin]# cd /apps/zookeeper/zookeeper-3.4.5/bin
[root@hadoop bin]# ./zkCli.sh 
Connecting to localhost:2181
2017-08-28 22:28:28,598 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2017-08-28 22:28:28,601 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=google-public-dns-a.google.com
2017-08-28 22:28:28,601 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.7.0_45
2017-08-28 22:28:28,602 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-08-28 22:28:28,602 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.7.0_45/jre
2017-08-28 22:28:28,602 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/apps/zookeeper/zookeeper-3.4.5/bin/../build/classes:/apps/zookeeper/zookeeper-3.4.5/bin/../build/lib/*.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../lib/slf4j-log4j12-1.6.1.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../lib/slf4j-api-1.6.1.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../lib/netty-3.2.2.Final.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../lib/log4j-1.2.15.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../lib/jline-0.9.94.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../zookeeper-3.4.5.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../src/java/lib/*.jar:/apps/zookeeper/zookeeper-3.4.5/bin/../conf:
2017-08-28 22:28:28,603 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-08-28 22:28:28,603 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-08-28 22:28:28,603 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2017-08-28 22:28:28,604 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2017-08-28 22:28:28,604 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2017-08-28 22:28:28,604 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=2.6.32-573.el6.x86_64
2017-08-28 22:28:28,605 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2017-08-28 22:28:28,605 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2017-08-28 22:28:28,605 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/apps/zookeeper/zookeeper-3.4.5/bin
2017-08-28 22:28:28,607 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@10636a7e
Welcome to ZooKeeper!
2017-08-28 22:28:28,922 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@966] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2017-08-28 22:28:28,961 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@849] - Socket connection established to localhost/127.0.0.1:2181, initiating session
JLine support is enabled
[zk: localhost:2181(CONNECTING) 0] 2017-08-28 22:28:29,064 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x15e2bcce47b0000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: localhost:2181(CONNECTED) 0] 

连接成功,连接到本机的zookeeper

  • 连接到指定机器的zookeeper
[zk: localhost:2181(CONNECTED) 0] connect mini2:2181
2017-08-28 22:31:56,498 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@509] - EventThread shut down
2017-08-28 22:31:56,499 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x15e2bcce47b0000 closed
2017-08-28 22:31:56,503 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=mini2:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@c04fe90
ZooKeeper -server host:port cmd args
        connect host:port
        get path [watch]
        ls path [watch]
        set path data [version]
        rmr path
        delquota [-n|-b] path
        quit 
        printwatches on|off
        create [-s] [-e] path data acl
        stat path [watch]
        close 
        ls2 path [watch]
        history 
        listquota path
        setAcl path acl
        getAcl path
        sync path
        redo cmdno
        addauth scheme auth
        delete path [version]
        setquota -n|-b val path
[zk: mini2:2181(CONNECTING) 1] 2017-08-28 22:31:56,509 [myid:] - INFO  [main-SendThread(mini2:2181):ClientCnxn$SendThread@966] - Opening socket connection to server mini2/192.168.25.202:2181. Will not attempt to authenticate using SASL (unknown error)
2017-08-28 22:31:56,510 [myid:] - INFO  [main-SendThread(mini2:2181):ClientCnxn$SendThread@849] - Socket connection established to mini2/192.168.25.202:2181, initiating session
2017-08-28 22:31:56,517 [myid:] - INFO  [main-SendThread(mini2:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server mini2/192.168.25.202:2181, sessionid = 0x25e2bc9b9bb0000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: mini2:2181(CONNECTED) 1] 
  • 查看客户端使用帮助
[zk: mini2:2181(CONNECTED) 1] help
ZooKeeper -server host:port cmd args
        connect host:port
        get path [watch]
        ls path [watch]
        set path data [version]
        rmr path
        delquota [-n|-b] path
        quit 
        printwatches on|off
        create [-s] [-e] path data acl
        stat path [watch]
        close 
        ls2 path [watch]
        history 
        listquota path
        setAcl path acl
        getAcl path
        sync path
        redo cmdno
        addauth scheme auth
        delete path [version]
        setquota -n|-b val path
[zk: mini2:2181(CONNECTED) 2] 

zookeeper特性

  • zookeeper特性
    1、Zookeeper:一个leader,多个follower组成的集群
    2、全局数据一致:每个server保存一份相同的数据副本,client无论连接到哪个server,数据都是一致的
    3、分布式读写,更新请求转发,由leader实施
    4、更新请求顺序进行,来自同一个client的更新请求按其发送顺序依次执行
    5、数据更新原子性,一次数据更新要么成功,要么失败
    6、实时性,在一定时间范围内,client能读到最新数据

  • zookeeper数据结构
    1、层次化的目录结构,命名符合常规文件系统规范(见下图)
    2、每个节点在zookeeper中叫做znode,并且其有一个唯一的路径标识
    3、节点Znode可以包含数据和子节点(但是EPHEMERAL类型的节点不能有子节点,下一页详细讲解)
    4、客户端应用可以在节点上设置监视器(后续详细讲解)

zookeeper数据结构图
  • 创建数据节点并设置值
[zk: mini2:2181(CONNECTED) 2] create /app1 "this is app1 node"
Created /app1
[zk: mini2:2181(CONNECTED) 4] create /app1/server01 "hello192.168.25.202" 
Created /app1/server01
[zk: mini2:2181(CONNECTED) 6] ls /
[app1, zookeeper]
[zk: mini2:2181(CONNECTED) 7] ls /app1
[server01]

  • 获取节点的值
[zk: mini2:2181(CONNECTED) 8] get /app1
"this
cZxid = 0x200000004
ctime = Mon Aug 28 22:49:47 EDT 2017
mZxid = 0x200000004
mtime = Mon Aug 28 22:49:47 EDT 2017
pZxid = 0x200000005
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 1
[zk: mini2:2181(CONNECTED) 9] get /app1/server01
"hello192.168.25.202"
cZxid = 0x200000005
ctime = Mon Aug 28 22:50:36 EDT 2017
mZxid = 0x200000005
mtime = Mon Aug 28 22:50:36 EDT 2017
pZxid = 0x200000005
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 21
numChildren = 0
[zk: mini2:2181(CONNECTED) 10] 

节点类型

  1. Znode有两种类型:
    短暂(ephemeral)(断开连接自己删除)
    持久(persistent)(断开连接不删除)
  2. Znode有四种形式的目录节点(默认是persistent )
    PERSISTENT
    PERSISTENT_SEQUENTIAL(持久序列/test0000000019 )
    EPHEMERAL
    EPHEMERAL_SEQUENTIAL
  3. 创建znode时设置顺序标识,znode名称后会附加一个值,顺序号是一个单调递增的计数器,由父节点维护
  4. 在分布式系统中,顺序号可以被用于为所有的事件进行全局排序,这样客户端可以通过顺序号推断事件的顺序

短暂节点

  • 创建短暂的节点
[zk: mini2:2181(CONNECTED) 10] create -e /app-e "nihao"
Created /app-e
[zk: mini2:2181(CONNECTED) 11] ls /
[app1, app-e, zookeeper]
  • 退出客户端再重新连接
[zk: mini2:2181(CONNECTED) 13] quit
[root@hadoop bin]# ./zkCli.sh
  • 查看数据
[zk: localhost:2181(CONNECTED) 0] ls /
[app1, zookeeper]

app-e节点已经不存在

  • 短暂节点下不能创建永久节点
[zk: localhost:2181(CONNECTED) 1] create -e /abd nihao
Created /abd
[zk: localhost:2181(CONNECTED) 2] create /abd/abc nihao
Ephemerals cannot have children: /abd/abc
[zk: localhost:2181(CONNECTED) 3] 

带序号的节点

  • 创建带序号的节点
[zk: localhost:2181(CONNECTED) 1] create /test 888
Created /test
[zk: localhost:2181(CONNECTED) 2] create -s /test/aa 888
Created /test/aa0000000000
[zk: localhost:2181(CONNECTED) 3] create -s /test/bb 888
Created /test/bb0000000001
[zk: localhost:2181(CONNECTED) 4] create -s /test/aa 888
Created /test/aa0000000002
[zk: localhost:2181(CONNECTED) 5] 
  • 修改目录,序号重新开始计算
[zk: localhost:2181(CONNECTED) 5] create -s /app1/aa 888
Created /app1/aa0000000001
[zk: localhost:2181(CONNECTED) 6] create -s /app1/bb 888
Created /app1/bb0000000002
[zk: localhost:2181(CONNECTED) 7] create -s /app1/aa 888
Created /app1/aa0000000003
[zk: localhost:2181(CONNECTED) 8] 

修改节点的值

[zk: localhost:2181(CONNECTED) 8] set /app1 yyyy
cZxid = 0x200000004
ctime = Mon Aug 28 22:49:47 EDT 2017
mZxid = 0x200000010
mtime = Mon Aug 28 23:09:54 EDT 2017
pZxid = 0x20000000f
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
[zk: localhost:2181(CONNECTED) 9] get /app1
yyyy
cZxid = 0x200000004
ctime = Mon Aug 28 22:49:47 EDT 2017
mZxid = 0x200000010
mtime = Mon Aug 28 23:09:54 EDT 2017
pZxid = 0x20000000f
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
[zk: localhost:2181(CONNECTED) 10] 

注意:在集群中通过连接到一台主机的客户端修改了节点的值,在连接到另一台主机的客户端就能马上查询到修改后的值

节点监听

  • 在连接mini1的客户端上执行
[zk: localhost:2181(CONNECTED) 10] get /app1 watch
yyyy
cZxid = 0x200000004
ctime = Mon Aug 28 22:49:47 EDT 2017
mZxid = 0x200000010
mtime = Mon Aug 28 23:09:54 EDT 2017
pZxid = 0x20000000f
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
  • 在连接mini2的客户端上执行
[zk: localhost:2181(CONNECTED) 0] set /app1 uuyy
cZxid = 0x200000004
ctime = Mon Aug 28 22:49:47 EDT 2017
mZxid = 0x200000012
mtime = Mon Aug 28 23:15:51 EDT 2017
pZxid = 0x20000000f
cversion = 4
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
  • 那么在连接mini1的客户端上会有输出
[zk: localhost:2181(CONNECTED) 11] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/app1

注意:监听是在获取数据的时候设置,监听只能是生效一次

监听子节点

  • 在连接mini1的客户端上执行
[zk: localhost:2181(CONNECTED) 11] ls /app1 watch
[bb0000000002, aa0000000001, aa0000000003, server01]
  • 在连接mini2的客户端上执行
[zk: localhost:2181(CONNECTED) 2] create /app1/aa nihao
Created /app1/aa
  • 那么在连接mini1的客户端上会有输出
[zk: localhost:2181(CONNECTED) 12] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/app1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,723评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,485评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,998评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,323评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,355评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,079评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,389评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,019评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,519评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,971评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,100评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,738评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,293评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,289评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,517评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,547评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,834评论 2 345

推荐阅读更多精彩内容