"/>

涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?/h1>

Posted 寮€鍙戣€呮妧鏈墠绾?/a>

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?/h1>

相关的知识,希望对你有一定的参考价值。

鐐瑰嚮鈥?/span>寮€鍙戣€呮妧鏈墠绾?/span>鈥濓紝閫夋嫨鈥滄槦鏍囸煍濃€?/span>

璁╀竴閮ㄥ垎寮€鍙戣€呯湅鍒版湭鏉?/span>

鏉ユ簮锛歝nblogs.com/luxiaoxun/p/4887452.html

ZooKeeper 绠€浠?/span>

ZooKeeper鏄竴涓紑鏀炬簮鐮佺殑鍒嗗竷寮忓簲鐢ㄧ▼搴忓崗璋冩湇鍔★紝瀹冨寘鍚竴涓畝鍗曠殑鍘熻闆嗭紝鍒嗗竷寮忓簲鐢ㄧ▼搴忓彲浠ュ熀浜庡畠瀹炵幇鍚屾鏈嶅姟锛岄厤缃淮鎶ゅ拰鍛藉悕鏈嶅姟绛夈€?/span>




ZooKeeper 璁捐鐩殑

1. 鏈€缁堜竴鑷存€э細client涓嶈杩炴帴鍒板摢涓猄erver锛屽睍绀虹粰瀹冮兘鏄悓涓€涓鍥撅紝杩欐槸zookeeper鏈€閲嶈鐨勬€ц兘銆?/span>

2. 鍙潬鎬э細鍏锋湁绠€鍗曘€佸仴澹€佽壇濂界殑鎬ц兘锛屽鏋滄秷鎭痬琚埌涓€鍙版湇鍔″櫒鎺ュ彈锛岄偅涔堝畠灏嗚鎵€鏈夌殑鏈嶅姟鍣ㄦ帴鍙椼€?/span>

3. 瀹炴椂鎬э細Zookeeper淇濊瘉瀹㈡埛绔皢鍦ㄤ竴涓椂闂撮棿闅旇寖鍥村唴鑾峰緱鏈嶅姟鍣ㄧ殑鏇存柊淇℃伅锛屾垨鑰呮湇鍔″櫒澶辨晥鐨勪俊鎭€?/span>
浣嗙敱浜庣綉缁滃欢鏃剁瓑鍘熷洜锛孼ookeeper涓嶈兘淇濊瘉涓や釜瀹㈡埛绔兘鍚屾椂寰楀埌鍒氭洿鏂扮殑鏁版嵁锛屽鏋滈渶瑕佹渶鏂版暟鎹紝搴旇鍦ㄨ鏁版嵁涔嬪墠璋冪敤sync()鎺ュ彛銆?/span>
4. 绛夊緟鏃犲叧锛坵ait-free锛夛細鎱㈢殑鎴栬€呭け鏁堢殑client涓嶅緱骞查蹇€熺殑client鐨勮姹傦紝浣垮緱姣忎釜client閮借兘鏈夋晥鐨勭瓑寰呫€?/span>
5. 鍘熷瓙鎬э細鏇存柊鍙兘鎴愬姛鎴栬€呭け璐ワ紝娌℃湁涓棿鐘舵€併€?/span>
6. 椤哄簭鎬э細鍖呮嫭鍏ㄥ眬鏈夊簭鍜屽亸搴忎袱绉嶏細鍏ㄥ眬鏈夊簭鏄寚濡傛灉鍦ㄤ竴鍙版湇鍔″櫒涓婃秷鎭痑鍦ㄦ秷鎭痓鍓嶅彂甯冿紝鍒欏湪鎵€鏈塖erver涓婃秷鎭痑閮藉皢鍦ㄦ秷鎭痓鍓嶈鍙戝竷锛涘亸搴忔槸鎸囧鏋滀竴涓秷鎭痓鍦ㄦ秷鎭痑鍚庤鍚屼竴涓彂閫佽€呭彂甯冿紝a蹇呭皢鎺掑湪b鍓嶉潰銆?/span>

ZooKeeper鏁版嵁妯″瀷

Zookeeper浼氱淮鎶や竴涓叿鏈夊眰娆″叧绯荤殑鏁版嵁缁撴瀯锛屽畠闈炲父绫讳技浜庝竴涓爣鍑嗙殑鏂囦欢绯荤粺锛屽鍥炬墍绀猴細


涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?></p> 
  <p><br></p> 
  <section> 
   <span><span class=Zookeeper杩欑鏁版嵁缁撴瀯鏈夊涓嬭繖浜涚壒鐐癸細

1锛夋瘡涓瓙鐩綍椤瑰NameService閮借绉颁綔涓簔node锛岃繖涓獄node鏄瀹冩墍鍦ㄧ殑璺緞鍞竴鏍囪瘑锛屽Server1杩欎釜znode鐨勬爣璇嗕负/NameService/Server1銆?/span>

