簡介
ZooKeeper是一個分布式的,開放原始碼的分布式應用程式協調服務,是Google的Chubby一個開源的實作,是Hadoop和Hbase的重要組件,它是一個為分布式應用提供一致性服務的軟體,提供的功能包括:配置維護、域名服務、分布式同步、組服務等,
Zookeeper資料模型
Zookeeper資料模型的結構和Unix檔案系統很類似,ZooKeeper中的每個節點都稱為znode,znode默認能夠存盤1MB的資料,并且znode的路徑是znode的唯一標識,

Zookeeper中的節點分為以下四種型別:
1.持久節點
- 持久節點被創建后會一直存在,直到主動的洗掉這個節點,
2.持久順序節點
- 持久順序節點具備持久節點的特性,并且持久順序節點在創建時,Zookeeper會自動的在該節點名后添加一個數字后綴,這個數字后綴是有序的,由父節點維護,
3.臨時節點
- 臨時節點類似于持久節點,區別在于在客戶端會話斷開時,該客戶端創建的臨時節點將被自動洗掉,并且臨時節點下不能創建子節點,
4.臨時順序節點
- 臨時順序節點類似于持久順序節點,區別在于在客戶端會話斷開時,該客戶端創建的臨時順序節點將被自動洗掉,并且臨時順序節點下不能創建子節點,
Linux安裝Zookeeper
下載Zookeeper:
cd /opt/
wget https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz
解壓縮:
tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz
創建資料存盤目錄:
mkdir /opt/zkdata
將config目錄下的zoo_sample.cfg重命名為zoo.cfg:
mv zoo_sample.cfg zoo.cfg
修改zoo.cfg中的dataDir為上面創建資料存盤目錄:
dataDir=/opt/zkdata
啟動Zookeeper
使用默認配置zoo.cfg啟動Zookeeper:
. /opt/apache-zookeeper-3.6.3-bin/bin/zkServer.sh start
指定組態檔啟動Zookeeper:
. /opt/apache-zookeeper-3.6.3-bin/bin/zkServer.sh start /opt/apache-zookeeper-3.6.3-bin/conf/zoo.cfg
使用jps查看Zookeeper是否已經啟動:
QuorumPeerMain為Zookeeper行程,
[root@localhost ~]# jps
2201 QuorumPeerMain
2235 Jps
啟動客戶端連接到Zookeeper:
不指定server引數,默認連接到本地的Zookeeper(localhost:2181):
. /opt/apache-zookeeper-3.6.3-bin/bin/zkCli.sh
指定IP地址和埠,連接Zookeeper:
. /opt/apache-zookeeper-3.6.3-bin/bin/zkCli.sh -server localhost:2181
ZooKeeper服務器其他命令
在前臺啟動ZooKeeper服務器:
. /opt/apache-zookeeper-3.6.3-bin/bin/zkServer.sh start-foreground
重啟ZooKeeper服務器:
. /opt/apache-zookeeper-3.6.3-bin/bin/zkServer.sh restart
停止ZooKeeper服務器:
. /opt/apache-zookeeper-3.6.3-bin/bin/zkServer.sh stop
組態檔決議
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/zkdata
# 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
tickTime:Zookeeper與客戶端的心跳間隔時間(單位毫秒),
initLimit:Zookeeper集群中Leader和Follower之間初始連接的最大心跳數量,
syncLimit:運行程序中Zookeeper集群中Leader和Follower之間的最大心跳數量,
dataDir:資料存盤位置,
clientPort:監聽客戶端連接的埠,即客戶端嘗試連接的埠,
maxClientCnxns:最大并發連接數,
autopurge.snapRetainCount:指定dataDir中保留的快照數量,
autopurge.purgeInterval:清除任務的時間間隔(小時),設定為“0”表示禁用自動清除功能,
metricsProvider.className:設定為“org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider”以啟用Prometheus.io匯出器,
metricsProvider.httpPort:Prometheus.io匯出器將啟動Jetty服務器并系結到該埠,默認為7000,
metricsProvider.exportJvmInfo:如果將此屬性設定為true,則Prometheus.io將匯出有關JVM的指標,默認值為true,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/280485.html
標籤:Java
