当前位置: 首页>大数据>正文

4. ZooKeeper

2 ZooKeeper

2.1 ZooKeeper浣跨敤鍦烘櫙

ZooKeeper鏄竴涓垎甯冨紡鏈嶅姟妗嗘灦, 瀹冧富瑕佹槸鐢ㄦ潵瑙e喅鍒嗗竷寮忓簲鐢ㄤ腑缁忓父閬囧埌鐨勪竴浜涙暟鎹鐞嗛棶棰? 濡? 鍛藉悕鏈嶅姟, 鐘舵€佸悓姝? 閰嶇疆涓績, 闆嗙兢绠$悊绛?/p>

  • 鍛藉悕鏈嶅姟: 鍛戒护鏈嶅姟鏄垎甯冨紡绯荤粺涓瘮杈冨父瑙佺殑涓€绫诲満鏅? 鍛藉悕鏈嶅姟鏄垎甯冨紡绯荤粺鏈€鍩烘湰鐨勫叕鍏辨湇鍔′箣涓€. 鍦ㄥ垎甯冨紡绯荤粺涓? 琚懡鍚嶇殑瀹炰綋閫氬父鍙互鏄泦缇や腑鐨勬満鍣? 鎻愪緵鐨勬湇鍔″湴鍧€鎴栬繙绋嬪璞$瓑. 杩欎簺鎴戜滑閮藉彲浠ョ粺绉颁负鍚嶅瓧, 鍏朵腑杈冧负甯歌鐨勫氨鏄竴浜涘垎甯冨紡鏈嶅姟妗嗘灦(濡?RPC, RMI)涓殑鏈嶅姟鍦板潃鍒楄〃, 閫氳繃浣跨敤鍛藉悕鏈嶅姟, 瀹㈡埛绔簲鐢ㄨ兘澶熸牴鎹寚瀹氬悕瀛楁潵鑾峰彇璧勬簮鐨勫疄浣? 鏈嶅姟鍦板潃鍜屾彁渚涜€呯殑淇℃伅绛?
4. ZooKeeper,第1张
  • 鐘舵€佸悓姝? 瑙e喅寰湇鍔″満鏅笅, 鏈嶅姟鍔ㄦ€佹墿瀹规垨鑰呯缉瀹规椂, 鏈嶅姟鍔ㄦ€佸彂鐜扮殑闂. 涓嶅悓鐨勬湇鍔′細灏嗚嚜宸辨敞鍐屽埌ZooKeeper, 娑堣垂鑰呴€氳繃杩炴帴ZooKeeper鍘昏幏寰楁湇鍔$殑鍦板潃. 鏃犺鏄井鏈嶅姟鎵╁杩樻槸鏈嶅姟瀹曟満, ZooKeeper閮戒細鍚屾缁欐秷璐硅€? 淇濊瘉娑堣垂鑰呰繛鎺ョ殑閮芥槸鍙敤鐨勬渶鏂扮殑鏈嶅姟鍦板潃

    姣忎釜鑺傜偣闄や簡瀛樺偍鏁版嵁鍐呭鍜宯ode鑺傜偣鐘舵€佷俊鎭箣澶? 杩樺瓨鍌ㄤ簡宸茬粡娉ㄥ唽鐨凙PP鐘舵€佷俊鎭? 褰撴湁浜涜妭鐐规垨APP涓嶅彲鐢? 灏卞皢褰撳墠鐘舵€佸悓姝ョ粰鍏朵粬鏈嶅姟.

  • 閰嶇疆涓績: 鐜板湪鎴戜滑澶у鏁板簲鐢ㄩ兘鏄噰鐢ㄧ殑鏄垎甯冨紡寮€鍙戠殑搴旂敤, 鎼缓鍒颁笉鍚岀殑鏈嶅姟鍣ㄤ笂, 瀵逛簬閰嶇疆鏂囦欢, 鍚屼竴涓簲鐢ㄧ▼搴忕殑閰嶇疆鏂囦欢涓€鏍? 杩樻湁灏辨槸澶氫釜绋嬪簭瀛樺湪鐩稿悓鐨勯厤缃? 褰撴垜浠厤缃枃浠朵腑鏈変釜閰嶇疆灞炴€ч渶瑕佹敼鍙? 鎴戜滑闇€瑕佹敼鍙樻瘡涓▼搴忕殑閰嶇疆灞炴€? 杩欐牱浼氬緢楹荤儲鐨勫幓淇敼閰嶇疆, 閭d箞鍙互浣跨敤ZooKeeper鏉ュ疄鐜伴厤缃腑蹇? ZooKeeper閲囩敤鐨勬槸鎺ㄦ媺鐩哥粨鍚堢殑鏂瑰紡: 瀹㈡埛绔悜鏈嶅姟绔敞鍐岃嚜宸遍渶瑕佸叧娉ㄧ殑鑺傜偣, 涓€鏃﹁鑺傜偣鐨勬暟鎹彂鐢熷彉鏇? 閭d箞鏈嶅姟绔氨浼氬悜鐩稿簲鐨勫鎴风鍙戦€乄atcher浜嬩欢閫氱煡, 瀹㈡埛绔帴鏀跺埌杩欎釜娑堟伅閫氱煡鍚? 闇€瑕佷富鍔ㄥ埌鏈嶅姟绔幏鍙栨渶鏂扮殑鏁版嵁

  • 闆嗙兢绠$悊: 鎵€璋撶殑闆嗙兢绠$悊, 鍖呮嫭闆嗙兢鐩戞帶涓庨泦缇ゆ帶鍒朵袱澶у潡, 鍓嶈€呬晶閲嶅闆嗙兢杩愯鏃剁姸鎬佺殑鏀堕泦, 鍚庤€呭垯鏄闆嗙兢杩涜鎿嶄綔鍜屾帶鍒? 鍦ㄦ棩甯稿紑鍙戝拰杩愮淮杩囩▼涓? 鎴戜滑缁忓父浼氭湁绫讳技浜庡涓嬬殑闇€姹?