2锛墇node鍙互鏈夊瓙鑺傜偣鐩綍锛屽苟涓旀瘡涓獄node鍙互瀛樺偍鏁版嵁锛屾敞鎰廍PHEMERAL锛堜复鏃剁殑锛夌被鍨嬬殑鐩綍鑺傜偣涓嶈兘鏈夊瓙鑺傜偣鐩綍銆?/span>

3锛墇node鏄湁鐗堟湰鐨勶紙version锛夛紝姣忎釜znode涓瓨鍌ㄧ殑鏁版嵁鍙互鏈夊涓増鏈紝涔熷氨鏄竴涓闂矾寰勪腑鍙互瀛樺偍澶氫唤鏁版嵁锛寁ersion鍙疯嚜鍔ㄥ鍔犮€?/span>

4锛墇node鐨勭被鍨嬶細
  • Persistent 鑺傜偣锛屼竴鏃﹁鍒涘缓锛屼究涓嶄細鎰忓涓㈠け锛屽嵆浣挎湇鍔″櫒鍏ㄩ儴閲嶅惎涔熶緷鐒跺瓨鍦ㄣ€傛瘡涓?Persist 鑺傜偣鍗冲彲鍖呭惈鏁版嵁锛屼篃鍙寘鍚瓙鑺傜偣銆?
  • Ephemeral 鑺傜偣锛屽湪鍒涘缓瀹冪殑瀹㈡埛绔笌鏈嶅姟鍣ㄩ棿鐨?Session 缁撴潫鏃惰嚜鍔ㄨ鍒犻櫎銆傛湇鍔″櫒閲嶅惎浼氬鑷?Session 缁撴潫锛屽洜姝?Ephemeral 绫诲瀷鐨?znode 姝ゆ椂涔熶細鑷姩鍒犻櫎銆?
  • Non-sequence 鑺傜偣锛屽涓鎴风鍚屾椂鍒涘缓鍚屼竴 Non-sequence 鑺傜偣鏃讹紝鍙湁涓€涓彲鍒涘缓鎴愬姛锛屽叾瀹冨寑澶辫触銆傚苟涓斿垱寤哄嚭鐨勮妭鐐瑰悕绉颁笌鍒涘缓鏃舵寚瀹氱殑鑺傜偣鍚嶅畬鍏ㄤ竴鏍枫€?
  • Sequence 鑺傜偣锛屽垱寤哄嚭鐨勮妭鐐瑰悕鍦ㄦ寚瀹氱殑鍚嶇О涔嬪悗甯︽湁10浣?0杩涘埗鏁扮殑搴忓彿銆傚涓鎴风鍒涘缓鍚屼竴鍚嶇О鐨勮妭鐐规椂锛岄兘鑳藉垱寤烘垚鍔燂紝鍙槸搴忓彿涓嶅悓銆?

5锛墇node鍙互琚洃鎺э紝鍖呮嫭杩欎釜鐩綍鑺傜偣涓瓨鍌ㄧ殑鏁版嵁鐨勪慨鏀癸紝瀛愯妭鐐圭洰褰曠殑鍙樺寲绛夛紝涓€鏃﹀彉鍖栧彲浠ラ€氱煡璁剧疆鐩戞帶鐨勫鎴风锛岃繖涓槸Zookeeper鐨勬牳蹇冪壒鎬э紝Zookeeper鐨勫緢澶氬姛鑳介兘鏄熀浜庤繖涓壒鎬у疄鐜扮殑銆?/span>

6锛塟XID锛氭瘡娆″Zookeeper鐨勭姸鎬佺殑鏀瑰彉閮戒細浜х敓涓€涓獄xid锛圸ooKeeper Transaction Id锛夛紝zxid鏄叏灞€鏈夊簭鐨勶紝濡傛灉zxid1灏忎簬zxid2锛屽垯zxid1鍦▃xid2涔嬪墠鍙戠敓銆?/span>


ZooKeeper Session

Client鍜孼ookeeper闆嗙兢寤虹珛杩炴帴锛屾暣涓猻ession鐘舵€佸彉鍖栧鍥炬墍绀猴細

涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?> 
  </section> 
  <section> 
   <span class=濡傛灉Client鍥犱负Timeout鍜孼ookeeper Server澶卞幓杩炴帴锛宑lient澶勫湪CONNECTING鐘舵€侊紝浼氳嚜鍔ㄥ皾璇曞啀鍘昏繛鎺erver锛屽鏋滃湪session鏈夋晥鏈熷唴鍐嶆鎴愬姛杩炴帴鍒版煇涓猄erver锛屽垯鍥炲埌CONNECTED鐘舵€併€?/span>

