Zookeeper——開源的分佈式應用程序協調服務

簡介

功能:

  1. 可以爲client管理少量數據(key-value)
  2. 可以爲client監聽指定數據節點的狀態並在數據節點發生變化時通知客戶端

Zookeeper的基本功能和應用場景

在這裏插入圖片描述

Zookeeper的整體運行機制

在這裏插入圖片描述

Zookeeper的數據存儲機制

zookeeper中對用戶的數據採用kv形式存儲
只是zk有點特別:
key:是以路徑的形式表示的,那就以爲着,各key之間有父子關係,比如
/ 是頂層key
用戶建的key只能在/ 下作爲子節點,比如建一個key: /aa  這個key可以帶value數據
 也可以建一個key:   /bb
 也可以建key: /aa/xx 
zookeeper中,對每一個數據key,稱作一個znode

集羣安裝、配置、部署、啓動

注意:

初次啓動需要奇數臺節點(3、5臺) 後臺進程名:QuorumPeerMain
Leader工作端口:2888
Follower工作端口:3888

  1. 上傳安裝包,解壓
  2. 修改配置文件conf/zoo.cfg(zoo.cfg.template–>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.
# 數據存儲位置(修改位置1)
dataDir=/root/zkdata
# the port at which the clients will connect
clientPort=2181
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
# server.id = ip:port:port (修改位置2)
server.1=hdp-01:2888:3888
server.2=hdp-02:2888:3888
server.3=hdp-03:2888:3888
  1. 複製拷貝安裝包到其它節點
配置文件修改完後,將安裝包拷貝給其它節點(hdp-02、hdp-03...)
scp -r /path/to/from to to to ...
ex:scp -r /path/to/zookeeper hdp-02:/path/to/zookeeper hdp-02:/path/to/zookeeper
  1. 新建數據目錄,並生成文件myid,內容爲id(與配置文件中id對應)
    接着,到hdp-01上,新建數據目錄/root/zkdata,並在目錄中生成一個文件myid,內容爲1
    接着,到hdp-02上,新建數據目錄/root/zkdata,並在目錄中生成一個文件myid,內容爲2
    接着,到hdp-03上,新建數據目錄/root/zkdata,並在目錄中生成一個文件myid,內容爲3
  2. 啓動zookeeper集羣
lib/zkServer.sh start
  1. 查看zookeeper狀態
lib/zkServer.sh start

自定義zkmanager腳本以啓動zookeeper

#!/bin/bash
for host in hdp-01 hdp-02 hdp-03
do
ehco “${host}:${1}ing...”
ssh $host “source /etc/profile;/path/to/zookeeper/bin/zkServer.sh $1done

./zkmanager.sh start

---------------------------------- 通常到此即可 ----------------------------------

zookeeper客戶端(zkCli.sh 2181)

# 1. 啓動
bin/zkCli.sh -server hdp-02:2181
# 2.命令
ls /      ls /zookeeper
get /zookeeper    get /zookeeper/quta
create /test “hello”
set /test hellospark
help
rmr /test
get /test watch # 監聽節點/test的數據(同步,只一次)
ls /test watch # 監聽/test下的節點

znode類型

1.PERSISTENT 持久的:創建者就算跟集羣斷開聯繫,該節點也會持久存在於zk集羣中
2.EPHEMERAL 短暫的:創建者一旦跟集羣斷開聯繫,zk就會將這個節點刪除
3.SEQUENTIAL 帶序號的:這類節點,zk會自動拼接上一個序號,而且序號是遞增的
> ※組合類型
> PERSISTENT:持久不帶序號
> EPHEMERAL:短暫不帶序號
> PERSISTENT且SEQUENTIAL:持久且帶序號
> EPHEMERAL且SEQUENTIAL:短暫且帶序號 

EX

> create -e /test 999    # -e 短暫
> create -s /test 66     # ???

JAVA-ZOOKEEPER API

// zookeeper地址,2s超時,null監聽回調
Zookeeper zk = new Zookeeper(“hdp-01:2181,hdp-02:2181,hdp-03:2181,2000,null);
// 1.增
// path,data(byte),acl權限,createNode類型   返回值:path(String)
zk.create(/test”,”hello”.getBytes(),Ids.OPEN_ACL_UNSAFE,CreateNode.PERSISTENT);
// 2.改
// path,data,version -1代表任何版本
zk.setData(/test”,”helloworld”.getBytes(“UTF-8),-1);
// 3.查
// path,是否監聽,stat數版本 null代表最新   返回值:data(byte[])
zk.getData(/test”,false,null);
// 4.查子節點
// path,是否監聽     返回值:List<String>節點名稱列表
Zk.getChildren(/test”,false);
// 5.刪
// path,version -1代表任何版本
zk.delete(/test”,-1);
// 6.節點是否存在
Zk.exists(/test”,fasle); //path.是否監聽  返回值:stat 不存在時返回null
Zk.close();

一次監聽

Zookeeper zk = new Zookeeper(“”,2000,null);
byte[] data = zk.getData(/test”,new Watcher(){
  @Override
  public void process(WatchedEvent event){
  sout<<event.getPath(); //節點路徑
  sout<<event.getType();//事件類型
  Zk.getData(/test”,true,null);
}
},null);

一直監聽

zk = new Zookeeper(“”,2000,new Watcher(){
  @Override
  Public void process(WatchedEvent event){
  ...
  try{
  Zk.getData(/test”,true,null);
}catch(KeeperException|InterruptedException e){}
}
});
byte[] data = zk.getData(/test”,true,null);
sleep(Long.MAX_VALUE); // 內部:thread.setDaemon(true); 設置了守護線程
ex:
event.getState()==keeperState.SyncConnected&&event.getType()==EventType.NodeDataChange

zookeeper圖形化客戶端插件

在Eclipse環境下安裝ZooKeeper狀態查看相關的插件步驟如下:
Step 1. 在 Eclipse 菜單打開Help -> Install New Software…
Step 2. 添加 url http://www.massedynamic.org/eclipse/updates/
Step 3. 選擇插件並安裝運行
Step 4. 在 Eclipse 菜單打開Window->Show View->Other…->ZooKeeper 3.2.2。
Step 5. 連接ZK 輸入正在運行的ZK server 地址和端口
連接成功後就就可以在Eclipse裏查看ZK Server裏的節點信息。如下所示:
在這裏插入圖片描述

寫在最後

歡迎留言私信討論;
文章有知識性錯誤請立馬聯繫博主,博主將非常感謝;
無需經過允許即可隨意使用轉載,知識本來就是被廣泛用來學習的;
非常感謝您能看到此處,本文爲博主學習筆記,如有不同見解,請不吝賜教。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章