- shell角度
- API角度
Zookeeper可单节点,可分布式
作用: distributed coordination(分布式协调)
同一时间只有一个对外提供服务,另外的就是standy.
当active挂了,就想standy切过来即可,无感知.
监控节点的状态信息,完全可以用过zk来实现
安装配置:
$ tar -zxvf zookeeper-3.4.5-cdh5.7.0.tar.gz -C ~/apps/
$ cd ~/apps/zookeeper-3.4.5-cdh5.7.0
配置ZK_HOME
export ZK_HOME=/home/hadoop/apps/zookeeper-3.4.5-cdh5.7.0
export PATH=$ZK_HOME/bin:$PATH
$ cp zoo_sample.cfg zoo.cfg
修改zoo.cfg
```xml
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/hadoop/tmp/zookeeper
clientPort=2181
```
启动:
启动zk服务
$ zkServer.sh start
查看zk状态:
$ zkServer.sh status
JMX enabled by default
Using config: /home/hadoop/apps/zookeeper-3.4.5-cdh5.7.0/bin/../conf/zoo.cfg
Mode: standalone
启动zk客户端命令行
$ zkCli.sh
使用:
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper, hbase]
切换到dataDir目录下,查看zk的pid:
$ pwd
/home/hadoop/tmp/zookeeper
$ cat zookeeper_server.pid
21980
zk的数据模型(重点)
1) 树形结构: /
2) zk中每个节点叫znode,唯一的路径标识
a) 每个znode都有自己的版本号
b) 如果znode节点的信息发生变化,version+1
c) znode数据量不要太大,几K最多
d) 权限,不同人不同权限
e) Watcher(监视器)
3) znode有两种类型
a) 临时: 当前session有效(当前客户端关掉,就会删掉),不能有子节点
b) 永久: 不依赖于session
4) znode四种形式
PERSISTENT 持久
PERSISTENT_SEQUENTIAL
EPHEMERAL 临时
EPHEMERAL_SEQUENTIAL
启动脚本:
$ vi zkServer.sh
case $1 in
start)
echo -n "Starting zookeeper ... "
shell使用:
状态信息:ls2 = ls + stat
[zk: localhost:2181(CONNECTED) 1] ls2 /
[zookeeper, hbase]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
get获取值
[zk: localhost:2181(CONNECTED) 4] get /
cZxid = 0x0 // 节点znode id
ctime = Thu Jan 01 08:00:00 CST 1970 //当前时间
mZxid = 0x0 //修改后的znode id
mtime = Thu Jan 01 08:00:00 CST 1970 //修改后的时间(带m的就是修改)
pZxid = 0x2 //最后更新的子节点id
cversion = 0 //子节点的版本号
dataVersion = 0 //数据版本
aclVersion = 0 //权限版本
ephemeralOwner = 0x0 //是否是临时的
dataLength = 0 //数据长度
numChildren = 2 //几个孩子
create
[zk: localhost:2181(CONNECTED) 6] create /ruoze ruoze-date
Created /ruoze
[zk: localhost:2181(CONNECTED) 7] ls /
[ruoze, zookeeper, hbase]
[zk: localhost:2181(CONNECTED) 8] get /ruoze
ruoze-date //数据信息
cZxid = 0xd7
ctime = Fri Jun 21 09:13:11 CST 2019
mZxid = 0xd7
mtime = Fri Jun 21 09:13:11 CST 2019
pZxid = 0xd7
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0 //非临时节点
dataLength = 10 //ruoze-date 长度为10
numChildren = 0
[zk: localhost:2181(CONNECTED) 9] create -e /ruoze/xiaoruoze xiaoruoze //-e: 临时节点
Created /ruoze/xiaoruoze
[zk: localhost:2181(CONNECTED) 10] get /ruoze
ruoze-date
cZxid = 0xd7
ctime = Fri Jun 21 09:13:11 CST 2019
mZxid = 0xd7
mtime = Fri Jun 21 09:13:11 CST 2019
pZxid = 0xd8 //最后更新的子节点id
cversion = 1 //(修改了一次,子版本加1 version+1)
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 1 //孩子个数1 (xiaoruoze)
[zk: localhost:2181(CONNECTED) 11] get /ruoze/xiaoruoze
xiaoruoze
cZxid = 0xd8
ctime = Fri Jun 21 09:16:40 CST 2019
mZxid = 0xd8
mtime = Fri Jun 21 09:16:40 CST 2019
pZxid = 0xd8
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x16b7114af0e000c
dataLength = 9 // xiaoruoze长度为9
numChildren = 0
退出出客户端,然后再进入:
[zk: localhost:2181(CONNECTED) 0] ls /ruoze
[] //此时临时节点 xiaoruoze 没了
创建永久节点:
[zk: localhost:2181(CONNECTED) 0] ls /ruoze
[]
[zk: localhost:2181(CONNECTED) 1] create -s /ruoze/seq seq
Created /ruoze/seq0000000001
[zk: localhost:2181(CONNECTED) 2] create -s /ruoze/seq seq
Created /ruoze/seq0000000002
[zk: localhost:2181(CONNECTED) 3] create -s /ruoze/seq seq
Created /ruoze/seq0000000003
[zk: localhost:2181(CONNECTED) 4] create -s /ruoze/seq seq
Created /ruoze/seq0000000004
[zk: localhost:2181(CONNECTED) 5] create -s /ruoze/seq seq
Created /ruoze/seq0000000005
[zk: localhost:2181(CONNECTED) 6] ls /ruoze
[seq0000000005, seq0000000003, seq0000000004, seq0000000001, seq0000000002]
使用场景: (顺序创建)
分布式锁的zookeeper实现,会借助seq这种方式来实现
不能一次创建多个子目录,必须一层一层创建
[zk: localhost:2181(CONNECTED) 7] create /ruoze/a/b/c abc
Node does not exist: /ruoze/a/b/c
修改数据信息
[zk: localhost:2181(CONNECTED) 8] get /ruoze
ruoze-date
cZxid = 0xd7
ctime = Fri Jun 21 09:13:11 CST 2019
mZxid = 0xd7
mtime = Fri Jun 21 09:13:11 CST 2019
pZxid = 0xdf
cversion = 7
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 5
[zk: localhost:2181(CONNECTED) 9] set /ruoze www.ruozedata.com
cZxid = 0xd7
ctime = Fri Jun 21 09:13:11 CST 2019
mZxid = 0xe1
mtime = Fri Jun 21 09:27:16 CST 2019
pZxid = 0xdf
cversion = 7
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 17
numChildren = 5
[zk: localhost:2181(CONNECTED) 10] get /ruoze
www.ruozedata.com //此时已经更改
cZxid = 0xd7
ctime = Fri Jun 21 09:13:11 CST 2019
mZxid = 0xe1
mtime = Fri Jun 21 09:27:16 CST 2019
pZxid = 0xdf
cversion = 7
dataVersion = 1 //数据版本加1 version+1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 17
numChildren = 5
补充:
乐观锁和悲观锁的实现
delete path [version] 删除时候,加上版本号,乐观锁
zk四字命令:
stat:
查看zk状态信息 (Lists brief details for the server and connected clients.)
[hadoop@hadoop000 version-2]$ pwd
/home/hadoop/tmp/zookeeper/version-2
[hadoop@hadoop000 version-2]$ echo stat | nc localhost 2181
Zookeeper version: 3.4.5-cdh5.7.0--1, built on 03/23/2016 18:31 GMT
Clients:
/0:0:0:0:0:0:0:1:44668[1](queued=0,recved=37808,sent=37808)
/127.0.0.1:38624[1](queued=0,recved=33268,sent=33268)
/0:0:0:0:0:0:0:1:38442[0](queued=0,recved=1,sent=0)
/127.0.0.1:34344[1](queued=0,recved=114,sent=114)
/127.0.0.1:58262[1](queued=0,recved=34430,sent=34430)
/0:0:0:0:0:0:0:1:44674[1](queued=0,recved=33167,sent=33167)
/127.0.0.1:54538[1](queued=0,recved=33157,sent=33157)
/127.0.0.1:49940[1](queued=0,recved=36966,sent=37022)
Latency min/avg/max: 0/0/85
Received: 210151
Sent: 210206
Connections: 8 //连接数
Outstanding: 0
Zxid: 0xeb
Mode: standalone
Node count: 41 //子节点数量
ruok :
测试服务器是否在非错误状态下运行。如果正在运行,服务器将使用imok响应。否则它根本不会响应。查看更多详细信息用 stat Tests if server is running in a non-error state. The server will respond with imok if it is running. Otherwise it will not respond at all. A response of "imok" does not necessarily indicate that the server has joined the quorum, just that the server process is active and bound to the specified client port. Use "stat" for details on state wrt quorum and client connection information.
[hadoop@hadoop000 version-2]$ echo ruok | nc localhost 2181 imok
dump :
列出未完成的会话和短暂的节点。这只适用于leader。 Lists the outstanding sessions and ephemeral nodes. This only works on the leader.
[hadoop@hadoop000 version-2]$ echo dump | nc localhost 2181
SessionTracker dump:
Session Sets (9):
0 expire at Fri Jun 21 09:43:26 CST 2019:
0 expire at Fri Jun 21 09:43:28 CST 2019:
0 expire at Fri Jun 21 09:43:30 CST 2019:
0 expire at Fri Jun 21 09:43:32 CST 2019:
2 expire at Fri Jun 21 09:43:34 CST 2019:
0x16b7114af0e0003
0x16b7114af0e0001
4 expire at Fri Jun 21 09:43:36 CST 2019:
0x16b7114af0e0000
0x16b7114af0e0005
0x16b7114af0e0004
0x16b7114af0e0002
0 expire at Fri Jun 21 09:43:40 CST 2019:
0 expire at Fri Jun 21 09:43:42 CST 2019:
1 expire at Fri Jun 21 09:43:50 CST 2019:
0x16b7114af0e000d
ephemeral nodes dump:
Sessions with Ephemerals (3): 临时节点数量3
0x16b7114af0e0001:
/hbase/rs/hadoop000,43741,1560970329064
0x16b7114af0e0000:
/hbase/master
0x16b7114af0e000d:
/tmp //创建临时节点后,所显示的
// 创建临时节点,便于在测试dump
[zk: localhost:2181(CONNECTED) 36] create -e /tmp 123
Created /tmp
conf :
打印服务配置的详细信息 New in 3.3.0: Print details about serving configuration.
[hadoop@hadoop000 version-2]$ echo conf | nc localhost 2181
clientPort=2181
dataDir=/home/hadoop/localhbase/zookeeper/zookeeper_0/version-2
dataLogDir=/home/hadoop/localhbase/zookeeper/zookeeper_0/version-2 //此处的dataDir和dataLogDir配置的是一个目录
tickTime=2000
maxClientCnxns=300
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=0
cons :
列出连接到此服务器的所有客户端的完整连接/会话详细信息。包括有关接收/发送的数据包数量,会话ID,操作延迟,上次执行的操作等信息... New in 3.3.0: List full connection/session details for all clients connected to this server. Includes information on numbers of packets received/sent, session id, operation latencies, last operation performed, etc...
[hadoop@hadoop000 version-2]$ echo cons | nc localhost 2181
/0:0:0:0:0:0:0:1:48434[1](queued=0,recved=549,sent=549,sid=0x16b782855880001,lop=PING,est=1561089061458,to=10000,lcxid=0x3d,lzxid=0x149,lresp=1561090699393,llat=1,minlat=0,avglat=1,maxlat=129)
/0:0:0:0:0:0:0:1:55212[0](queued=0,recved=1,sent=0)
/0:0:0:0:0:0:0:1:39984[1](queued=0,recved=494,sent=494,sid=0x16b782855880005,lop=PING,est=1561089072589,to=10000,lcxid=0x5,lzxid=0x149,lresp=1561090701748,llat=1,minlat=0,avglat=0,maxlat=26)
/0:0:0:0:0:0:0:1:34008[1](queued=0,recved=599,sent=599,sid=0x16b782855880003,lop=PING,est=1561089069547,to=10000,lcxid=0x70,lzxid=0x149,lresp=1561090699284,llat=0,minlat=0,avglat=0,maxlat=21)
/0:0:0:0:0:0:0:1:54780[1](queued=0,recved=2836,sent=2851,sid=0x16b782855880000,lop=PING,est=1561089050094,to=10000,lcxid=0x929,lzxid=0x149,lresp=1561090702189,llat=0,minlat=0,avglat=0,maxlat=257)
/127.0.0.1:58972[1](queued=0,recved=492,sent=492,sid=0x16b782855880004,lop=PING,est=1561089069547,to=10000,lcxid=0x2,lzxid=0x149,lresp=1561090702189,llat=0,minlat=0,avglat=0,maxlat=11)
/0:0:0:0:0:0:0:1:52670[1](queued=0,recved=66,sent=66,sid=0x16b782855880009,lop=PING,est=1561090153029,to=30000,lcxid=0x11,lzxid=0x149,lresp=1561090693148,llat=0,minlat=0,avglat=2,maxlat=23)
/127.0.0.1:58968[1](queued=0,recved=495,sent=495,sid=0x16b782855880002,lop=PING,est=1561089069546,to=10000,lcxid=0x6,lzxid=0x149,lresp=1561090700141,llat=0,minlat=0,avglat=0,maxlat=11)
mntr :
输出可用于监视群集运行状况的变量列表 New in 3.4.0: Outputs a list of variables that could be used for monitoring the health of the cluster.
[hadoop@hadoop000 version-2]$ echo mntr | nc localhost 2181
zk_version 3.4.5-cdh5.7.0--1, built on 03/23/2016 18:31 GMT
zk_avg_latency 0
zk_max_latency 85
zk_min_latency 0
zk_packets_received 211466
zk_packets_sent 211521
zk_num_alive_connections 8
zk_outstanding_requests 0
zk_server_state standalone
zk_znode_count 42
zk_watch_count 23
zk_ephemerals_count 3
zk_approximate_data_size 1560
zk_open_file_descriptor_count 474
zk_max_file_descriptor_count 4096
wchs :
列出服务器 Watch(锁)的简要信息。 New in 3.3.0: Lists brief information on watches for the server. (当前有多少个session)
[hadoop@hadoop000 version-2]$ echo wchs | nc localhost 2181
3 connections watching 11 paths
Total watches:14
[zk: localhost:2181(CONNECTED) 38] create /ruoze ruozedata
Node already exists: /ruoze
[zk: localhost:2181(CONNECTED) 39] get /ruoze watch
spark-ruoze
cZxid = 0xd7
ctime = Fri Jun 21 09:13:11 CST 2019
mZxid = 0xe2
mtime = Fri Jun 21 09:28:48 CST 2019
pZxid = 0xeb
cversion = 10
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 11
numChildren = 2
重点:
ZooKeeper is replicated
zk节点数: 奇数个(>=3)
存活的机器必须大于(N/2)+1台
zk集群的写操作,由leader负责,会把通知所有节点写入操作,
只有收到半数以上节点的成功反馈,才算成功
3: 2
4: 2
5: 3
leader+follwer
API编程操作:
连接上zk后就立刻返回
public void process(WatchedEvent watchedEvent) {
if (Event.KeeperState.SyncConnected == watchedEvent.getState()) {
logger.warn("接收到Watch通知:{}", watchedEvent);
connected.countDown();
}
}
connected.await(); //等到countDown执行完,才会往下走 Java并发中的东西
watch 是zk中的锁 可以设置watch为null