分布式應用協同服務:ZooKeeper
ZooKeeper是Apache Hadoop的一個子項目,其實現的功能與Google的Chubby基本一致,主要是用來解決分布式應用中經常遇到的一些數據管理問題,如:統一命名服務、狀態同步服務、集群管理、分布式應用配置項的管理等。
數據模型:</span>
zookeeper維護一個層次關系的數據結構,其非常類似于一個標準的文件系統,如下:
znode具有如下特點:
1. ZooKeeper有臨時節點的概念。臨時節點在創建它的會話活動期間存在。會話終止的時候,臨時節點被刪除,所以臨時節點不能有子節點。
2. Znode可以被監控,包括這個目錄節點中存儲的數據的修改,子節點目錄的變化等,一旦變化可以通知設置監控的客戶端。
3. Znode可以保留數據,但是,其不是設計用來作為通用數據庫或者大型對象存儲的,而是用來存儲協調數據的。協調數據的形式可能是配置、狀態信息、聚合等 等。各種形式的協調數據的一個共同特點是:它們通常比較小,以千字節來衡量。ZooKeeper客戶端和服務器實現會進行檢查,以保證znode數據小于 1MB
4.Znode中的數據可以有多個版本,比如某一個路徑下存有多個數據版本,那么查詢這個路徑下的數據就需要帶上版本。
ZooKeeper中的時間:
zxid : 每次修改ZooKeeper狀態都會收到一個zxid形式的時間戳,也就是ZooKeeper事務ID。事務ID是ZooKeeper中所有修改總的次序。每個修改都有唯一的zxid。
版本號 : 對節點的每次修改將使得節點的版本號增加一。版本號有三種:version(znode數據修改的次數)、cversion(znode子節點修改的次數),以及aversion(znode的ACL修改次數)。
tick : 多服務器ZooKeeper中,服務器使用tick來定義狀態上傳、會話超時、節點間連接超時等事件的時序。tick僅被最小會話超時(2倍的tick時間)間接使用:如果客戶端要求小于最小會話超時的時間,服務器將告知客戶端,實際使用的是最小會話超時。
znode會維護一個包含數據修改和ACL修改版本號的 Stat結構體 , 這個結構體還包含時間戳字段。版本號和時間戳讓ZooKeeper可以校驗緩存,協調更新。每次修改znode數據的時候,版本號會增加。客戶端獲取數據 的同時,也會取得數據的版本號。執行更新或者刪除操作時,客戶端必須提供版本號。如果提供的版本號與數據的實際版本不匹配,則更新操作失敗。stat結構 體內容:
czxid : The zxid of the change that caused this znode to be created.
mzxid : The zxid of the change that last modified this znode.
ctime : The time in milliseconds from epoch when this znode was created.
mtime : The time in milliseconds from epoch when this znode was last modified.
version : The number of changes to the data of this znode.
cversion : The number of changes to the children of this znode.
aversion : The number of changes to the ACL of this znode
ephemeralOwner : The session id of the owner of this znode if the znode is an ephemeral node. If it is not an ephemeral node, it will be zero.
dataLength : The length of the data field of this znode.
numChildren : The number of children of this znode.
安裝使用
下載解壓:</span>
$ wget http://mirror.bit.edu.cn/apache//zookeeper/zookeeper-3.4.3/zookeeper-3.4.3.tar.gz .
$ tar -zxvf zookeeper-3.4.3.tar.gz
修改配置:
$ cp zookeeper-3.4.3/conf/zoo_sample.cfg zookeeper-3.4.3/conf/zoo.cfg
$ cat zookeeper-3.4.3/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=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
#
# 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
啟動zookeeper:
ZooKeeper 要求 JAVA 的環境才能運行,并且需要 JAVA6 以上的版本
ubuntu下安裝jdk:
$sudo apt-get install openjdk-7-jre-headless
啟動zookeeper:
$ sudo sh zookeeper-3.4.3/bin/zkServer.sh start
JMX enabled by default
Using config: /home/zxm/zookeeper-3.4.3/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查看相應端口:
$ netstat -na | grep 2181
tcp6 0 0 :::2181 :::* LISTEN
客戶端連接測試:
$ sh zookeeper-3.4.3/bin/zkCli.sh -server 127.0.0.1:2181
.......
輸入help
[zk: 127.0.0.1:2181(CONNECTED) 0] 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: 127.0.0.1:2181(CONNECTED) 4] ls /
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 5] create /test1 99
Created /test1
[zk: 127.0.0.1:2181(CONNECTED) 6] get /test1
99
cZxid = 0xa
ctime = Thu Apr 19 02:08:37 PDT 2012
mZxid = 0xa
mtime = Thu Apr 19 02:08:37 PDT 2012
pZxid = 0xa
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 7] create /test1/mytest 1000
Created /test1/mytest
[zk: 127.0.0.1:2181(CONNECTED) 8] get /test1/mytest
1000
cZxid = 0xb
ctime = Thu Apr 19 02:08:54 PDT 2012
mZxid = 0xb
mtime = Thu Apr 19 02:08:54 PDT 2012
pZxid = 0xb
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
編譯使用C客戶端:
$ cd zookeeper-3.4.3/src/c
$ mkdir prefix
$ ./configure --prefix=/home/zxm/zookeeper-3.4.3/src/c/prefix
$ make
$ ./cli_mt 127.0.0.1:2181
Watcher -1 state = CONNECTED_STATE
Got a new session id: 0x136c9d62bdc0004
ls /
time = 4 msec
/: rc = 0
test1
zookeeper
time = 7 msec
get /test1
time = 2438 msec
/test1: rc = 0
value_len = 2
99
Stat:
ctime = Thu Apr 19 02:08:37 2012
czxid=a
mtime=Thu Apr 19 02:08:37 2012
mzxid=a
version=0 aversion=0
ephemeralOwner = 0
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!