娉ㄦ剰锛?/strong>濡傛灉鍥犱负缃戠粶鐘舵€佷笉濂斤紝client鍜孲erver澶卞幓鑱旂郴锛宑lient浼氬仠鐣欏湪褰撳墠鐘舵€侊紝浼氬皾璇曚富鍔ㄥ啀娆¤繛鎺ookeeper Server銆俢lient涓嶈兘瀹gО鑷繁鐨剆ession expired锛宻ession expired鏄敱Zookeeper Server鏉ュ喅瀹氱殑锛宑lient鍙互閫夋嫨鑷繁涓诲姩鍏抽棴session銆?/span>


ZooKeeper Watch

Zookeeper watch鏄竴绉嶇洃鍚€氱煡鏈哄埗銆俍ookeeper鎵€鏈夌殑璇绘搷浣済etData(), getChildren()鍜?exists()閮藉彲浠ヨ缃洃瑙?watch)锛岀洃瑙嗕簨浠跺彲浠ョ悊瑙d负涓€娆℃€х殑瑙﹀彂鍣?/span>


瀹樻柟瀹氫箟濡備笅锛?/span>

a watch event is one-time trigger, sent to the client that set the watch, whichoccurs when the data for which the watch was set changes銆?/p>


Watch鐨勪笁涓叧閿偣锛?/span>

  • 锛堜竴娆℃€цЕ鍙戯級One-time trigger
褰撹缃洃瑙嗙殑鏁版嵁鍙戠敓鏀瑰彉鏃讹紝璇ョ洃瑙嗕簨浠朵細琚彂閫佸埌瀹㈡埛绔€?/span>
渚嬪锛屽鏋滃鎴风璋冪敤浜唃etData(/znode1, true) 骞朵笖绋嶅悗 /znode1 鑺傜偣涓婄殑鏁版嵁鍙戠敓浜嗘敼鍙樻垨鑰呰鍒犻櫎浜嗭紝瀹㈡埛绔皢浼氳幏鍙栧埌 /znode1 鍙戠敓鍙樺寲鐨勭洃瑙嗕簨浠讹紱
鑰屽鏋?/znode1 鍐嶄竴娆″彂鐢熶簡鍙樺寲锛岄櫎闈炲鎴风鍐嶆瀵?znode1 璁剧疆鐩戣锛屽惁鍒欏鎴风涓嶄細鏀跺埌浜嬩欢閫氱煡銆?br>

  • 锛堝彂閫佽嚦瀹㈡埛绔級Sent to the client
Zookeeper瀹㈡埛绔拰鏈嶅姟绔槸閫氳繃 socket 杩涜閫氫俊鐨勶紝鐢变簬缃戠粶瀛樺湪鏁呴殰锛屾墍浠ョ洃瑙嗕簨浠跺緢鏈夊彲鑳戒笉浼氭垚鍔熷湴鍒拌揪瀹㈡埛绔紝鐩戣浜嬩欢鏄紓姝ュ彂閫佽嚦鐩戣鑰呯殑銆?/span>
Zookeeper 鏈韩鎻愪緵浜嗛『搴忎繚璇?ordering guarantee)锛氬嵆瀹㈡埛绔彧鏈夐鍏堢湅鍒颁簡鐩戣浜嬩欢鍚庯紝鎵嶄細鎰熺煡鍒板畠鎵€璁剧疆鐩戣鐨剒node鍙戠敓浜嗗彉鍖?a client will never see a change for which it has set a watch until it first sees the watch event)銆?/span>
缃戠粶寤惰繜鎴栬€呭叾浠栧洜绱犲彲鑳藉鑷翠笉鍚岀殑瀹㈡埛绔湪涓嶅悓鐨勬椂鍒绘劅鐭ユ煇涓€鐩戣浜嬩欢锛屼絾鏄笉鍚岀殑瀹㈡埛绔墍鐪嬪埌鐨勪竴鍒囧叿鏈変竴鑷寸殑椤哄簭銆?/span>

  • 锛堣璁剧疆 watch 鐨勬暟鎹級The data for which the watch was set
杩欐剰鍛崇潃znode鑺傜偣鏈韩鍏锋湁涓嶅悓鐨勬敼鍙樻柟寮忋€備綘涔熷彲浠ユ兂璞?Zookeeper 缁存姢浜嗕袱鏉$洃瑙嗛摼琛細鏁版嵁鐩戣鍜屽瓙鑺傜偣鐩戣(data watches and child watches) getData() 鍜宔xists()璁剧疆鏁版嵁鐩戣锛実etChildren()璁剧疆瀛愯妭鐐圭洃瑙嗐€?/span>
鎴栬€呬綘涔熷彲浠ユ兂璞?Zookeeper 璁剧疆鐨勪笉鍚岀洃瑙嗚繑鍥炰笉鍚岀殑鏁版嵁锛実etData() 鍜?exists() 杩斿洖znode鑺傜偣鐨勭浉鍏充俊鎭紝鑰実etChildren() 杩斿洖瀛愯妭鐐瑰垪琛ㄣ€?/span>

鍥犳锛宻etData() 浼氳Е鍙戣缃湪鏌愪竴鑺傜偣涓婃墍璁剧疆鐨勬暟鎹洃瑙嗭紙鍋囧畾鏁版嵁璁剧疆鎴愬姛锛夛紝鑰屼竴娆℃垚鍔熺殑create() 鎿嶄綔鍒欎細鍑哄彂褰撳墠鑺傜偣涓婃墍璁剧疆鐨勬暟鎹洃瑙嗕互鍙婄埗鑺傜偣鐨勫瓙鑺傜偣鐩戣銆?/span>
涓€娆℃垚鍔熺殑 delete鎿嶄綔灏嗕細瑙﹀彂褰撳墠鑺傜偣鐨勬暟鎹洃瑙嗗拰瀛愯妭鐐圭洃瑙嗕簨浠讹紝鍚屾椂涔熶細瑙﹀彂璇ヨ妭鐐圭埗鑺傜偣鐨刢hild watch銆?/span>

Zookeeper 涓殑鐩戣鏄交閲忕骇鐨勶紝鍥犳瀹规槗璁剧疆銆佺淮鎶ゅ拰鍒嗗彂銆傚綋瀹㈡埛绔笌 Zookeeper 鏈嶅姟鍣ㄥけ鍘昏仈绯绘椂锛屽鎴风骞朵笉浼氭敹鍒扮洃瑙嗕簨浠剁殑閫氱煡锛屽彧鏈夊綋瀹㈡埛绔噸鏂拌繛鎺ュ悗锛岃嫢鍦ㄥ繀瑕佺殑鎯呭喌涓嬶紝浠ュ墠娉ㄥ唽鐨勭洃瑙嗕細閲嶆柊琚敞鍐屽苟瑙﹀彂锛屽浜庡紑鍙戜汉鍛樻潵璇磋繖閫氬父鏄€忔槑鐨勩€?/span>

鍙湁涓€绉嶆儏鍐典細瀵艰嚧鐩戣浜嬩欢鐨勪涪澶憋紝鍗筹細閫氳繃exists()璁剧疆浜嗘煇涓獄node鑺傜偣鐨勭洃瑙嗭紝浣嗘槸濡傛灉鏌愪釜瀹㈡埛绔湪姝node鑺傜偣琚垱寤哄拰鍒犻櫎鐨勬椂闂撮棿闅斿唴涓巣ookeeper鏈嶅姟鍣ㄥけ鍘讳簡鑱旂郴锛岃瀹㈡埛绔嵆浣跨◢鍚庨噸鏂拌繛鎺?zookeeper鏈嶅姟鍣ㄥ悗涔熷緱涓嶅埌浜嬩欢閫氱煡銆?/span>


Consistency Guarantees

Zookeeper鏄竴涓珮鏁堢殑銆佸彲鎵╁睍鐨勬湇鍔★紝read鍜寃rite鎿嶄綔閮借璁捐涓哄揩閫熺殑锛宺ead姣攚rite鎿嶄綔鏇村揩銆?/span>

椤哄簭涓€鑷存€э紙Sequential Consistency锛夛細浠庝竴涓鎴风鏉ョ殑鏇存柊璇锋眰浼氳椤哄簭鎵ц銆?/span>

鍘熷瓙鎬э紙Atomicity锛夛細鏇存柊瑕佷箞鎴愬姛瑕佷箞澶辫触锛屾病鏈夐儴鍒嗘垚鍔熺殑鎯呭喌銆?/span>

鍞竴鐨勭郴缁熼暅鍍忥紙Single System Image锛夛細鏃犺瀹㈡埛绔繛鎺ュ埌鍝釜Server锛岀湅鍒扮郴缁熼暅鍍忔槸涓€鑷寸殑銆?/span>

鍙潬鎬э紙Reliability锛夛細鏇存柊涓€鏃︽湁鏁堬紝鎸佺画鏈夋晥锛岀洿鍒拌瑕嗙洊銆?/span>

鏃堕棿绾匡紙Timeliness锛夛細淇濊瘉鍦ㄤ竴瀹氱殑鏃堕棿鍐呭悇涓鎴风鐪嬪埌鐨勭郴缁熶俊鎭槸涓€鑷寸殑銆?/span>


ZooKeeper鐨勫伐浣滃師鐞?/span>

鍦▃ookeeper鐨勯泦缇や腑锛屽悇涓妭鐐瑰叡鏈変笅闈?绉嶈鑹插拰4绉嶇姸鎬侊細


  • 瑙掕壊锛?/strong>leader,follower,observer
  • 鐘舵€侊細leading,following,observing,looking


Zookeeper鐨勬牳蹇冩槸鍘熷瓙骞挎挱锛岃繖涓満鍒朵繚璇佷簡鍚勪釜Server涔嬮棿鐨勫悓姝ャ€傚疄鐜拌繖涓満鍒剁殑鍗忚鍙仛Zab鍗忚锛圸ooKeeper Atomic Broadcast protocol锛夈€俍ab鍗忚鏈変袱绉嶆ā寮忥紝瀹冧滑鍒嗗埆鏄仮澶嶆ā寮忥紙Recovery閫変富锛夊拰骞挎挱妯″紡锛圔roadcast鍚屾锛夈€?/span>


褰撴湇鍔″惎鍔ㄦ垨鑰呭湪棰嗗鑰呭穿婧冨悗锛孼ab灏辫繘鍏ヤ簡鎭㈠妯″紡锛屽綋棰嗗鑰呰閫変妇鍑烘潵锛屼笖澶у鏁癝erver瀹屾垚浜嗗拰leader鐨勭姸鎬佸悓姝ヤ互鍚庯紝鎭㈠妯″紡灏辩粨鏉熶簡銆傜姸鎬佸悓姝ヤ繚璇佷簡leader鍜孲erver鍏锋湁鐩稿悓鐨勭郴缁熺姸鎬併€?/span>


涓轰簡淇濊瘉浜嬪姟鐨勯『搴忎竴鑷存€э紝zookeeper閲囩敤浜嗛€掑鐨勪簨鍔d鍙凤紙zxid锛夋潵鏍囪瘑浜嬪姟銆傛墍鏈夌殑鎻愯锛坧roposal锛夐兘鍦ㄨ鎻愬嚭鐨勬椂鍊欏姞涓婁簡zxid銆?/span>


瀹炵幇涓瓃xid鏄竴涓?4浣嶇殑鏁板瓧锛屽畠楂?2浣嶆槸epoch鐢ㄦ潵鏍囪瘑leader鍏崇郴鏄惁鏀瑰彉锛屾瘡娆′竴涓猯eader琚€夊嚭鏉ワ紝瀹冮兘浼氭湁涓€涓柊鐨別poch锛屾爣璇嗗綋鍓嶅睘浜庨偅涓猯eader鐨勭粺娌绘椂鏈熴€備綆32浣嶇敤浜庨€掑璁℃暟銆?/span>


姣忎釜Server鍦ㄥ伐浣滆繃绋嬩腑鏈?绉嶇姸鎬侊細
LOOKING锛?/strong>褰撳墠Server涓嶇煡閬搇eader鏄皝锛屾鍦ㄦ悳瀵汇€?/span>
LEADING锛?/strong>褰撳墠Server鍗充负閫変妇鍑烘潵鐨刲eader銆?/span>
FOLLOWING锛?/strong>leader宸茬粡閫変妇鍑烘潵锛屽綋鍓峉erver涓庝箣鍚屾銆?/span>
OBSERVING锛?/strong>observer鐨勮涓哄湪澶у鏁版儏鍐典笅涓巉ollower瀹屽叏涓€鑷达紝浣嗘槸浠栦滑涓嶅弬鍔犻€変妇鍜屾姇绁紝鑰屼粎浠呮帴鍙?observing)閫変妇鍜屾姇绁ㄧ殑缁撴灉銆?/span>

Leader Election

褰搇eader宕╂簝鎴栬€卨eader澶卞幓澶у鏁扮殑follower锛岃繖鏃跺€檢k杩涘叆鎭㈠妯″紡锛屾仮澶嶆ā寮忛渶瑕侀噸鏂伴€変妇鍑轰竴涓柊鐨刲eader锛岃鎵€鏈夌殑Server閮芥仮澶嶅埌涓€涓纭殑鐘舵€併€?/span>


Zk鐨勯€変妇绠楁硶鏈変袱绉嶏細涓€绉嶆槸鍩轰簬basic paxos瀹炵幇鐨勶紝鍙﹀涓€绉嶆槸鍩轰簬fast paxos绠楁硶瀹炵幇鐨勩€?/span>


绯荤粺榛樿鐨勯€変妇绠楁硶涓篺ast paxos銆傚厛浠嬬粛basic paxos娴佺▼锛?/span>
1. 閫変妇绾跨▼鐢卞綋鍓峉erver鍙戣捣閫変妇鐨勭嚎绋嬫媴浠伙紝鍏朵富瑕佸姛鑳芥槸瀵规姇绁ㄧ粨鏋滆繘琛岀粺璁★紝骞堕€夊嚭鎺ㄨ崘鐨凷erver锛?/span>