甯屾湜鐭ラ亾褰撳墠闆嗙兢涓┒绔熸湁澶氬皯鏈哄櫒鍦ㄥ伐浣?
瀵归泦缇や腑姣忓彴鏈哄櫒鐨勮繍琛屾椂鐘舵€佽繘琛屾暟鎹敹闆?
瀵归泦缇や腑鏈哄櫒杩涜涓婁笅绾挎搷浣?

ZooKeeper鍏锋湁浠ヤ笅涓ゅぇ鐗规€?/p>

  • 瀹㈡埛绔鏋滃ZooKeeper鐨勪竴涓暟鎹妭鐐规敞鍐學atcher鐩戝惉, 閭d箞褰撹鏁版嵁鑺傜偣鐨勫唴瀹规垨鏄叾瀛愯妭鐐瑰垪琛ㄥ彂鐢熷彉鏇存椂, ZooKeeper鏈嶅姟鍣ㄥ氨浼氬悜璁㈤槄鐨勫鎴风鍙戦€佸彉鏇撮€氱煡. 瀵瑰湪ZooKeeper涓婂垱寤虹殑涓存椂鑺傜偣, 涓€鏃﹀鎴风涓庢湇鍔″櫒涔嬮棿鐨勪細璇濆け鏁? 閭d箞璇ヤ复鏃惰妭鐐逛篃灏变細琚嚜鍔ㄦ竻闄?/li>
  • Watcher(浜嬩欢鐩戝惉鍣?, 鏄痁ooKeeper涓殑涓€涓緢閲嶈鐨勭壒鎬? ZooKeeper鍏佽鐢ㄦ埛鍦ㄦ寚瀹氳妭鐐逛笂娉ㄥ唽涓€浜沇atcher,. 骞朵笖鍦ㄤ竴浜涚壒瀹氫簨浠惰Е鍙戠殑鏃跺€? ZooKeeper鏈嶅姟绔細灏嗕簨浠堕€氱煡鍒版劅鍏磋叮鐨勫鎴风涓婂幓, 璇ユ満鍒舵槸ZooKeeper瀹炵幇鍒嗗竷寮忓崗璋冩湇鍔$殑閲嶈鐗规€?/li>
4. ZooKeeper,第2张
0 鐢熶骇鑰呭惎鍔?
1 鐢熶骇鑰呮敞鍐岃嚦ZooKeeper
2 娑堣垂鑰呭惎鍔ㄥ苟璁㈤槄棰戦亾
3 ZooKeeper閫氱煡娑堣垂鑰呬簨浠?
4 娑堣垂鑰呰皟鐢ㄧ敓浜ц€?
5 鐩戞帶涓績璐熻矗缁熻鍜岀洃鎺ф湇鍔$姸鎬?

2.2 ZooKeeper鍗曟満瀹夎

鍗曟満鐗堢殑ZooKeeper瀹夎

10.0.0.209

2.2.1 閰嶇疆Java鐜

瀹樻柟渚濊禆浠嬬粛: https://zookeeper.apache.org/doc/r3.4.14/zookeeperAdmin.html#sc_requiredSoftware

root@zookeeper:~# apt install openjdk-8-jdk -y

2.2.2 閮ㄧ讲ZooKeeper-3.5.8

涓嬭浇浜岃繘鍒跺寘: https://archive.apache.org/dist/zookeeper/zookeeper-3.5.8/

root@zookeeper:~# mkdir /apps
root@zookeeper:~# cd /apps
root@zookeeper:/apps# rz -E
rz waiting to receive.
root@zookeeper:/apps# ls
apache-zookeeper-3.5.8-bin.tar.gz
root@zookeeper:/apps# tar xvf apache-zookeeper-3.5.8-bin.tar.gz
root@zookeeper:/apps# cd apache-zookeeper-3.5.8-bin/bin
root@zookeeper:/apps/apache-zookeeper-3.5.8-bin/bin# ll
total 64
drwxr-xr-x 2 root root 4096 May  4  2020 ./
drwxr-xr-x 6 root root 4096 Dec 15 15:54 ../
-rwxr-xr-x 1 root root  232 May  4  2020 README.txt*
-rwxr-xr-x 1 root root 2067 May  4  2020 zkCleanup.sh*
-rwxr-xr-x 1 root root 1158 May  4  2020 zkCli.cmd*
-rwxr-xr-x 1 root root 1621 May  4  2020 zkCli.sh* #Linux瀹㈡埛绔剼鏈? 鍙互杩炴帴鍒癦ooKeeper鏈嶅姟鍣ㄦ墽琛屾搷浣?
-rwxr-xr-x 1 root root 1766 May  4  2020 zkEnv.cmd*
-rwxr-xr-x 1 root root 3690 May  4  2020 zkEnv.sh*
-rwxr-xr-x 1 root root 1286 May  4  2020 zkServer.cmd*
-rwxr-xr-x 1 root root 4573 May  4  2020 zkServer-initialize.sh*
-rwxr-xr-x 1 root root 9386 May  4  2020 zkServer.sh* #Linux鏈嶅姟鍚姩鑴氭湰
-rwxr-xr-x 1 root root  996 May  4  2020 zkTxnLogToolkit.cmd*
-rwxr-xr-x 1 root root 1385 May  4  2020 zkTxnLogToolkit.sh*

2.2.3 缂栬緫閰嶇疆鏂囦欢

root@zookeeper:/apps/apache-zookeeper-3.5.8-bin/conf# ll
total 20
drwxr-xr-x 2 root root 4096 May  4  2020 ./
drwxr-xr-x 6 root root 4096 Dec 15 15:54 ../
-rw-r--r-- 1 root root  535 May  4  2020 configuration.xsl
-rw-r--r-- 1 root root 2712 May  4  2020 log4j.properties
-rw-r--r-- 1 root root  922 May  4  2020 zoo_sample.cfg  #妯℃澘閰嶇疆鏂囦欢
root@zookeeper:/apps/apache-zookeeper-3.5.8-bin/conf# cp zoo_sample.cfg zoo.cfg
root@zookeeper:/apps/apache-zookeeper-3.5.8-bin/conf# vim 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=/tmp/zookeeper #ZooKeeper鏁版嵁鐩綍鐨勪繚瀛樹綅缃?
dataDir=/apps/apache-zookeeper-3.5.8-bin/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

2.2.4 鍚姩ZooKeeper

root@zookeeper:/apps/apache-zookeeper-3.5.8-bin/bin# ./zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /apps/apache-zookeeper-3.5.8-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

2.2.5 楠岃瘉ZooKeeper杩涚▼

root@zookeeper:/apps/apache-zookeeper-3.5.8-bin/bin# ./zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /apps/apache-zookeeper-3.5.8-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone #鍗曟満妯″紡
root@zookeeper:/apps/apache-zookeeper-3.5.8-bin/bin# ss -ntl
State             Recv-Q              Send-Q                            Local Address:Port                            Peer Address:Port             
LISTEN            0                   128                               127.0.0.53%lo:53                                   0.0.0.0:*                
LISTEN            0                   128                                     0.0.0.0:22                                   0.0.0.0:*                
LISTEN            0                   50                                            *:8080                                       *:*                
LISTEN            0                   128                                        [::]:22                                      [::]:*                
LISTEN            0                   50                                            *:46813                                      *:*                
LISTEN            0                   50                                            *:2181    #ZooKeeper鐩戝惉2181绔彛                                   *:* 

2.2.6 娴嬭瘯鏁版嵁鍐欏叆

#!/usr/bin/env python

def myzk():
    from kazoo.client import KazooClient
    zk = KazooClient(hosts='10.0.0.209:2181')
    zk.start()
    zk.create('/zoo/data',makepath=True) #makepath=True 閫掑綊鍒涘缓鐩綍
    ret=zk.set('/zoo/data',"Hello World")
    data=zk.get("/zoo/data")
    print(data)
    zk.stop()
myzk()

2.2.7 鍒╃敤瀹㈡埛绔剼鏈? 鎿嶄綔ZooKeeper

root@zookeeper:/apps/apache-zookeeper-3.5.8-bin/bin# ./zkCli.sh
...
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper] #zookeeper鐩稿綋浜?涓嬬殑涓€涓洰褰?
[zk: localhost:2181(CONNECTED) 1] ls /zookeeper
[config, quota] #config, quota鐩稿綋浜巣ookeeper鐩綍閲岀殑key
[zk: localhost:2181(CONNECTED) 6] get /zookeeper/quota #get鍛戒护鏌ョ湅涓€涓猭ey鐨勫€? 杩欓噷quota榛樿娌℃湁value

杩樺師铏氭嫙鏈哄噯澶囬泦缇ゆ惌寤?/p>

2.3 ZooKeeper闆嗙兢

4. ZooKeeper,第3张
鍥剧墖.png

4. ZooKeeper,第4张
鍥剧墖.png

4. ZooKeeper,第5张
鍥剧墖.png
4. ZooKeeper,第6张
鍥剧墖.png

ZooKeeper闆嗙兢鐢ㄤ簬瑙e喅鍗曠偣鍜屽崟鏈烘€ц兘鍙婃暟鎹珮鍙敤绛夐棶棰?/p>

ZooKeeper鏄鍒堕泦缇? 鏁版嵁鍦ㄦ瘡涓妭鐐归兘鏄浉鍚岀殑

閫氳繃閫変妇, 閫夊嚭涓€涓敮涓€鐨刲eader, 鏁版嵁鐨勫啓鍏ュ彧閫氳繃leader鍐欏叆, 鍥犱负濡傛灉閫氳繃鍏朵粬鑺傜偣鍐欏叆, 鏃犳硶淇濊瘉鏁版嵁涓€鑷存€?/p>

瀹㈡埛绔繛鎺ュ埌follower鍚庡啓鍏ユ暟鎹? follower浼氭妸鏁版嵁璺敱缁檒eader, 鐢眑eader瀹屾垚鏁版嵁鍐欏叆, leader瀹屾垚鍐欏叆鍚? 浼氭妸鏁版嵁鍚屾鍒板叾浠杅ollower鑺傜偣. 娑堣垂鑰呭彲浠ヨ繛鎺ュ埌浠绘剰鑺傜偣鍘绘秷璐规暟鎹? 濡傛灉leader瀹曟満, 鍓╀綑鑺傜偣浼氬啀娆¢€変妇鍑轰竴涓猯eader

