文章作者:foochane
原文鏈接:https://foochane.cn/article/2019062601.html
zookeeper數(shù)據(jù)存儲形式 zookeeper安裝 zookeeper命令行客戶端的使用
1 zookeeper數(shù)據(jù)存儲形式
zookeeper中對用戶的數(shù)據(jù)采用kv形式存儲
key:是以路徑的形式表示的,各key之間有父子關(guān)系,比如 /是頂層key
用戶建的key只能在/ 下作為子節(jié)點(diǎn),比如建一個(gè)key: /aa 這個(gè)key可以帶value數(shù)據(jù)
也可以建一個(gè)key: /bb
也可以建多個(gè)key: /aa/xx
zookeeper中,對每一個(gè)數(shù)據(jù)key,稱作一個(gè)znode
2 znode類型
zookeeper中的znode有多種類型:
- 1、
PERSISTENT持久的:創(chuàng)建者就算跟集群斷開聯(lián)系,該類節(jié)點(diǎn)也會(huì)持久存在與zk集群中 - 2、
EPHEMERAL短暫的:創(chuàng)建者一旦跟集群斷開聯(lián)系,zk就會(huì)將這個(gè)節(jié)點(diǎn)刪除 - 3、
SEQUENTIAL帶序號的:這類節(jié)點(diǎn),zk會(huì)自動(dòng)拼接上一個(gè)序號,而且序號是遞增的
組合類型:
-
PERSISTENT:持久不帶序號 -
EPHEMERAL:短暫不帶序號 -
PERSISTENT且SEQUENTIAL:持久且?guī)蛱?/li> -
EPHEMERAL且SEQUENTIAL:短暫且?guī)蛱?/li>
3 安裝zookeeper
解壓安裝包 zookeeper-3.4.6.tar.gz
修改conf/zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/bigdata/data/zkdata
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=Master:2888:3888
server.2=Slave01:2888:3888
server.3=Slave02:2888:3888
對3臺節(jié)點(diǎn),都創(chuàng)建目錄/usr/local/bigdata/data/zkdata
對3臺節(jié)點(diǎn),在工作目錄中生成myid文件,但內(nèi)容要分別為各自的id: 1,2,3
Master上: echo 1 > /usr/local/bigdata/data/zkdata/myid
Slave01上: echo 2 > /usr/local/bigdata/data/zkdata/myid
Slave02上: echo 3 > /usr/local/bigdata/data/zkdata/myid
4 啟動(dòng)zookeeper集群
zookeeper沒有提供自動(dòng)批量啟動(dòng)腳本,需要手動(dòng)一臺一臺地起zookeeper進(jìn)程
在每一臺節(jié)點(diǎn)上,運(yùn)行命令:
$ bin/zkServer.sh start
啟動(dòng)后,用jps應(yīng)該能看到一個(gè)進(jìn)程:QuorumPeerMain
查看狀態(tài)
$ bin/zkServer.sh status
5 編寫啟動(dòng)腳本zkmanage.sh
zookeeper沒有提供批量腳本,不能像hadoop一樣在一臺機(jī)器上同時(shí)啟動(dòng)所有節(jié)點(diǎn),可以自己編寫腳本批量啟動(dòng)。
#!/bin/bash
for host in Master Slave01 Slave02
do
echo "${host}:${1}ing....."
ssh $host "source ~/.bashrc;/usr/local/bigdata/zookeeper-3.4.6/bin/zkServer.sh $1"
done
sleep 2
for host in Master Slave01 Slave02
do
ssh $host "source ~/.bashrc;/usr/local/bigdata/zookeeper-3.4.6/bin/zkServer.sh status"
done
- $1 :指接收第一個(gè)參數(shù)
運(yùn)行命令:
sh zkmanage.sh start #啟動(dòng)
sh zkmanage.sh stop #停止
6 zookeeper命令行客戶端
啟動(dòng)本地客戶端:
$ bin/zkCli.sh
啟動(dòng)其他機(jī)器的客戶端:
$ bin/zkCli.sh -server Master:2181
基本命令:
- 查看幫助:
help - 查看目錄:
ls / - 查看節(jié)點(diǎn)數(shù)據(jù):
get /zookeeper - 插入數(shù)據(jù):
create /節(jié)點(diǎn) 數(shù)據(jù), 如:create /aa hello - 更改某節(jié)點(diǎn)數(shù)據(jù):
set /aa helloworld - 刪除數(shù)據(jù):
rmr /aa/bb - 注冊監(jiān)聽:
get /aa watch-->數(shù)據(jù)發(fā)生改變會(huì)通知 ;ls /aa watch-->目錄發(fā)現(xiàn)改變也會(huì)通知