2. 閫変妇绾跨▼棣栧厛鍚戞墍鏈塖erver鍙戣捣涓€娆¤闂紙鍖呮嫭鑷繁锛夛紱

3. 閫変妇绾跨▼鏀跺埌鍥炲鍚庯紝楠岃瘉鏄惁鏄嚜宸卞彂璧风殑璇㈤棶锛堥獙璇亃xid鏄惁涓€鑷达級锛岀劧鍚庤幏鍙栧鏂圭殑id锛坢yid锛夛紝骞跺瓨鍌ㄥ埌褰撳墠璇㈤棶瀵硅薄鍒楄〃涓紝鏈€鍚庤幏鍙栧鏂规彁璁殑leader鐩稿叧淇℃伅锛坕d,zxid锛夛紝骞跺皢杩欎簺淇℃伅瀛樺偍鍒板綋娆¢€変妇鐨勬姇绁ㄨ褰曡〃涓紱

4. 鏀跺埌鎵€鏈塖erver鍥炲浠ュ悗锛屽氨璁$畻鍑簔xid鏈€澶х殑閭d釜Server锛屽苟灏嗚繖涓猄erver鐩稿叧淇℃伅璁剧疆鎴愪笅涓€娆¤鎶曠エ鐨凷erver锛?/span>

5. 绾跨▼灏嗗綋鍓峼xid鏈€澶х殑Server璁剧疆涓哄綋鍓峉erver瑕佹帹鑽愮殑Leader锛屽鏋滄鏃惰幏鑳滅殑Server鑾峰緱n/2 + 1鐨凷erver绁ㄦ暟锛岃缃綋鍓嶆帹鑽愮殑leader涓鸿幏鑳滅殑Server锛屽皢鏍规嵁鑾疯儨鐨凷erver鐩稿叧淇℃伅璁剧疆鑷繁鐨勭姸鎬侊紝鍚﹀垯锛岀户缁繖涓繃绋嬶紝鐩村埌leader琚€変妇鍑烘潵銆?/span>

閫氳繃娴佺▼鍒嗘瀽鎴戜滑鍙互寰楀嚭锛氳浣縇eader鑾峰緱澶氭暟Server鐨勬敮鎸侊紝鍒橲erver鎬绘暟蹇呴』鏄鏁?n+1锛屼笖瀛樻椿鐨凷erver鐨勬暟鐩笉寰楀皯浜巒+1.

姣忎釜Server鍚姩鍚庨兘浼氶噸澶嶄互涓婃祦绋嬨€傚湪鎭㈠妯″紡涓嬶紝濡傛灉鏄垰浠庡穿婧冪姸鎬佹仮澶嶇殑鎴栬€呭垰鍚姩鐨剆erver杩樹細浠庣鐩樺揩鐓т腑鎭㈠鏁版嵁鍜屼細璇濅俊鎭紝zk浼氳褰曚簨鍔℃棩蹇楀苟瀹氭湡杩涜蹇収锛屾柟渚垮湪鎭㈠鏃惰繘琛岀姸鎬佹仮澶嶃€?/span>

fast paxos娴佺▼鏄湪閫変妇杩囩▼涓紝鏌怱erver棣栧厛鍚戞墍鏈塖erver鎻愯鑷繁瑕佹垚涓簂eader锛屽綋鍏跺畠Server鏀跺埌鎻愯浠ュ悗锛岃В鍐砮poch鍜寊xid鐨勫啿绐侊紝骞舵帴鍙楀鏂圭殑鎻愯锛岀劧鍚庡悜瀵规柟鍙戦€佹帴鍙楁彁璁畬鎴愮殑娑堟伅锛岄噸澶嶈繖涓祦绋嬶紝鏈€鍚庝竴瀹氳兘閫変妇鍑篖eader銆?/span>


Leader宸ヤ綔娴佺▼

Leader涓昏鏈変笁涓姛鑳斤細

  1. 鎭㈠鏁版嵁锛?
  2. 缁存寔涓巉ollower鐨勫績璺筹紝鎺ユ敹follower璇锋眰骞跺垽鏂璮ollower鐨勮姹傛秷鎭被鍨嬶紱
  3. follower鐨勬秷鎭被鍨嬩富瑕佹湁PING娑堟伅銆丷EQUEST娑堟伅銆丄CK娑堟伅銆丷EVALIDATE娑堟伅锛屾牴鎹笉鍚岀殑娑堟伅绫诲瀷锛岃繘琛屼笉鍚岀殑澶勭悊銆?

璇存槑锛?/strong>
PING娑堟伅鏄寚follower鐨勫績璺充俊鎭紱REQUEST娑堟伅鏄痜ollower鍙戦€佺殑鎻愯淇℃伅锛屽寘鎷啓璇锋眰鍙婂悓姝ヨ姹傦紱
ACK娑堟伅鏄痜ollower鐨勫鎻愯鐨勫洖澶嶏紝瓒呰繃鍗婃暟鐨刦ollower閫氳繃锛屽垯commit璇ユ彁璁紱
REVALIDATE娑堟伅鏄敤鏉ュ欢闀縎ESSION鏈夋晥鏃堕棿銆?/span>


Follower宸ヤ綔娴佺▼

Follower涓昏鏈夊洓涓姛鑳斤細
  1. 鍚慙eader鍙戦€佽姹傦紙PING娑堟伅銆丷EQUEST娑堟伅銆丄CK娑堟伅銆丷EVALIDATE娑堟伅锛夛紱
  2. 鎺ユ敹Leader娑堟伅骞惰繘琛屽鐞嗭紱
  3. 鎺ユ敹Client鐨勮姹傦紝濡傛灉涓哄啓璇锋眰锛屽彂閫佺粰Leader杩涜鎶曠エ锛?
  4. 杩斿洖Client缁撴灉銆?

Follower鐨勬秷鎭惊鐜鐞嗗涓嬪嚑绉嶆潵鑷狶eader鐨勬秷鎭細
  1. PING娑堟伅锛氬績璺虫秷鎭?
  2. PROPOSAL娑堟伅锛歀eader鍙戣捣鐨勬彁妗堬紝瑕佹眰Follower鎶曠エ
  3. COMMIT娑堟伅锛氭湇鍔″櫒绔渶鏂颁竴娆℃彁妗堢殑淇℃伅
  4. UPTODATE娑堟伅锛氳〃鏄庡悓姝ュ畬鎴?
  5. REVALIDATE娑堟伅锛氭牴鎹甃eader鐨凴EVALIDATE缁撴灉锛屽叧闂緟revalidate鐨剆ession杩樻槸鍏佽鍏舵帴鍙楁秷鎭?
  6. SYNC娑堟伅锛氳繑鍥濻YNC缁撴灉鍒板鎴风锛岃繖涓秷鎭渶鍒濈敱瀹㈡埛绔彂璧凤紝鐢ㄦ潵寮哄埗寰楀埌鏈€鏂扮殑鏇存柊銆?


Zab: Broadcasting State Updates

Zookeeper Server鎺ユ敹鍒颁竴娆equest锛屽鏋滄槸follower锛屼細杞彂缁檒eader锛孡eader鎵ц璇锋眰骞堕€氳繃Transaction鐨勫舰寮忓箍鎾繖娆℃墽琛屻€?/span>


Zookeeper闆嗙兢濡備綍鍐冲畾涓€涓猅ransaction鏄惁琚玞ommit鎵ц锛熼€氳繃鈥滀袱娈垫彁浜ゅ崗璁€濓紙a two-phase commit锛夛細

  • Leader缁欐墍鏈夌殑follower鍙戦€佷竴涓狿ROPOSAL娑堟伅銆?
  • 涓€涓猣ollower鎺ユ敹鍒拌繖娆ROPOSAL娑堟伅锛屽啓鍒扮鐩橈紝鍙戦€佺粰leader涓€涓狝CK娑堟伅锛屽憡鐭ュ凡缁忔敹鍒般€?
  • 褰揕eader鏀跺埌娉曞畾浜烘暟锛坬uorum锛夌殑follower鐨凙CK鏃跺€欙紝鍙戦€乧ommit娑堟伅鎵ц銆?

Zab鍗忚淇濊瘉锛?/span>
  • 濡傛灉leader浠1鍜孴2鐨勯『搴忓箍鎾紝閭d箞鎵€鏈夌殑Server蹇呴』鍏堟墽琛孴1锛屽啀鎵цT2銆?
  • 濡傛灉浠绘剰涓€涓猄erver浠1銆乀2鐨勯『搴廲ommit鎵ц锛屽叾浠栨墍鏈夌殑Server涔熷繀椤讳互T1銆乀2鐨勯『搴忔墽琛屻€?

鈥滀袱娈垫彁浜ゅ崗璁€濇渶澶х殑闂鏄鏋淟eader鍙戦€佷簡PROPOSAL娑堟伅鍚巆rash鎴栨殏鏃跺け鍘昏繛鎺ワ紝浼氬鑷存暣涓泦缇ゅ鍦ㄤ竴绉嶄笉纭畾鐨勭姸鎬侊紙follower涓嶇煡閬撹鏀惧純杩欐鎻愪氦杩樻槸鎵ц鎻愪氦锛夈€?/span>

Zookeeper杩欐椂浼氶€夊嚭鏂扮殑leader锛岃姹傚鐞嗕篃浼氱Щ鍒版柊鐨刲eader涓婏紝涓嶅悓鐨刲eader鐢变笉鍚岀殑epoch鏍囪瘑銆傚垏鎹eader鏃讹紝闇€瑕佽В鍐充笅闈袱涓棶棰橈細

