天天看點

12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

ZooKeeper簡介

官網:https://zookeeper.apache.org/doc/r3.5.9/zookeeperOver.html

ZooKeeper 是分布式應用程式的分布式開源協調服務。

分布式應用程式可以基于這些原語來實作更進階别的同步、配置維護以及組和命名服務。它被設計為易于程式設計,并使用以熟悉的檔案系統目錄樹結構為樣式的資料模型。

衆所周知,協調服務很難做好。它們特别容易出現諸如競争條件和死鎖之類的錯誤。ZooKeeper 背後的動機是減輕分布式應用程式從頭開始實作協調服務的責任。

zookeeper叢集兩種狀态,可用狀态、不可用狀态。不可用狀态可以快速選舉出leader恢複成可用狀态,官方壓測200ms左右。

12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

zookeeper是一個目錄樹結構,node節點可以存1M資料。節點包括:持久節點、臨時節點、序列節點(持久、臨時)。臨時節點依托session,session結束,臨時節點立即消失。

12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

安裝、配置、啟動

安裝

# 下載下傳
$ wget https://downloads.apache.org/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz

# 解壓縮
$ tar -xf apache-zookeeper-3.5.9-bin.tar.gz

# 存放快照的目錄
$ mkdir zk
           

配置

修改配置檔案

# 心跳時間
tickTime=2000

# 初始延遲心跳數(時間 = tickTime * initLimit)
initLimit=10

# 請求得到響應的允許心跳數(時間 = tickTime * syncLimit)
syncLimit=5

# 存放快照的目錄
dataDir=/home/zk

# 用戶端連接配接zk的端口号
clientPort=2181

# 最大連接配接數
# maxClientCnxns=60

# 叢集配置
# 伺服器編号1、2、3、4 是用來選leader的
# 3888	在第一次啟動或者leader挂掉後,用這個端口号建立socket連接配接進行通信,選舉leader
# 2888	在有leader後,其它follower通過這個端口号建立socket連接配接,和leader進行通信,後續建立節點等操作是通過這個端口去通信的
server.1=192.168.174.62:2888:3888
server.2=192.168.174.63:2888:3888
server.3=192.168.174.64:2888:3888
server.4=192.168.174.65:2888:3888
           

配置檔案遠端拷貝到其它三個節點

$ scp zoo.cfg [email protected].168.174.63:`pwd`
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

建立myid

$ cd /home/zk
$ vim myid
# myid檔案存端口号,與配置一緻
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

myid所在目錄遠端拷貝到其它三個節點

$ scp -r ./zk/ [email protected].168.174.65:`pwd`
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

修改其它3個節點的myid

12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

修改環境變量

# 打開環境變量配置檔案
$ vim /etc/profile

# 配置檔案中增加
ZOOKEEPER_HOME=/home/apache-zookeeper-3.5.9-bin
JAVA_HOME=/home/jdk1.8.0_291
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
PATH=$PATH:$ZOOKEEPER_HOME/bin:$JAVA_HOME/bin
export ZOOKEEPER_HOME JAVA_HOME CLASS_PATH PATH

# 修改的環境變量立刻生效
$ source /etc/profile
           

開放端口

# 檢查端口,指令
$ firewall-cmd --zone=public --query-port=2181/tcp
$ firewall-cmd --zone=public --query-port=2888/tcp
$ firewall-cmd --zone=public --query-port=3888/tcp

# 開放端口
$ firewall-cmd --zone=public --add-port=2181/tcp --permanent
$ firewall-cmd --zone=public --add-port=2888/tcp --permanent
$ firewall-cmd --zone=public --add-port=3888/tcp --permanent

# 設定生效
$ firewall-cmd --reload
           

啟動

12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令
$ zkServer.sh start-foreground	# 前台啟動
$ zkServer.sh start	# 背景啟動
$ zkServer.sh status	# 狀态
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

用戶端指令

用戶端連接配接ZooKeeper

$ zkCli.sh
$ zkServer.sh -server 192.168.174.62:2181
           

幫助指令

> help
ZooKeeper -server host:port cmd args
	addauth scheme auth
	close 
	config [-c] [-w] [-s]
	connect host:port
	create [-s] [-e] [-c] [-t ttl] path [data] [acl]
	delete [-v version] path
	deleteall path
	delquota [-n|-b] path
	get [-s] [-w] path
	getAcl [-s] path
	history 
	listquota path
	ls [-s] [-w] [-R] path
	ls2 path [watch]
	printwatches on|off
	quit 
	reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
	redo cmdno
	removewatches path [-c|-d|-a] [-l]
	rmr path
	set [-s] [-v version] path data
	setAcl [-s] [-v version] [-R] path acl
	setquota -n|-b val path
	stat [-w] path
	sync path
           

關閉連接配接

> close
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

建立連接配接

> connect 192.168.174.62:2181
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

退出用戶端

> quit
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

檢視曆史指令

> history
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

查詢節點

# ls [-s] [-w] [-R] path

> ls /
> ls -w /
> ls -s /
> ls -R /

# 查詢子節點
> ls /zookeeper/config
> ls -w /zookeeper/config
> ls -s /zookeeper/config
> ls -R /zookeeper/config
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

查詢節點詳情

# stat [-w] path

> ls -s /
> stat /
> stat -w /
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

查詢節點值

# get [-s] [-w] path

> get /zookeeper/config		# 查詢節點值
> get -w /zookeeper/config		# 查詢節點值,同上
> get -s /zookeeper/config		# 查詢節點值、節點詳情
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

節點資訊解釋

[zk: localhost:2181(CONNECTED) 49] get -s /test1
# 節點值
hello test1 u v5 to v6		
# 建立節點,事務id,c建立
# 64位,前32位“0x2” leader紀元;後32位“00000053”,事務序号 
# 在2紀元,建立節點,事務序号53
cZxid = 0x200000053
# 建立時間
ctime = Wed Jul 28 10:30:09 CST 2021
# 修改節點,事務id,m修改
# 在5紀元,修改節點,事務序号12
mZxid = 0x500000012
# 修改時間
mtime = Wed Jul 28 11:34:52 CST 2021
pZxid = 0x200000053
cversion = 0
# 資料版本
dataVersion = 6
aclVersion = 0
ephemeralOwner = 0x0
# 資料長度
dataLength = 22
numChildren = 0
[zk: localhost:2181(CONNECTED) 50] 
           

建立節點

# create [-s] [-e] [-c] [-t ttl] path [data] [acl]

# 建立節點
> create /test	
# 建立節點,給節點指派
> create /test2 "This is test2 node"	
# 建立節點,給節點指派,給節點設定權限
> create /test3 "This is test3 node" world:anyone:cdwra	
           

持久節點 [-c] (預設)

12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

臨時節點 [-e]

12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

序列化節點 [-s]

> create -s /test3/s	# 建立序列化、持久節點,以s開頭
> create -s /test3/		# 建立序列化、持久節點,不指定開頭
> create -s -e /test3/temp	# 建立序列化、臨時節點,以temp開頭
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

修改節點值

# set [-s] [-v version] path data

> set /test1 "hello test1"
> set -v 5 /test1 "hello test1 u v5 to v6"
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

删除節點

# delete [-v version] path
# deleteall path

> delete /test3/s0000000013		# 删除節點,無子節點
> delete -v 0 /test3/s0000000014	# 删除節點,通過版本号,無子節點
> deleteall /test3		# 全部删除,節點、子節點都删除
           
12 Zookeeper介紹、安裝、shell cli 使用,基本概念驗證ZooKeeper簡介安裝、配置、啟動用戶端指令

上一篇《11 Redis開發:spring.data.redis、連接配接、序列化、high/low api》

下一篇 《13 Zookeeper原理知識,paxos、zab、角色功能、API開發基礎》

繼續閱讀