ZooKeeper鐗规€?:

鍐欐搷浣滅殑"杩囧崐鍐欐垚鍔熺瓥鐣?: 鐢ㄦ潵鍒ゆ柇鏁版嵁浣曟椂琚涓哄啓鍏ュ畬姣?/p>

涓句緥. 褰撴湁浜斾釜鑺傜偣, 鍏朵腑涓€涓狶eader鑺傜偣, 鍥涗釜Follower鑺傜偣鏃?/p>

瀹㈡埛绔彂璧锋暟鎹啓鍏ユ搷浣? 浼氱敱杩炴帴鐨刦ollower鎶婃暟鎹矾鐢辩粰leader, 鐢眑eader瀹屾垚鏁版嵁鍐欏叆, 鍐嶇敱leader鍚屾缁欏叾浠栫殑follower鑺傜偣. 杩囧崐鍐欐垚鍔熺瓥鐣ュ氨瑙勫畾浜? 闆嗙兢涓? 鏈夊崐鏁颁互涓婂畬鎴愪簡鏁版嵁鍐欏叆, 灏辫涓烘暟鎹啓鍏ュ畬姣? 杩斿洖缁欏鎴风鏁版嵁鍐欏叆瀹屾瘯, 涔嬪悗鍐嶇敱leader鎶婃暟鎹悓姝ュ埌鍏朵綑鏈畬鎴愭暟鎹啓鍏ョ殑鑺傜偣, 鏈夊姪浜庡啓鍏ユ€ц兘鎻愪緵

2.4 闆嗙兢閮ㄧ讲

zookeeper 闆嗙兢鐗规€э細鏁翠釜闆嗙兢涓彧瑕佹湁瓒呰繃闆嗙兢鏁伴噺涓€鍗婄殑 zookeeper 宸ヤ綔鏄甯哥殑锛岄偅涔堟暣涓泦缇ゅ澶栧氨鏄彲鐢ㄧ殑锛屽亣濡傛湁 2 鍙版湇鍔″櫒鍋氫簡涓€涓?zookeeper闆嗙兢锛屽彧瑕佹湁浠讳綍涓€鍙版晠闅滄垨瀹曟満锛岄偅涔堣繖涓獄ookeeper 闆嗙兢灏变笉鍙敤浜嗭紝鍥犱负鍓╀笅鐨勪竴鍙版病鏈夎秴杩囬泦缇や竴鍗婄殑鏁伴噺锛屼絾鏄亣濡傛湁涓夊彴 zookeeper 缁勬垚涓€涓泦缇わ紝閭d箞鎹熷潖涓€鍙板氨杩樺墿涓ゅ彴锛屽ぇ浜?3 鍙扮殑涓€鍗婏紝鎵€浠ユ崯鍧忎竴鍙拌繕鏄彲浠ユ甯歌繍琛岀殑锛屼絾鏄啀鎹熷潖涓€鍙板氨鍙墿涓€鍙伴泦缇ゅ氨涓嶅彲鐢ㄤ簡銆傞偅涔堣鏄?4 鍙扮粍鎴愪竴涓獄ookeeper 闆嗙兢锛屾崯鍧忎竴鍙伴泦缇よ偗瀹氭槸姝e父鐨勶紝閭d箞鎹熷潖涓ゅ彴灏辫繕鍓╀袱鍙帮紝閭d箞2 鍙颁笉澶т簬闆嗙兢鏁伴噺鐨勪竴鍗婏紝鎵€浠?3 鍙扮殑 zookeeper 闆嗙兢鍜?4 鍙扮殑 zookeeper 闆嗙兢鎹熷潖涓ゅ彴鐨勭粨鏋滈兘鏄泦缇や笉鍙敤锛屼互姝ょ被鎺?5 鍙板拰 6 鍙颁互鍙?7 鍙板拰 8 鍙伴兘鏄悓鐞嗭紝鎵€浠ヨ繖涔熷氨鏄负浠€涔堥泦缇や竴鑸兘鏄鏁扮殑鍘熷洜銆?/p>

闆嗙兢鐜

Zoo1 - 10.0.0.209
Zoo2 - 10.0.0.199
Zoo3 - 10.0.0.189
  1. 涓夊彴ZooKeeper瀹夎jdk
apt install openjdk-8-jdk -y
  1. 灏哯ooKeeper浜岃繘鍒跺寘涓婁紶鍒?apps骞惰В鍘?/li>
mkdir /apps
cd /apps
tar xvf apache-zookeeper-3.5.8-bin.tar.gz
  1. 鍒涘缓杞繛鎺?/li>
ln -sv apache-zookeeper-3.5.8-bin/ /apps/zookeeper
  1. 鍒涘缓ZooKeeper鏁版嵁鐩綍
mkdir /apps/zookeeper/data
  1. 閰嶇疆鏂囦欢淇敼

鎷疯礉妯℃澘鏂囦欢

cp /apps/zookeeper/conf/zoo_sample.cfg /apps/zookeeper/conf/zoo.cfg

涓変釜鏈嶅姟鍣ㄧ殑閰嶇疆鏂囦欢鐩稿悓