1. Never forget delivered messages
Leader鍦–OMMIT鎶曢€掑埌浠讳綍涓€鍙癴ollower涔嬪墠crash锛屽彧鏈夊畠鑷繁commit浜嗐€傛柊Leader蹇呴』淇濊瘉杩欎釜浜嬪姟涔熷繀椤籧ommit銆?/span>

2. Let go of messages that are skipped
Leader浜х敓鏌愪釜proposal锛屼絾鏄湪crash涔嬪墠锛屾病鏈塮ollower鐪嬪埌杩欎釜proposal銆傝server鎭㈠鏃讹紝蹇呴』涓㈠純杩欎釜proposal銆?/span>

Zookeeper浼氬敖閲忎繚璇佷笉浼氬悓鏃舵湁2涓椿鍔ㄧ殑Leader锛屽洜涓?涓笉鍚岀殑Leader浼氬鑷撮泦缇ゅ鍦ㄤ竴绉嶄笉涓€鑷寸殑鐘舵€侊紝鎵€浠ab鍗忚鍚屾椂淇濊瘉锛?/span>
  • 鍦ㄦ柊鐨刲eader骞挎挱Transaction涔嬪墠锛屽厛鍓峀eader commit鐨凾ransaction閮戒細鍏堟墽琛屻€?
  • 鍦ㄤ换鎰忔椂鍒伙紝閮戒笉浼氭湁2涓猄erver鍚屾椂鏈夋硶瀹氫汉鏁帮紙quorum锛夌殑鏀寔鑰呫€?
    杩欓噷鐨剄uorum鏄竴鍗婁互涓婄殑Server鏁扮洰锛岀‘鍒囩殑璇存槸鏈夋姇绁ㄦ潈鍔涚殑Server锛堜笉鍖呮嫭Observer锛夈€?

鎬荤粨

绠€鍗曚粙缁嶄簡Zookeeper鐨勫熀鏈師鐞嗭紝鏁版嵁妯″瀷锛孲ession锛學atch鏈哄埗锛屼竴鑷存€т繚璇侊紝Leader Election锛孡eader鍜孎ollower鐨勫伐浣滄祦绋嬪拰Zab鍗忚銆?/span>


---  END  ---

鍦ㄨ繖閲岋紝鎴戜负澶у鍑嗗浜嗕竴浠?020骞存渶鏂版渶鍏ㄧ殑銆奐ava闈㈣瘯棰樺強绛旀V3.0銆嬶紝杩欏鐢靛瓙涔︽兜鐩栦簡璇稿鍚庣鎶€鏈爤鐨勯潰璇曢鍜岀瓟妗堬紝鐩镐俊鍙互甯姪澶у鍦ㄦ渶鐭殑鏃堕棿鍐呭涔燡ava鍚庣鐨勫ぇ澶氭暟闈㈣瘯棰橈紝浠庤€屾嬁鍒拌嚜宸卞績浠殑offer銆?
鎴簡寮犲浘锛屽ぇ瀹跺彲浠ヤ粩缁嗘煡鐪嬪乏杈圭殑鑿滃崟鏍忥紝瑕嗙洊鐨勭煡璇嗛潰鐪熺殑寰堝箍锛岃€屼笖璐ㄩ噺閮藉緢涓嶉敊銆?

涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?> 
   </section> 
   <section class= 璧勬枡鑾峰彇鏂规硶
  1. 鎵弿涓嬫柟浜岀淮鐮?/span>

  2. 鍚庡彴鍥炲鍏抽敭璇嶏細Java鏍稿績鏁寸悊


涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?> 
   </section> 
   <section class=
鍘嗗彶鎺ㄨ崘






涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?> 
                  </section> 
                 </section> 
                </section> 
               </section> 
              </section> 
             </section> 
            </section> 
           </section> 
          </section> 
         </section> 
        </section> 
       </article> 
      </section> 
      <section class=
涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?> 
        </section> 
        <section class=
鐐逛釜鍦ㄧ湅鍚?/span>

以上是关于涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?/h1>

的主要内容,如果未能解决你的问题,请参考以下文章

闈㈣瘯蹇呴棶鐨?JVM 杩愯鏃舵暟鎹尯锛屼綘鎳備簡鍚楋紵

涓轰粈涔堟垜瑕佹敼鍙樿嚜宸憋紵

fastjson鍒板簳鍋氶敊浜嗕粈涔堬紵涓轰粈涔堜細琚绻佺垎鍑烘紡娲烇紵

涓轰粈涔堣浣跨敤鏅鸿兘鍚堢害鏉ユ瀯寤哄尯鍧楅摼搴旂敤绋嬪簭?

鑻规灉涓轰粈涔堜笉灏佹潃 Flutter 锛?/h1>