Windows環(huán)境下的安裝
下載與安裝
1.訪問地址: https://zookeeper.apache.org/releases.html#download下載需要的ZK版本,下載到本地后解壓。
2.在解壓目錄下面新建一個空的 data 文件夾和 log 文件夾

配置
將 conf 目錄下的 zoo_sample.cfg 文件,復(fù)制一份,重命名為 zoo.cfg

修改 zoo.cfg 配置文件,將 dataDir=/tmp/zookeeper 修改成 zookeeper 安裝目錄所在的 data 文件夾,再添加一條添加數(shù)據(jù)日志的配置
# 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=D:\Development\apache-zookeeper-3.6.3-bin\data
dataLogDir=D:\Development\apache-zookeeper-3.6.3-bin\log
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
啟動
雙擊bin目錄下的zkServer.cmd啟動即可

zookeeper新版本中有個內(nèi)嵌的管理控制臺是通過jetty啟動,會占用8080 端口

解決方法
1.修改端口
在zoo.cfg中增加admin.serverPort=沒有被占用的端口號
admin.serverPort=8088
2.修改啟動腳本
在啟動腳本中增加"-Dzookeeper.admin.enableServer=false"
"-Dzookeeper.admin.enableServer=false"
Linux環(huán)境下的安裝
下載與安裝
訪問地址: https://zookeeper.apache.org/releases.html#download下載需要的ZK版本
wget https://downloads.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
解壓zookeeper
[root@administrator conf]# tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz
重命名
[root@administrator conf]# mv tar -zxvf apache-zookeeper-3.7.0-bin zookeeper
移動zookeeper位置
[root@administrator conf]# cp zookeeper /usr/local/
配置
修改zoo_sample.cfg文件
[root@administrator conf]# cd /usr/local/zookeeper/conf
[root@administrator conf]# mv zoo_sample.cfg zoo.cfg
修改dataDir目錄,改成真實輸出目錄
# The number of milliseconds of each tick
# 基本事件單元,以毫秒為單位。它用來控制心跳和超時,默認(rèn)情況下最小的會話超時時間為兩個心跳時間
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 用于限制follower跟隨者服務(wù)器必須連接到leader領(lǐng)導(dǎo)者服務(wù)器的時限
# 多少個心跳時間內(nèi),允許其他server連接并初始化數(shù)據(jù),如果ZooKeeper管理的數(shù)據(jù)較大,則應(yīng)相應(yīng)增大這個值
# 它以tickTime的倍數(shù)來表示。當(dāng)超過設(shè)置倍數(shù)的tickTime時間,則連接失敗
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# leader與follower之間發(fā)送消息,請求和應(yīng)答時間長度。
# 如果follower在設(shè)置的時間內(nèi)不能與leader進(jìn)行通信,那么此follower將被丟棄。
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#dataDir=/tmp/zookeeper
#存儲內(nèi)存數(shù)據(jù)庫快照的位置
dataDir=/usr/local/zookeeper/data
# the port at which the clients will connect
# 監(jiān)聽客戶端連接的端口,默認(rèn)是2181
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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
創(chuàng)建data目錄
[root@administrator conf]# cd /usr/local/zookeeper/
[root@administrator zookeeper]# mkdir data
啟動
注意:zookeeper需要依賴jdk環(huán)境
[root@administrator bin]# cd /usr/local/zookeeper/bin
[root@administrator bin]# ./zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@administrator bin]#
查看啟動狀態(tài)
[root@administrator bin]# ./zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
停止
[root@administrator bin]# ./zkServer.sh stop
測試
[root@administrator bin]# ./zkCli.sh -server 127.0.0.1:2181
[zk: 127.0.0.1:2181(CONNECTED) 8] ls
ls [-s] [-w] [-R] path
退出客戶端
[zk: 127.0.0.1:2181(CONNECTED) 0] quit
搭建Zookeeper集群環(huán)境
搭建Zookeeper集群至少需要三臺服務(wù)器,且服務(wù)器之間系統(tǒng)時間保持一致,三臺服務(wù)器中一個leader和兩個follower
下載與安裝
[root@node001 zookeeper]# wget https://downloads.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
解壓并重命名
[root@node001 zookeeper]# tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz
[root@node001 zookeeper]# mv apache-zookeeper-3.7.0-bin zookeeper
配置
創(chuàng)建data目錄
[root@node001 zookeeper]# cd zookeeper
[root@node001 zookeeper]# mkdir data
創(chuàng)建myid文件,填寫每個節(jié)點編號(node001 ==> 1),需唯一。
[root@node001 zookeeper]# cd data
[root@node001 zookeeper]# vim myid
1
配置zoo.cfg
[root@node001 zookeeper]# cd conf/
[root@node001 zookeeper]# mv zoo_sample.cfg zoo.cfg
[root@node001 zookeeper]# vim zoo.cfg
# The number of milliseconds of each tick
# 心跳時間
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 用于限制follower跟隨者服務(wù)器必須連接到leader領(lǐng)導(dǎo)者服務(wù)器的時限
# 多少個心跳時間內(nèi),允許其他server連接并初始化數(shù)據(jù),如果ZooKeeper管理的數(shù)據(jù)較大,則應(yīng)相應(yīng)增大這個值
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# 集群中Leader與Follower之間的最大響應(yīng)時間單位
# 多少個tickTime內(nèi),允許follower同步,如果follower落后太多,則會被丟棄。
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 修改數(shù)據(jù)存儲路徑
# 存儲內(nèi)存數(shù)據(jù)庫快照的位置
dataDir=/usr/local/program/zookeeper/data
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
#3臺服務(wù)器的地址
# server.A=B:C:D
# 添加集群配置
server.1=node001:2888:3888
server.2=node002:2888:3888
server.3=node003:2888:3888
A :一個數(shù)字,表示是第幾號服務(wù)器
B:服務(wù)器的地址
C:服務(wù)器Follower與集群中的Leader服務(wù)器交換信息的端口
D:萬一集群中的Leader服務(wù)器掛了,需要一個端口來重新進(jìn)行選舉一個新的Leader,而這個端口就是用來執(zhí)行選舉時服務(wù)器相互通信的端口。
分發(fā)到其他節(jié)點
scp -r zookeeper node002:/usr/local/program/zookeeper
scp -r zookeeper node003:/usr/local/program/zookeeper
修改分發(fā)Zookeeper機(jī)器的myid編號,分別為2,3
[root@node002 zookeeper]# vim data/myid
2
[root@node003 zookeeper]# vim data/myid
3
啟動集群
[root@node001 zookeeper]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/program/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@node002 zookeeper]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/program/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@node003 zookeeper]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/program/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
查看集群狀態(tài)
[root@node001 zookeeper]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/program/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
[root@node002 zookeeper]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/program/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[root@node003 zookeeper]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/program/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
查看進(jìn)程
[root@node001 hadoop]# jps
18946 QuorumPeerMain
19675 Jps
[root@node002 hadoop]# jps
21860 QuorumPeerMain
22095 Jps
[root@node003 hadoop]# jps
25607 Jps
25547 JournalNode
常用命令
ZK操作
| 命令 | 作用 |
|---|---|
| zkServer.sh start/stop | 啟動/關(guān)閉服務(wù) |
| zkCli.sh start/stop | 啟動/關(guān)閉客戶端 |
| zkServer.sh status | 查看狀態(tài)(leader/follower) |
| bin/zkCli.sh | 連接本機(jī)zk |
| zkCli.sh -server node001:2181 | 連接其他zk服務(wù)器 |
| status /zk | 查看/zk節(jié)點的狀態(tài)信息 |
節(jié)點ZNode的創(chuàng)建
| 命令 | 作用 |
|---|---|
| create /zk mydata | 創(chuàng)建znode節(jié)點,永久+不帶序號 |
| create -e /zk mydata | 創(chuàng)建臨時znode節(jié)點 |
| create -s /zk mydata | 創(chuàng)建順序znode節(jié)點 |
| create -e -s /zk mydata | 創(chuàng)建臨時的順序znode節(jié)點 |
節(jié)點ZNode的查詢
| 命令 | 作用 |
|---|---|
| ls /zk | 查看znode子節(jié)點列表 |
| ls /zk watch | 對一個節(jié)點的子節(jié)點變化事件注冊了監(jiān)聽 |
| get /zk | 獲取znode數(shù)據(jù) |
| get /zk watch | 對一個節(jié)點的數(shù)據(jù)內(nèi)容變化事件注冊了監(jiān)聽 |
節(jié)點ZNode的修改與刪除
| 命令 | 作用 |
|---|---|
| set /zk value | 設(shè)置znode數(shù)據(jù) |
| delete /zk | 只能刪除沒有子znode的znode |
| rmr /zk | 刪除znode的znode,遞歸刪除 |
數(shù)據(jù)信息字段
znode數(shù)據(jù)信息字段的解釋
cZxid = 0x1000fe0597c0002 節(jié)點創(chuàng)建的時候的zxid
# The zxid of the change that caused this znode to be created.
ctime = Fri Dec 02 16:41:50 PST 2022 節(jié)點創(chuàng)建的時間
# The time in milliseconds from epoch when this znode was created.
mZxid = 0x1000fe0597c0002 節(jié)點修改的時候的zxid,與子節(jié)點的修改無關(guān)
# The zxid of the change that last modified this znode.
mtime = Fri Dec 02 16:41:50 PST 2022 節(jié)點的修改的時間
# The time in milliseconds from epoch when this znode was last modified.
pZxid = 0x1000fe0597c0002 和子節(jié)點的創(chuàng)建/刪除對應(yīng)的zxid,和修改無關(guān),和孫子節(jié)點無關(guān)
# The zxid of the change that last modified children of this znode.
cversion = 0 子節(jié)點的更新次數(shù)
# The number of changes to the children of this znode.
dataVersion = 0 節(jié)點數(shù)據(jù)的更新次數(shù)
# The number of changes to the data of this znode.
aclVersion = 0 節(jié)點(ACL)的更新次數(shù)
# The number of changes to the ACL of this znode.
ephemeralOwner = 0x0 如果該節(jié)點為ephemeral節(jié)點, ephemeralOwner值表示與該節(jié)點綁定的
session id. 如果該節(jié)點不是ephemeral節(jié)點, ephemeralOwner值為0
# The session id of the owner of this znode if the znode is an ephemeral node.
If it is not an ephemeral node, it will be zero.
dataLength = 6 節(jié)點數(shù)據(jù)的字節(jié)數(shù)
# The length of the data field of this znode.
numChildren = 0 子節(jié)點個數(shù),不包含孫子節(jié)點
# The number of children of this znode.