vim /apps/zookeeper/conf/zoo.cfg
...
dataDir=/apps/zookeeper/data
...
maxClientCnxns=128 #鍗曚釜瀹㈡埛绔疘P鍙互鍜孼ooKeeper淇濇寔鐨勮繛鎺ユ暟
autopurge.snapRetainCount=16
autopurge.purgeInterval=6
server.1=10.0.0.209:2888:3888
server.2=10.0.0.199:2888:3888
server.3=10.0.0.189:2888:3888
tickTime=2000 #鏈嶅姟鍣ㄤ笌鏈嶅姟鍣ㄤ箣闂寸殑鍗曟蹇冭烦妫€娴嬫椂闂撮棿闅旓紝鍗曚綅涓烘绉?
initLimit=10 #闆嗙兢涓?leader 鏈嶅姟鍣ㄤ笌 follower 鏈嶅姟鍣ㄥ垵濮嬭繛鎺ュ績璺虫鏁帮紝鍗冲灏戜釜 2000 姣
syncLimit=5 # leader 涓?follower 涔嬮棿杩炴帴瀹屾垚涔嬪悗锛屽悗鏈熸娴嬪彂閫佸拰搴旂瓟鐨勫績璺虫鏁帮紝濡傛灉璇?follower 鍦ㄨ缃殑鏃堕棿鍐?5*2000)涓嶈兘涓?leader 杩涜閫氫俊锛岄偅涔堟 follower 灏嗚瑙嗕负涓嶅彲鐢ㄣ€?
dataDir=/usr/local/zookeeper/data #鑷畾涔夌殑 zookeeper 淇濆瓨鏁版嵁鐨勭洰褰?
clientPort=2181 #瀹㈡埛绔繛鎺?Zookeeper 鏈嶅姟鍣ㄧ殑绔彛锛孼ookeeper 浼氱洃鍚繖涓鍙o紝鎺ュ彈瀹㈡埛绔殑璁块棶璇锋眰
maxClientCnxns=128 #鍗曚釜瀹㈡埛绔?IP 鍙互鍜?zookeeper 淇濇寔鐨勮繛鎺ユ暟
autopurge.snapRetainCount=3 #3.4.0 涓殑鏂板鍔熻兘锛氬惎鐢ㄥ悗锛孼ooKeeper 鑷姩娓呴櫎鍔熻兘浼氬皢autopurge.snapRetainCount 鏈€鏂板揩鐓у拰鐩稿簲鐨勪簨鍔℃棩蹇楀垎鍒繚鐣欏湪 dataDir 鍜?dataLogDir 涓紝骞跺垹闄ゅ叾浣欓儴鍒嗭紝榛樿鍊间负 3銆傛渶灏忓€间负 3銆傚氨鏄繚瀛樺揩鐓х殑鏁伴噺.
autopurge.purgeInterval=1 # 3.4.0 鍙婁箣鍚庣増鏈紝ZK 鎻愪緵浜嗚嚜鍔ㄦ竻鐞嗘棩蹇楀拰蹇収鏂囦欢鐨勫姛鑳斤紝杩欎釜鍙傛暟鎸囧畾浜嗘竻鐞嗛鐜囷紝鍗曚綅鏄皬鏃讹紝闇€瑕侀厤缃竴涓?1 鎴栨洿澶х殑鏁存暟锛岄粯璁ゆ槸 0锛岃〃绀轰笉寮€鍚嚜鍔ㄦ竻鐞嗗姛鑳?
server.1=172.18.0.101:2888:3888 # server.鏈嶅姟鍣ㄧ紪鍙?鏈嶅姟鍣?IP:LF 鏁版嵁鍚屾绔彛:LF 閫変妇绔彛
server.2=172.18.0.102:2888:3888 # leader鐩戝惉2888
server.3=172.18.0.103:2888:3888 # leader鍜宖ollower閮戒細鐩戝惉3888

涓変釜鑺傜偣鍒嗗埆鐢熸垚鑷繁鐨刴yid鏂囦欢

root@zoo1:~# echo "1" > /apps/zookeeper/data/myid
root@zoo2:~# echo "2" > /apps/zookeeper/data/myid
root@zoo3:~# echo "3" > /apps/zookeeper/data/myid
  1. 鍒嗗埆鍚姩ZooKeeper
/apps/zookeeper/bin/zkServer.sh start
  1. 鏌ョ湅闆嗙兢鐘舵€?/li>

姝ゆ椂鍙互瑙傚療鍒?0.0.0.189琚€変妇涓簂eader

root@Zoo1:/apps/zookeeper/data#  /apps/zookeeper/bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /apps/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

root@Zoo2:/apps/zookeeper/data#  /apps/zookeeper/bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /apps/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

root@Zoo3:/apps/zookeeper/data#  /apps/zookeeper/bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /apps/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader

root@Zoo1:~# ss -ntl
State                Recv-Q                Send-Q                                      Local Address:Port                                  Peer Address:Port                
LISTEN               0                     128                                         127.0.0.53%lo:53                                         0.0.0.0:*                   
LISTEN               0                     128                                               0.0.0.0:22                                         0.0.0.0:*                   
LISTEN               0                     128                                                  [::]:22                                            [::]:*                   
LISTEN               0                     50                                                      *:2181                                             *:*                   
LISTEN               0                     50                                                      *:37935                                            *:*                   
LISTEN               0                     50                                    [::ffff:10.0.0.209]:3888                                             *:*                   
LISTEN               0                     50                                                      *:8080                                             *:*  
root@Zoo2:~# ss -ntl
State                Recv-Q                Send-Q                                      Local Address:Port                                  Peer Address:Port                
LISTEN               0                     128                                         127.0.0.53%lo:53                                         0.0.0.0:*                   
LISTEN               0                     128                                               0.0.0.0:22                                         0.0.0.0:*                   
LISTEN               0                     50                                                      *:2181                                             *:*                   
LISTEN               0                     50                                                      *:35531                                            *:*                   
LISTEN               0                     50                                    [::ffff:10.0.0.199]:3888                                             *:*                   
LISTEN               0                     50                                                      *:8080                                             *:*                   
LISTEN               0                     128                                                  [::]:22                                            [::]:*     
root@Zoo3:~# ss -ntl
State                Recv-Q                Send-Q                                      Local Address:Port                                  Peer Address:Port                
LISTEN               0                     128                                         127.0.0.53%lo:53                                         0.0.0.0:*                   
LISTEN               0                     128                                               0.0.0.0:22                                         0.0.0.0:*                   
LISTEN               0                     128                                                  [::]:22                                            [::]:*                   
LISTEN               0                     50                                                      *:2181                                             *:*                   
LISTEN               0                     50                                    [::ffff:10.0.0.189]:2888                                             *:*                   
LISTEN               0                     50                                                      *:35053                                            *:*                   
LISTEN               0                     50                                    [::ffff:10.0.0.189]:3888                                             *:*                   
LISTEN               0                     50                                                      *:8080                                             *:* 
  1. 閫変妇杩囩▼
  • 鑺傜偣瑙掕壊鐘舵€?/li>
LOOKING锛氬鎵?Leader 鐘舵€侊紝澶勪簬璇ョ姸鎬侀渶瑕佽繘鍏ラ€変妇娴佺▼
LEADING锛氶瀵艰€呯姸鎬侊紝澶勪簬璇ョ姸鎬佺殑鑺傜偣璇存槑鏄鑹插凡缁忔槸 Leader
FOLLOWING锛氳窡闅忚€呯姸鎬侊紝琛ㄧず Leader 宸茬粡閫変妇鍑烘潵锛屽綋鍓嶈妭鐐硅鑹叉槸follower
OBSERVER锛氳瀵熻€呯姸鎬侊紝琛ㄦ槑褰撳墠鑺傜偣瑙掕壊鏄?observer
  • 閫変妇id
ZXID锛坺ookeeper transaction id锛夛細姣忎釜鏀瑰彉 Zookeeper 鐘舵€佺殑鎿嶄綔閮戒細褰㈡垚涓€涓搴旂殑 zxid銆?
myid锛氭湇鍔″櫒鐨勫敮涓€鏍囪瘑(SID)锛岄€氳繃閰嶇疆 myid 鏂囦欢鎸囧畾锛岄泦缇や腑鍞竴銆?
  • 閫変妇杩囩▼

褰撻泦缇や腑鐨?zookeeper 鑺傜偣鍚姩浠ュ悗锛屼細鏍规嵁閰嶇疆鏂囦欢涓寚瀹氱殑 zookeeper 鑺傜偣鍦板潃杩涜 leader 閫夋嫨鎿嶄綔锛岃繃绋嬪涓嬶細

  1. 姣忎釜 zookeeper 閮戒細鍙戝嚭鎶曠エ锛岀敱浜庢槸绗竴娆¢€変妇 leader锛屽洜姝ゆ瘡涓妭鐐归兘浼氭妸鑷繁褰撳仛 leader 瑙掕壊杩涜閫変妇锛屾瘡涓?zookeeper 鐨勬姇绁ㄤ腑閮戒細鍖呭惈鑷繁鐨?myid 鍜?zxid锛屾鏃?zookeeper 1 鐨勬姇绁ㄤ负 myid 涓?1锛屽垵濮?zxid 鏈変竴涓垵濮嬪€硷紝鍚庢湡浼氶殢鐫€鏁版嵁鏇存柊鑰岃嚜鍔ㄥ彉鍖栵紝zookeeper2 鐨勬姇绁ㄤ负 myid 涓?2锛屽垵
    濮?zxid 涓哄垵濮嬬敓鎴愮殑鍊笺€?/li>
  2. 姣忎釜鑺傜偣鎺ュ彈骞舵鏌ュ鏂圭殑鎶曠エ淇℃伅锛屾瘮濡傛姇绁ㄦ椂闂淬€佹槸鍚︾姸鎬佷负 LOOKING鐘舵€佺殑鎶曠エ銆?/li>
  3. 瀵规瘮鎶曠エ锛屼紭鍏堟鏌?zxid锛屽鏋?zxid涓嶄竴鏍峰垯 zxid澶х殑涓?leader锛屽鏋?zxid鐩稿悓鍒欑户缁姣?myid锛宮yid 澶х殑涓€鏂逛负 leader

