第2章 Zookeeper安裝
2.1 本地模式安裝部署
1.安裝前準備
(1)安裝Jdk
(2)拷貝Zookeeper安裝包到Linux系統(tǒng)下
(3)解壓到指定目錄
[yulong@hadoop102 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/
第4章 Zookeeper實戰(zhàn)(開發(fā)重點)
4.1 分布式安裝部署
1.集群規(guī)劃
在hadoop102、hadoop103和hadoop104三個節(jié)點上部署Zookeeper灯荧。
2.解壓安裝
(1)解壓Zookeeper安裝包到/opt/module/目錄下
[yulong@hadoop102 software]$ tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/
(2)同步/opt/module/zookeeper-3.4.10目錄內容到hadoop103取视、hadoop104
[yulong@hadoop102 module]$ xsync zookeeper-3.4.10/
3.配置服務器編號
(1)在/opt/module/zookeeper-3.4.10/這個目錄下創(chuàng)建zkData
[yulong@hadoop102 zookeeper-3.4.10]$ mkdir zkData
(2)在/opt/module/zookeeper-3.4.10/zkData目錄下創(chuàng)建一個myid的文件
[yulong@hadoop102 zkData]$ touch myid
添加myid文件偏友,注意一定要在linux里面創(chuàng)建,在notepad++里面很可能亂碼
(3)編輯myid文件
[yulong@hadoop102 zkData]$ vi myid
在文件中添加與server對應的編號:
2
(4)拷貝配置好的zookeeper到其他機器上
[yulong@hadoop102 zkData]$ xsync myid
并分別在hadoop103捷犹、hadoop104上修改myid文件中內容為3、4
4.配置zoo.cfg文件
(1)重命名/opt/module/zookeeper-3.4.10/conf這個目錄下的zoo_sample.cfg為zoo.cfg
[yulong@hadoop102 conf]$ mv zoo_sample.cfg zoo.cfg
(2)打開zoo.cfg文件
[yulong@hadoop102 conf]$ vim zoo.cfg
修改數據存儲路徑配置
dataDir=/opt/module/zookeeper-3.4.10/zkData
增加如下配置
#######################cluster##########################
server.2=hadoop102:2888:3888
server.3=hadoop103:2888:3888
server.4=hadoop104:2888:3888
(3)同步zoo.cfg配置文件
[yulong@hadoop102 conf]$ xsync zoo.cfg
(4)配置參數解讀
server.A=B:C:D。
A是一個數字名眉,表示這個是第幾號服務器;
集群模式下配置一個文件myid凰棉,這個文件在dataDir目錄下损拢,這個文件里面有一個數據就是A的值,Zookeeper啟動時讀取此文件撒犀,拿到里面的數據與zoo.cfg里面的配置信息比較從而判斷到底是哪個server福压。
B是這個服務器的ip地址;
C是這個服務器與集群中的Leader服務器交換信息的端口或舞;
D是萬一集群中的Leader服務器掛了荆姆,需要一個端口來重新進行選舉,選出一個新的Leader映凳,而這個端口就是用來執(zhí)行選舉時服務器相互通信的端口胆筒。
4.集群操作
(1)分別啟動Zookeeper
[yulong@hadoop102 zookeeper-3.4.10]$ bin/zkServer.sh start
[yulong@hadoop103 zookeeper-3.4.10]$ bin/zkServer.sh start
[yulong@hadoop104 zookeeper-3.4.10]$ bin/zkServer.sh start
(2)查看狀態(tài)
[yulong@hadoop102 zookeeper-3.4.10]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
[yulong@hadoop103 zookeeper-3.4.10]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
[yulong@hadoop104 zookeeper-3.4.5]# bin/zkServer.sh status
JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
4.2 客戶端命令行操作
表5-1
命令基本語法功能描述
help顯示所有操作命令
ls path [watch]使用ls 命令來查看當前znode中所包含的內容
ls2 path [watch]查看當前節(jié)點數據并能看到更新次數等數據
create普通創(chuàng)建
-s含有序列
-e臨時(重啟或者超時消失)
get path [watch]獲得節(jié)點的值
set設置節(jié)點的具體值
stat查看節(jié)點狀態(tài)
delete刪除節(jié)點
rmr遞歸刪除節(jié)點
1.啟動客戶端
[yulong@hadoop103 zookeeper-3.4.10]$ bin/zkCli.sh
2.顯示所有操作命令
[zk: localhost:2181(CONNECTED) 1] help
3.查看當前znode中所包含的內容
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
4.查看當前節(jié)點詳細數據
[zk: localhost:2181(CONNECTED) 1] ls2 /
[zookeeper]
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
5.分別創(chuàng)建2個普通節(jié)點
[zk: localhost:2181(CONNECTED) 3] create /sanguo "jinlian"
Created /sanguo
[zk: localhost:2181(CONNECTED) 4] create /sanguo/shuguo "liubei"
Created /sanguo/shuguo
6.獲得節(jié)點的值
[zk: localhost:2181(CONNECTED) 5] get /sanguo
jinlian
cZxid = 0x100000003
ctime = Wed Aug 29 00:03:23 CST 2018
mZxid = 0x100000003
mtime = Wed Aug 29 00:03:23 CST 2018
pZxid = 0x100000004
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 1
[zk: localhost:2181(CONNECTED) 6]
[zk: localhost:2181(CONNECTED) 6] get /sanguo/shuguo
liubei
cZxid = 0x100000004
ctime = Wed Aug 29 00:04:35 CST 2018
mZxid = 0x100000004
mtime = Wed Aug 29 00:04:35 CST 2018
pZxid = 0x100000004
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
7.創(chuàng)建短暫節(jié)點
[zk: localhost:2181(CONNECTED) 7] create -e /sanguo/wuguo "zhouyu"
Created /sanguo/wuguo
(1)在當前客戶端是能查看到的
[zk: localhost:2181(CONNECTED) 3] ls /sanguo
[wuguo, shuguo]
(2)退出當前客戶端然后再重啟客戶端
[zk: localhost:2181(CONNECTED) 12] quit
[yulong@hadoop104 zookeeper-3.4.10]$ bin/zkCli.sh
(3)再次查看根目錄下短暫節(jié)點已經刪除
[zk: localhost:2181(CONNECTED) 0] ls /sanguo
[shuguo]
8.創(chuàng)建帶序號的節(jié)點
(1)先創(chuàng)建一個普通的根節(jié)點/sanguo/weiguo
[zk: localhost:2181(CONNECTED) 1] create /sanguo/weiguo "caocao"
Created /sanguo/weiguo
(2)創(chuàng)建帶序號的節(jié)點
[zk: localhost:2181(CONNECTED) 2] create -s /sanguo/weiguo/xiaoqiao "jinlian"
Created /sanguo/weiguo/xiaoqiao0000000000
[zk: localhost:2181(CONNECTED) 3] create -s /sanguo/weiguo/daqiao "jinlian"
Created /sanguo/weiguo/daqiao0000000001
[zk: localhost:2181(CONNECTED) 4] create -s /sanguo/weiguo/diaocan "jinlian"
Created /sanguo/weiguo/diaocan0000000002
如果原來沒有序號節(jié)點,序號從0開始依次遞增魏宽。如果原節(jié)點下已有2個節(jié)點腐泻,則再排序時從2開始决乎,以此類推。
9.修改節(jié)點數據值
[zk: localhost:2181(CONNECTED) 6] set /sanguo/weiguo "simayi"
10.節(jié)點的值變化監(jiān)聽
(1)在hadoop104主機上注冊監(jiān)聽/sanguo節(jié)點數據變化
[zk: localhost:2181(CONNECTED) 26] [zk: localhost:2181(CONNECTED) 8] get /sanguo watch
(2)在hadoop103主機上修改/sanguo節(jié)點的數據
[zk: localhost:2181(CONNECTED) 1] set /sanguo "xisi"
(3)觀察hadoop104主機收到數據變化的監(jiān)聽
WATCHER::
WatchedEvent state:SyncConnected type:NodeDataChanged path:/sanguo
11.節(jié)點的子節(jié)點變化監(jiān)聽(路徑變化)
(1)在hadoop104主機上注冊監(jiān)聽/sanguo節(jié)點的子節(jié)點變化
[zk: localhost:2181(CONNECTED) 1] ls /sanguo watch
[aa0000000001, server101]
(2)在hadoop103主機/sanguo節(jié)點上創(chuàng)建子節(jié)點
[zk: localhost:2181(CONNECTED) 2] create /sanguo/jin "simayi"
Created /sanguo/jin
(3)觀察hadoop104主機收到子節(jié)點變化的監(jiān)聽
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/sanguo
12.刪除節(jié)點
[zk: localhost:2181(CONNECTED) 4] delete /sanguo/jin
13.遞歸刪除節(jié)點
[zk: localhost:2181(CONNECTED) 15] rmr /sanguo/shuguo
14.查看節(jié)點狀態(tài)
[zk: localhost:2181(CONNECTED) 17] stat /sanguo
cZxid = 0x100000003
ctime = Wed Aug 29 00:03:23 CST 2018
mZxid = 0x100000011
mtime = Wed Aug 29 00:21:23 CST 2018
pZxid = 0x100000014
cversion = 9
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 1