一、分布式文件系統(tǒng)概述
1、分布式文件系統(tǒng):硬件、軟件分布在不同計算機上,通過消息傳遞進行通信。
2、分布式文件系統(tǒng)存在的典型問題:通信異常、網(wǎng)絡(luò)分區(qū)(分布式環(huán)境下,由于網(wǎng)絡(luò)通訊故障,一些節(jié)點認為應(yīng)用不可用,一些節(jié)點認為應(yīng)用可用,整個系統(tǒng)提供服務(wù)時,造成不一致性)、三態(tài)(成功、失敗、超時)、節(jié)點故障
3、任何分布式系統(tǒng)都必須遵循CAP特性,滿足其中的兩個,P:分區(qū)容錯性,CP:一致性,AP:可用性。
二、zookeeper介紹
1、zookeeper的功能
zookeeper是一個分布式數(shù)據(jù)一致性的解決方案,可以基于它實現(xiàn)數(shù)據(jù)的發(fā)布及訂閱、負載均衡、名稱服務(wù)、分布式協(xié)調(diào)及通知、集群管理、master選舉、分布式鎖和分布式隊列。
2、zookeeper的結(jié)構(gòu)
zookeeper是一個集群,其中包括leader、follower、observer(觀察者)
leader:選舉產(chǎn)生,具備讀寫權(quán)限
follower:參與選舉,可被選舉,具備讀權(quán)限
observer:參與選舉,不可被選舉,具備讀權(quán)限
3、會話
zookeeper中,客戶端與服務(wù)端是通過一個TCP長連接進行通信。
(面)4、數(shù)據(jù)節(jié)點
zookeeper數(shù)據(jù)存儲于內(nèi)存中,數(shù)據(jù)模型為樹狀結(jié)構(gòu),每個znode都會將數(shù)據(jù)存儲在自己的內(nèi)存中,znode包括持久節(jié)點(僅顯示刪除才消失)、臨時節(jié)點(會話終止即消失)、持久化順序節(jié)點(客戶端與 zookeeper 斷開連接后,該節(jié)點依舊存在,只是 Zookeeper 給該節(jié)點名稱進行順序編號)、臨時順序節(jié)點(客戶端與 zookeeper 斷開連接后,該節(jié)點被刪除,只是 Zookeeper 給該節(jié)點名稱進行順序編號)
5、版本
zookeeper為每個znode維護一個stat的數(shù)據(jù)結(jié)構(gòu),記錄當前znode的三個版本,分別為version(當前版本)、cversion(當前znode子節(jié)點版本)、aversion(當前znode的ACL版本)
6、ACL
zookeeper使用ACL進行權(quán)限控制
7、事件監(jiān)聽器
zookeeper上,由用戶指定的觸發(fā)機制,在某些事件產(chǎn)生時,zookeeper能將通知給相關(guān)客戶端。
8、ZAB協(xié)議
ZAB協(xié)議為zookeeper的原子廣播協(xié)議,為了選舉新的leader,并保證數(shù)據(jù)的完整性和一致性。ZAB協(xié)議中存在三種狀態(tài),looking、following、leading,在leader故障或者集群剛啟動時,所有的節(jié)點都處于looking狀態(tài),等到選出leader后,改為following狀態(tài)。
9、安裝
zookeeper的部署安裝可以分為三種類型,單機模式、偽分布式(一個機器,啟動多個zookeeper實例)、分布式模式(集群模式)
10、zookeeper命令的使用
(1)delete和rmr
持久節(jié)點只能使用delete和rmr刪除,文件路徑下沒有子路徑使用delete刪除,存在子路徑,使用rmr
(2)stat(查詢節(jié)點信息)
cZxid:事務(wù)id,創(chuàng)建該事務(wù)的節(jié)點,
mZxid:最近更新該節(jié)點的事務(wù)id
11、watcher
client 端會對某個 znode 建立一個 watcher 事件,當該 znode 發(fā)生變化時,這些client 會收到 zk 的通知,然后 client 可以根據(jù) znode 變化來做出業(yè)務(wù)上的改變等。
12、監(jiān)控zookeeper的四字命令
ruok、stat、srvr、conf、cons、wchs、envi
[root@centos7 bin]# telnet 192.168.43.101 2181
Trying 192.168.43.101...
Connected to 192.168.43.101.
Escape character is '^]'.
ruok
imokConnection closed by foreign host.
[root@centos7 bin]# telnet 192.168.43.101 2181
Trying 192.168.43.101...
Connected to 192.168.43.101.
Escape character is '^]'.
stat
Zookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMT
Clients:
/192.168.43.101:50102[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/1/10
Received: 14
Sent: 13
Connections: 1
Outstanding: 0
Zxid: 0x2
Mode: standalone
Node count: 4
Connection closed by foreign host.
13、zoo.cfg配置文件的參數(shù)
#基本配置參數(shù)
clientPort=2181
dataDir=/data/zookeeper
dataLogDir #事務(wù)日志文件路徑
tickTime=2000
#存儲配置
preAllocsize #為事務(wù)日志預先分配的磁盤空間,默認65536kb
snapCount #每多少次事務(wù)執(zhí)行一次快照,每次事務(wù)平均大小在100字節(jié)
autopurge.snapRetainCount #要保留在 dataDir 中的快照數(shù)
autopurge.purgeInterval #purge操作的時間間隔,0表示不啟動
fsync.warningresholdms #zookeeper進行事務(wù)日志fsync操作時消耗的時間報警閾值
weight.x=N #判斷quornum時的投票權(quán)限,默認1
#網(wǎng)絡(luò)配置
maxClientCnxns #每客戶端IP的最大并發(fā)連接數(shù)
clientPortAddress #zookeeper監(jiān)聽IP地址
minSessionTimeout #默認為ticktime的兩倍
maxSessionTimeout
#集群配置
initLimit #follower連入leader并完成數(shù)據(jù)同步的時長,一般為ticktime的倍數(shù)
syncLimit #心跳檢測的最大延遲
leaderServer #默認zk的leader接收讀寫請求
cnxTimeout #leader選舉期間,各服務(wù)器創(chuàng)建TCP鏈接的超時時長
ellectionAlg #選舉算法,僅支持FastLeaderElection一種
server.id=[hostname]:port:port[:observer]
三、zookeeper的部署安裝
1、獲取zookeeper的源碼文件
https://archive.apache.org/dist/zookeeper/
2、配置jdk
如果系統(tǒng)存在jdk,可以直接使用,如果沒有,安裝好jdk,可選擇較新版的jdk文件,openjdk也可以。
3、解壓壓縮文件到系統(tǒng)中
cd /usr/local/src
tar -xvf zookeeper-3.4.6.tar.gz
4、修改配置文件
cp /usr/local/src/zookeeper-3.4.6/conf/zoo_sample.cfg /usr/local/src/zookeeper-3.4.6/conf/zoo.cfg
mkdir /data/zookeeper
vim 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
dataDir=/data/zookeeper
# 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
5、啟動服務(wù)
cd /usr/local/src/zookeeper-3.4.6/bin
./zkServer.sh start
./zkCli.sh (鏈接客戶端)
6、偽分布式部署
(1)配置文件的修改
在一臺機器上部署了3個Server,需要注意的是在集群為分布式模式下我們使用的每個配置文檔模擬一臺機器,也就是說單臺機器及上運行多個ZooKeeper實例。但是,必須保證每個配置文檔的各個端口號不能沖突,除了clientPort不同之外,dataDir也不同。另外,還要在dataDir所對應(yīng)的目錄中創(chuàng)建myid文件來指定對應(yīng)的ZooKeeper服務(wù)器實例。
clientPort端口:如果在1臺機器上部署多個Server,那么每臺機器都要不同的clientPort,比如Server1是2181,Server2是2182,Server3是2183
dataDir和dataLogDir:dataDir和dataLogDir也需要區(qū)分下,將數(shù)據(jù)文件和日志文件分開存放,同時每個Server的這兩變量所對應(yīng)的路徑都是不同的
server.X和myid: server.X這個數(shù)字就是對應(yīng),data/myid中的數(shù)字。在3個Server的myid文件中分別寫入了0,1,2,那么每個Server中的zoo.cfg都配server.0,server.2,server.3就行了。因為在同一臺機器上,后面連著的2個端口,3個Server都不要一樣,否則端口沖突。
# 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.
dataDir=/usr/local/zk/data_1
# the port at which the clients will connect
clientPort=2181
#the location of the log file
dataLogDir=/usr/local/zk/logs_1
server.0=localhost:2287:3387
server.1=localhost:2288:3388
server.2=localhost:2289:3389
(2)寫入myid文件
echo "0">/usr/local/data_1/myid
echo "1">/usr/local/data_2/myid
echo "2">/usr/local/data_3/myid
(3)服務(wù)啟動
zkServer.sh start zoo1.cfg
zkServer.sh start zoo2.cfg
zkServer.sh start zoo3.cfg