鎴愪负 Leader 鐨勫繀瑕佹潯浠讹細 Leader 瑕佸叿鏈夋渶楂樼殑 zxid锛涘綋闆嗙兢鐨勮妯℃槸 n 鏃讹紝
闆嗙兢涓ぇ澶氭暟鐨勬満鍣紙鑷冲皯 n/2+1锛夊緱鍒板搷搴斿苟 follow 閫夊嚭鐨?Leader銆?/p>

蹇冭烦鏈哄埗锛歀eader 涓?Follower 鍒╃敤 PING 鏉ユ劅鐭ュ鏂圭殑鏄惁瀛樻椿锛屽綋 Leader 鏃?br> 娉曞搷搴?PING 鏃讹紝灏嗛噸鏂板彂璧?Leader 閫変妇銆?/p>

鏂伴泦缇? 鐢变簬娌℃湁鏁版嵁, zxid鍦ㄥ悇涓妭鐐圭浉鍚? 鍥犳, 涓€鑸細姣旇緝myid, 闆嗙兢杩愯鍚? zxid浼氶殢鐫€鏁版嵁鐨勬彃鍏ヨ€屽彂鐢熷彉鍖? 鍝釜鑺傜偣鐨勬暟鎹秺鏂? zxid鍊煎氨浼氳秺澶? 灏变細琚€変妇涓簂eader

  1. 鍚勮妭鐐圭殑閫夋嫨鏃ュ織
cat /apps/zookeeper/logs/zookeeper-root-server-Zoo3.out
  1. 娴嬭瘯鏁版嵁鍚屾

鍒嗗埆鍦ㄤ笁鍙拌妭鐐? 閫氳繃鍛戒护琛屽伐鍏疯繛鎺ュ埌鏈湴ZooKeeper

/apps/zookeeper/bin/zkCli.sh

zoo1涓婂垱寤烘暟鎹?/p>

[zk: localhost:2181(CONNECTED) 0] create /test "hello" # 閫氳繃鍛戒护琛屾槸鏃犳硶閫掑綊鍒涘缓鐩綍鐨?
Created /test

zoo2鏌ョ湅

[zk: localhost:2181(CONNECTED) 0] get /test
hello

zoo3鏌ョ湅

[zk: localhost:2181(CONNECTED) 0] get /test
hello
  1. ZooKeeper鏁版嵁瀛樻斁

ZooKeeper鏁版嵁瀛樻斁鍦ㄩ厤缃枃浠朵腑瀹氫箟鐨勬暟鎹洰褰曚笅鐨剉ersion-x/log鏂囦欢閲? 鍥犳瑕佸鏁版嵁鐩綍鍋氬畾鏈熷浠?br> 鐢变簬涓変釜鏈嶅姟鍣ㄧ殑鏁版嵁閮芥槸鐩稿悓鐨? 鍥犳澶囦唤涓€涓鎴风鐨勫嵆鍙?/p>

cat /apps/zookeeper/data/version-2/log.100000001 
ZKLG"$*,pvf娄驅€棵狂朝侼=$V8Pvf搂C@每每每癯瘋|c $ 鹂亖vf搂H駚€秛0B&
                                                    R*,pvf漏A/testhelloworldanyoneB 

  1. ZooKeeper鐩戞帶
  • Zabbix鐩戞帶鍚勪釜鑺傜偣鐨勫悇涓鍙?/li>
  • 鎵ц瀹㈡埛绔痾kCli.sh鍛戒护, 杩涘叆ZooKeeper,鍐嶉€€鍑? 妫€鏌ュ懡浠ゆ槸鍚︽墽琛屾垚鍔?/li>
  1. Zookeeper瀹㈡埛绔湪Linux骞冲彴浣跨敤
  • 瀹㈡埛绔紪璇?/li>
root@zooinsepect:~# apt install git maven openjdk-8-jdk -y
root@zooinsepect:~# git clone https://github.com/zzhang5/zooinspector.git
root@zooinsepect:~# cd /zooinspector
root@zooinsepect:~/zooinspector# mvn clean package
4. ZooKeeper,第7张
鍥剧墖.png
  • 瀹㈡埛绔娇鐢?/li>
root@Zoo1:~/zooinspector# chmod +x target/zooinspector-pkg/bin/zooinspector.sh
root@Zoo1:~/zooinspector# export DISPLAY=10.0.0.1:0.0

Windows寮€鍚疿manager

淇敼xshell閰嶇疆

4. ZooKeeper,第8张
鍥剧墖.png
root@Zoo1:~/zooinspector# target/zooinspector-pkg/bin/zooinspector.sh
4. ZooKeeper,第9张
鍥剧墖.png
4. ZooKeeper,第10张
鍥剧墖.png

https://www.xamrdz.com/bigdata/7eh1994574.html

相关文章: