zookeeper其實是yahoo開發的,用於分佈式中一致性處理的框架。最初其做爲研發Hadoop時的副產品。因爲分佈式系統中一致性處理較爲困難,其餘的分佈式系統沒有必要 費勁重複造輪子,故隨後的分佈式系統中大量應用了zookeeper,以致於zookeeper成爲了各類分佈式系統的基礎組件,其地位之重要,可想而知。著名的hadoop,kafka,dubbo 都是基於zookeeper而構建。html
要想理解zookeeper究竟是作啥的,那首先得理解清楚,什麼是一致性?java
所謂的一致性,實際上就是圍繞着「看見」來的。誰能看見?可否看見?何時看見?舉個例子:淘寶後臺賣家,在後臺上架一件大促的商品,經過服務器A提交到主數據庫,假設剛提交後立馬就有用戶去經過應用服務器B去從數據庫查詢該商品,就會出現一個現象,賣家已經更新成功了,然而買家卻看不到;而通過一段時間後,主數據庫的數據同步到了從數據庫,買家就能查到了。node
假設賣家更新成功以後買家立馬就能看到賣家的更新,則稱爲強一致性linux
若是賣家更新成功後買家不能看到賣家更新的內容,則稱爲弱一致性數據庫
而賣家更新成功後,買家通過一段時間最終能看到賣家的更新,則稱爲最終一致性apache
《一致性協議》segmentfault
《ZooKeeper應用場景》centos
《分佈式架構》api
VMware版本號:12.0.0
CentOS版本:CentOS 7.3.1611
ZooKeeper版本:ZooKeeper-3.4.9.tar.gz
虛擬機IP:192.168.252.101,192.168.252.102,192.168.252.103
集羣主機名稱:node1,node2,node3 具體參考《CentOs7.3 修改主機名》
集羣主機用戶:都是用root用戶
集羣JDK環境:jdk-8u144-linux-x64.tar.gz JDK 1.8 安裝 具體參考《CentOs7.3 安裝 JDK1.8》
集羣主機之間設置免密登錄:設置方式見:《CentOs7.3 ssh 免密登陸》
關閉防火牆
centos 6.x 關閉 iptables
$ service iptables stop # 關閉命令:
centos 7.x 關閉firewall
$ systemctl stop firewalld.service # 中止firewall
下載最新版本的ZooKeeper ,我在北京我就選擇,清華鏡像比較快
清華鏡像:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/
阿里鏡像:https://mirrors.aliyun.com/apache/zookeeper/
$ cd /opt/ $ wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
或者在瀏覽器下載上傳至opt 目錄
$ cd /opt/ $ tar -zxf zookeeper-3.4.9.tar.gz $ cd zookeeper-3.4.9
建立data
文件夾 用於存儲數據文件
$ mkdir /opt/zookeeper-3.4.9/data
建立logs
文件夾 用於存儲日誌
$ mkdir /opt/zookeeper-3.4.9/logs
zoo.cfg
zookeeper的主要配置文件,由於Zookeeper是一個集羣服務,集羣的每一個節點都須要這個配置文件。爲了不出差錯,zoo.cfg這個配置文件裏沒有跟特定節點相關的配置,因此每一個節點上的這個zoo.cfg都是如出一轍的配置。這樣就很是便於管理了,好比咱們能夠把這個文件提交到版本控制裏管理起來。其實這給咱們設計集羣系統的時候也是個提示:集羣系統通常有不少配置,應該儘可能將通用的配置和特定每一個服務的配置(好比服務標識)分離,這樣通用的配置在不一樣服務之間copy就ok了
$ vi /opt/zookeeper-3.4.9/conf/zoo.cfg
編輯內容以下
tickTime = 2000 dataDir = /opt/zookeeper-3.4.9/data dataLogDir = /opt/zookeeper-3.4.9/logs tickTime = 2000 clientPort = 2181 initLimit = 5 syncLimit = 2 server.1=node1:2888:3888 server.2=node2:2888:3888 server.3=node3:2888:3888
tickTime
dataDir
dataLogDir
initLimit
當follower最初與leader創建鏈接時,它們之間會傳輸至關多的數據,尤爲是follower的數據落後leader不少。initLimit配置follower與leader之間創建鏈接後進行同步的最長時間。
syncLimit
server.id=host:port1:port2
server.id
其中id爲一個數字,表示zk進程的id,這個id也是data目錄下myid文件的內容
host
是該zk進程所在的IP地址
port1
表示follower和leader交換消息所使用的端口
port2
表示選舉leader所使用的端口
在data裏會放置一個myid文件,裏面就一個數字,用來惟一標識這個服務。這個id是很重要的,必定要保證整個集羣中惟一
ZooKeeper會根據這個id來取出server.x上的配置。好比當前id爲1,則對應着zoo.cfg裏的server.1的配置
$ echo "1" > /opt/zookeeper-3.4.9/data/myid
這樣一臺node1機器就配置完了
在集羣node1 上執行,複製配置好的zookeeper到其餘兩臺主機上
$ for a in {2..3} ; do scp -r /opt/zookeeper-3.4.9/ node$a:/opt/zookeeper-3.4.9 ; done
在集羣node1 上執行 ,批量修改myid 文件
$ for a in {1..3} ; do ssh node$a "source /etc/profile; echo $a > /opt/zookeeper-3.4.9/data/myid" ; done
在集羣任意一臺機器上執行
$ for a in {1..3} ; do ssh node$a "source /etc/profile; /opt/zookeeper-3.4.9/bin/zkServer.sh start" ; done
響應
ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Starting zookeeper ... STARTED ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Starting zookeeper ... STARTED ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
$ /opt/zookeeper-3.4.9/bin/zkCli.sh -server node1:2181,node2:2181,node3:2181
響應
Connecting to node1:2181,node2:2181,node3:2181 2017-08-23 11:08:10,323 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT 2017-08-23 11:08:10,328 [myid:] - INFO [main:Environment@100] - Client environment:host.name=node1 2017-08-23 11:08:10,329 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_144 2017-08-23 11:08:10,331 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2017-08-23 11:08:10,331 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/jre 2017-08-23 11:08:10,331 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/opt/zookeeper-3.4.9/bin/../build/classes:/opt/zookeeper-3.4.9/bin/../build/lib/*.jar:/opt/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/opt/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/opt/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/opt/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/opt/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/opt/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/opt/zookeeper-3.4.9/bin/../conf:.:/lib/jvm/lib:/lib/jvm/jre/lib 2017-08-23 11:08:10,331 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2017-08-23 11:08:10,331 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 2017-08-23 11:08:10,331 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA> 2017-08-23 11:08:10,331 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 2017-08-23 11:08:10,331 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64 2017-08-23 11:08:10,331 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-514.26.2.el7.x86_64 2017-08-23 11:08:10,331 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root 2017-08-23 11:08:10,332 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root 2017-08-23 11:08:10,332 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/root 2017-08-23 11:08:10,333 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=node1:2181,node2:2181,node3:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@506c589e 2017-08-23 11:08:10,361 [myid:] - INFO [main-SendThread(node3:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server node3/192.168.252.123:2181. Will not attempt to authenticate using SASL (unknown error) Welcome to ZooKeeper! JLine support is enabled 2017-08-23 11:08:10,474 [myid:] - INFO [main-SendThread(node3:2181):ClientCnxn$SendThread@876] - Socket connection established to node3/192.168.252.123:2181, initiating session [zk: node1:2181,node2:2181,node3:2181(CONNECTING) 0] 2017-08-23 11:08:10,535 [myid:] - INFO [main-SendThread(node3:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server node3/192.168.252.123:2181, sessionid = 0x35e0d0716340000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: node1:2181,node2:2181,node3:2181(CONNECTED) 0]
從日誌能夠看出客戶端成功鏈接的是node3 鏈接上哪臺機器的zk進程是隨機的
2017-08-23 11:08:10,361 [myid:] - INFO [main-SendThread(node3:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server node3/192.168.252.123:2181. Will not attempt to authenticate using SASL (unknown error) Welcome to ZooKeeper! JLine support is enabled
$ for a in {1..3} ; do ssh node$a "source /etc/profile; /opt/zookeeper-3.4.9/bin/zkServer.sh status" ; done
響應
ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Mode: follower ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Mode: leader ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Mode: follower
經過日誌我能夠看到 node2 leader (ps 是老大),其餘 node1 ,node2 follower (ps 都是小弟)
Leader 怎麼選舉的能夠參考《Zookeeper的Leader選舉》
$ for a in {1..3} ; do ssh node$a "source /etc/profile; /opt/zookeeper-3.4.9/bin/zkServer.sh stop" ; done
響應
ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED ZooKeeper JMX enabled by default Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED