ZooKeeper系列 (一)ZooKeeper基本簡介與命令和集羣環境搭建

ZooKeeper分佈式應用協調服務:

ZooKeeper架構:

ZooKeeper是一個爲分佈式應用提供協調服務得apache項目

Zookeeper = 文件系統 + 通知機制

  • ZooKeeper從設計模式來看是從觀察者模式設計得分佈式服務管理框架,負責存儲和管理大家都關心得數據。
  • 一旦數據狀態發生變化,註冊再zookeeper得觀察者會做出相應得反應。

ZooKeeper角色:

  • leader領導者
    • 負責投票發起和決議,更新系統狀態
  • follower跟隨者
    • 用於接收客戶端請求,並向客戶端返回結果,選舉時參與投票
  • observer觀察者
    • 可以接收客戶端連接,將寫請求轉發給leader節點,observer不參加投票過程,之同步leader狀態。----爲了擴展系統,提高讀取速度。

ZooKeeper數據結構:–每個zookeeper節點數據一樣

  • znode:
    • 數據模型類似linux文件系統,每個節點稱作一個znode,每個znode默認存1mb數據
  • 節點類型
    • persistent:持久化節點
    • persistent_sequential:持久化順序編號節點
    • ephemeral:臨時節點
    • ephemeral_sequential:臨時順序編號節點

節點文件具體數據結構:

cZxid = 0x100000014 # 創建事務id
ctime = Mon Jul 06 09:47:40 CST 2020 # 創建時間
mZxid = 0x100000014 # 修改事務id
mtime = Mon Jul 06 09:47:40 CST 2020 # 修改時間
pZxid = 0x100000014 # 更新id
cversion = 0 # 子節點修改次數
dataVersion = 0 
aclVersion = 0
ephemeralOwner = 0x0 # 這個節點屬於誰
dataLength = 10 #數據得長度
numChildren = 0 # 子節點數量

ZooKeeper選舉機制:

  • 半數機制:

    • 集羣半數以上機器存活,集羣纔可用–奇數會比偶數多浪費一臺機器

    • zookeeper選舉leader時,一般誰上面數據存的是最新得,誰作爲leader,如果是剛搭建好得集羣,會通過myid最大得來作爲leader,但是也不一定,leader如果已經確定了,就不一定是最大得myid最大得作爲leader。

      zkServer.sh status # 查看節點狀態
      
  • leader選舉機制觸發時機:

    • leader宕機
    • 服務器初始化啓動

ZooKeeper集羣搭建:

第一步: 解壓zookeeper安裝包:

-zxvf zookeeper-3.4.5-cdh5.14.2.tar.gz

第二步: 到對應配置文件夾下–修改配置文件

cd /opt/soft/zk345/conf
cp zoo_sample.cfg zoo.cfg
vi 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=/opt/soft/zk345/tmp
# 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
# 2287 領導和跟隨者之間連接用的,傳遞信息,
# 3387 領導掛掉,跟隨者之間選領導使用
server.1=192.168.56.102:2287:3387
server.2=192.168.56.103:2287:3387
server.3=192.168.56.104:2287:3387

第三步: 修改myid

給每個myid寫上數字寫上-上面配置文件的server.後面的數字

vi /opt/soft/zk345/tmp/myid 

第四步: 啓動zk

# 啓動zk
zkServer.sh

zookeeper命令:

進入zk客戶端

zkCli.sh

ls:查看節點

ls 目錄

create:

# 創建nciedya2臨時節點
create -e /kgc/niceday2 "niceday2"

# 創建nciedya2有序節點
create -s /kgc/niceday2 "niceday2"

# 創建nciedya2臨時有序節點
create -e -s /kgc/niceday2 "niceday2"

get

# 查看節點
get 節點

set

set 節點 值

delete

delete 節點
rmr 節點

監聽節點

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