Zookeeper使用

  • 启动ZOOKEEPER
    配置好ZOOKEEPER后,进入./bin 目录,执行 ./zkServer.sh start
[hadoop@hadoop4 bin]$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apps/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
  • 查看ZOOKEEPER配置
    zkServer.sh status
[hadoop@hadoop2 bin]$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Mode: leader
  • jps
    查看java进程
[hadoop@hadoop2 bin]$ jps
24538 Jps
22847 QuorumPeerMain
  • 关闭ZOOKEEPER
    ./zkServer.sh stop
[hadoop@hadoop4 bin]$ ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /home/hadoop/apps/zookeeper-3.4.10/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[hadoop@hadoop4 bin]$ ./zkServer

客户端使用

  • 登录客户端,默认登录本地的服务器
    ./zkCli.sh

  • 登录远程ZOOKEEPER节点

./zkCli.sh -server hadoop3:2181

Connecting to hadoop3:2181
2018-01-15 22:32:42,666 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2018-01-15 22:32:42,669 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=hadoop1
2018-01-15 22:32:42,670 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_151
2018-01-15 22:32:42,671 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/bigdata/jdk1.8.0_151/jre
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/bin/../zookeeper-3.4.10.jar:/usr/local/zookeeper/bin/../src/java/lib/*.jar:/usr/local/zookeeper/bin/../conf:
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-693.el7.x86_64
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=hadoop
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/home/hadoop
2018-01-15 22:32:42,672 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/home/hadoop/apps/zookeeper-3.4.10/bin
2018-01-15 22:32:42,675 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=hadoop3:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@1a86f2f1
Welcome to ZooKeeper!
2018-01-15 22:32:42,697 [myid:] - INFO  [main-SendThread(hadoop3:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server hadoop3/10.211.55.8:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2018-01-15 22:32:42,760 [myid:] - INFO  [main-SendThread(hadoop3:2181):ClientCnxn$SendThread@876] - Socket connection established to hadoop3/10.211.55.8:2181, initiating session
2018-01-15 22:32:42,784 [myid:] - INFO  [main-SendThread(hadoop3:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server hadoop3/10.211.55.8:2181, sessionid = 0x460fa0697de0002, negotiated timeout = 30000
[zk: hadoop3:2181(CONNECTED) 0]
WATCHER::

WatchedEvent state:SyncConnected type:None path:null
  • 帮助
    help
[zk: localhost:2181(CONNECTED) 1] 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) 2]
  • stat path
    查看某个节点的属性
[zk: hadoop3:2181(CONNECTED) 2] stat /test
cZxid = 0x5d0000000f
ctime = Mon Jan 15 22:24:15 CST 2018
mZxid = 0x5d00000010
mtime = Mon Jan 15 22:24:27 CST 2018
pZxid = 0x5d0000000f
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
  • stat paht watch
    监听节点状态
[zk: hadoop3:2181(CONNECTED) 5] stat /test watch
cZxid = 0x5d0000000f
ctime = Mon Jan 15 22:24:15 CST 2018
mZxid = 0x5d0000001b
mtime = Mon Jan 15 22:35:33 CST 2018
pZxid = 0x5d0000000f
cversion = 0
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
[zk: hadoop3:2181(CONNECTED) 6]

监听节点,然后我们登录另外一个机器,修改/test节点的值,触发监听

[zk: hadoop3:2181(CONNECTED) 6]
WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/test
  • set path data
    设置某节点的值

  • set path data version
    根据version来设置某节点的值,版本号不一致,则不能修改
    /test的版本是3

[zk: hadoop3:2181(CONNECTED) 8] stat /test
cZxid = 0x5d0000000f
ctime = Mon Jan 15 22:24:15 CST 2018
mZxid = 0x5d0000001e
mtime = Mon Jan 15 22:36:40 CST 2018
pZxid = 0x5d0000001d
cversion = 1
dataVersion = 3
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 1
[zk: hadoop3:2181(CONNECTED) 9] set /test 3333 0
version No is not valid : /test    ---设置出错

[zk: hadoop3:2181(CONNECTED) 12] set /test 3333 3
cZxid = 0x5d0000000f
ctime = Mon Jan 15 22:24:15 CST 2018
mZxid = 0x5d00000021
mtime = Mon Jan 15 22:40:28 CST 2018
pZxid = 0x5d0000001d
cversion = 1
dataVersion = 4
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1
  • ls path
    查看某节点的子节点
[zk: hadoop3:2181(CONNECTED) 13] ls /test
[a]
  • ls path watch
    监听目录结构的变化

监听

[zk: hadoop3:2181(CONNECTED) 14] ls /test watch
[a]

在另外一个客户端,在/test节点下创建新的节点

[zk: localhost:2181(CONNECTED) 7] create /test/c 123
Created /test/c

触发监听

[zk: hadoop3:2181(CONNECTED) 16]
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/test

  • delete
    删除节点,如果存在子节点,不能删除

  • rmr
    删除节点以及子节点

  • get path
    获取某个节点的值

  • get path watch
    获取某个节点的值,并监听节点值的变化

  • create [-s] [-e] path data acl
    -s 创建顺序节点
    -e 创建临时节点

[zk: hadoop3:2181(CONNECTED) 19] create -s /a aaa
Created /a0000000010

客户端取消连接后,节点被删除,临时节点下不能创建子节点

[zk: hadoop3:2181(CONNECTED) 16] create -e /t 123
Created /t
[zk: hadoop3:2181(CONNECTED) 17] create /t/a 123
Ephemerals cannot have children: /t/a
  • connect
    连接某个ZOOKEEPER服务节点
[zk: hadoop3(CONNECTED) 21] connect hadoop0 2181
2018-01-15 22:48:54,097 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x460fa0697de0003 closed
2018-01-15 22:48:54,097 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=hadoop0 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@182decdb
2018-01-15 22:48:54,099 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x460fa0697de0003
[zk: hadoop0(CONNECTING) 22] 2018-01-15 22:48:54,100 [myid:] - INFO  [main-SendThread(hadoop0:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server hadoop0/10.211.55.5:2181. Will not attempt to authenticate using SASL (unknown error)
2018-01-15 22:48:54,101 [myid:] - INFO  [main-SendThread(hadoop0:2181):ClientCnxn$SendThread@876] - Socket connection established to hadoop0/10.211.55.5:2181, initiating session
2018-01-15 22:48:54,145 [myid:] - INFO  [main-SendThread(hadoop0:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server hadoop0/10.211.55.5:2181, sessionid = 0x160fa06156c0000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: hadoop0(CONNECTED) 22]

ZOOKEEPER 四字命令

  • conf
    显示ZOOKEEPER节点配置信息
[hadoop@hadoop1 bin]$ telnet hadoop3 2181
Trying 10.211.55.8...
Connected to hadoop3.
Escape character is '^]'.
conf
clientPort=2181
dataDir=/usr/local/zookeeper/data/version-2
dataLogDir=/usr/local/zookeeper/log/version-2
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=4
initLimit=10
syncLimit=5
electionAlg=3
electionPort=3888
quorumPort=2888
peerType=0
Connection closed by foreign host.
  • cons
    显示某个ZOOKEEPER节点连接信息
[hadoop@hadoop1 bin]$ telnet hadoop3 2181
Trying 10.211.55.8...
Connected to hadoop3.
Escape character is '^]'.
cons
 /10.211.55.9:50720[1](queued=0,recved=1,sent=1,sid=0x460fa0697de0004,lop=SESS,est=1516027865069,to=30000,lcxid=0x0,lzxid=0x5d0000002c,lresp=1516027865081,llat=5,minlat=0,avglat=5,maxlat=5)
 /10.211.55.10:33798[0](queued=0,recved=1,sent=0)

Connection closed by foreign host.
[hadoop@hadoop1 bin]$
  • stat
    输出ZOOKEEPER运行时信息
[hadoop@hadoop1 bin]$ telnet hadoop3 2181
Trying 10.211.55.8...
Connected to hadoop3.
Escape character is '^]'.
stat
Zookeeper version: 3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
Clients:
 /10.211.55.10:33802[0](queued=0,recved=1,sent=0)
 /10.211.55.5:58326[1](queued=0,recved=1,sent=1)
 /10.211.55.9:50720[1](queued=0,recved=16,sent=16)

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

推荐阅读更多精彩内容