鑱婅亰 Raft 绠楁硶
Posted SegmentFault
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鑱婅亰 Raft 绠楁硶相关的知识,希望对你有一定的参考价值。
鏈枃杞浇浜?SegmentFault 绀惧尯
涓€銆佹杩?/span>
鏈€杩戠粓浜庢湁鏃堕棿鍘荤湅鐪?raft 绠楁硶鐨勮鏂囷紝raft 浣滀负涓€涓槗浜庣悊瑙e拰瀹炵幇鐨勪竴鑷存€х畻娉?/span>锛堟瘯绔熸垜杩欐牱鐨勮彍馃惗閮借兘鐪嬫噦锛?/span>锛屽凡缁忓簲鐢ㄥ埌寰堝绯荤粺鐨勬瀯寤轰笂锛堝垎甯冨紡鏁版嵁搴?TIDB锛孯ocketMq 鐨?Dledger 绛夛級锛屾槸闈炲父鍊煎緱鍘绘繁鍏ョ悊瑙e拰鐮旂┒鐨勩€傛湰鏂囦富瑕佹槸瀛︿範 raft 璁烘枃鐨勬€荤粨浠ュ強鑷繁鐨勪竴浜涙兂娉曘€傚鏋滃ぇ瀹跺璁烘枃宸茬粡瓒冲浜嗚В銆傞偅灏变笉鐢ㄧ户缁線涓嬬湅浜嗐€傚厜鐪嬬湅鏂囨湯鐨勪釜浜烘兂娉?/span>锛堟剼瑙侊級锛屾杩庢潵鍜屾垜鎺㈣锛?/span>
浜屻€丷aft 绠楁硶姒傝堪
raft 绠楁硶涔嬫墍浠ュ鏄撶悊瑙o紝鍏朵竴鏄粬灏嗕竴鑷存€ч棶棰樺垝鍒嗘垚鍑犱釜瀛愰棶棰橈紝杩欏嚑涓瓙闂閮芥槸鐙珛銆佸彲鐞嗚В鍜岃В閲婄殑銆備粠浼犵粺鐨勬€濈淮鏉ヨ锛屽浜庝竴涓鏉傜殑绯荤粺鎴栬€呭伐绋嬶紝閮芥槸澶у寲灏忥紝鍒嗚В瀹炵幇锛岀劧鍚庡幓灏濊瘯铻嶅悎瑙e喅鏁翠綋閫昏緫銆傚寘鎷?CS 绯荤粺鐨勮璁′篃鏄姝ゃ€?/span>
涓€鑷存€х畻娉曠殑鐩爣
1. 瀹夊叏鎬э細鍦ㄩ潪鎷滃崰搴敊璇儏鍐典笅锛屽寘鎷綉缁滃欢杩熴€佸垎鍖恒€佷涪鍖呫€佸啑浣欏拰涔卞簭绛夐敊璇兘鍙互淇濊瘉姝g‘銆?br>2. 鍙敤鎬э細鍙闆嗙兢涓ぇ澶氭暟鑺傜偣澶勪簬 runing锛屽苟涓斾笉鍒嗗尯锛屽拰瀹㈡埛绔兘閫氫俊锛岄偅涔堟垜浠渶瑕佷繚璇佽繖涓泦缇ゅ彲鐢ㄣ€?br>3. 瀵逛簬鏁版嵁鍚屾锛屽皬閮ㄥ垎鎱㈣妭鐐圭殑涓嶄細褰卞搷绯荤粺鎬ц兘銆傚洜涓哄浜庢棩蹇楀鍒讹紝鎴戜滑濡傛灉绛夊緟鎵€鏈夎妭鐐瑰搷搴旓紝閭d箞绯荤粺鐨勬€ц兘浼氬瓨鍦ㄧ煭鏉挎晥搴斻€?/span>
璇寸櫧浜嗭紝灏辨槸濡傛灉涓€涓泦缇や腑锛屽鏋滃ぇ澶氭暟鑺傜偣鍙敤 锛堢綉缁溿€佹湇鍔★級 锛岄偅涔堥€氳繃 raft 绠楁硶锛屾垜浠氨鑳戒繚璇佹暣涓郴缁熷彲鐢?/span> 锛堝彲澶勭悊璇锋眰锛屾暟鎹竴鑷存€э級 銆傚悗闈㈡垜浠富瑕佺爺绌剁殑灏辨槸 raft 鏄浣曞仛鍒扮殑銆?/span>
棣栧厛鎴戜滑瑕佺煡閬擄紝Raft 绠楁硶灏嗗叾闂鍒掑垎涓?/span>
-
棰嗗閫変妇 -
鏃ュ織澶嶅埗 -
瀹夊叏鎬?/span>
瀵逛簬涓€涓泦缇ゅ彧鏈変竴涓?/span> leader锛堥瀵硷級锛岄偅涔堟垜浠氨寰堝鏄撶悊瑙c€傚彧瑕侀瀵兼搷浣滃悓姝ュ埌瀵瑰簲鐨?/span> followers锛堣窡闅忚€咃級锛屾暟鎹繀鐒朵竴鑷淬€傚綋 leader 瀹曟満锛岄渶瑕佽繘琛岄瀵奸€変妇銆?/span>
鏃ュ織澶嶅埗鍏跺疄灏辨槸鍚屾鎿嶄綔鏁版嵁鐨勮繃绋嬨€俵eader 灏嗘搷浣滄棩蹇楀悓姝ュ埌鍏朵粬鑺傜偣銆?/span>
瀹夊叏鎬э細濡備綍瀹夊叏鐨勫悓姝ワ紝鍦ㄤ笉鍚岀殑鎯呭喌锛屾垜浠兘鑳戒繚璇佷竴鑷存€э紝杩欎篃灏辨槸瀹夊叏鎬ч渶瑕佽€冭檻鐨勯棶棰樸€?/span>
鍏跺疄灏辨槸濡傛锛宺aft 棣栧厛鍋囪浜嗛瀵奸€変妇銆傜劧鍚庡疄鐜颁簡鏃ュ織澶嶅埗锛屾渶鍚庡湪瀹夊叏闂涓婅В鍐充笂闈㈢殑婕忔礊闂銆?
涓夈€侀瀵奸€変妇
鐩殑锛氬綋闆嗙兢鍒濆鍖栨垨鑰呴瀵?gg 鐨勬椂鍊欓€夊嚭涓€涓柊鐨勯瀵笺€傛瘯绔熶竴涓泦缇や笉鑳芥病鏈夐瀵硷紝濡傛灉娌℃湁锛岄偅涔堣繖涓泦缇ゅ氨涓嶅彲鐢ㄤ簡銆?/span>
瑙﹀彂鏈哄埗锛氶€氳繃蹇冭烦銆?/span>
杩欏箙鍥惧睍鐜颁簡璺熼殢鑰呫€佸€欓€夎€呭拰棰嗗鑰呬箣闂寸殑鐘舵€佽浆鎹㈠叧绯伙紝涓嬮潰涓昏浠嬬粛浠栦滑鐨勮浆鎹㈡祦绋嬨€?/span>
璺熼殢鑰?/span>
濡傛灉浠栬兘鎸佺画浠庨瀵艰€呮垨鑰呭€欓€夎€呮帴鏀跺埌鏈夋晥鐨?/span> RPCs锛岄偅涔堜粬鐨勭姸鎬佸氨涓嶄細鍙樸€備竴鐩翠繚鎸佽窡闅忚€呰韩浠姐€備絾濡傛灉娌℃湁鎸佺画鎺ュ彈锛屼篃灏辨槸鍦ㄤ竴娈垫椂闂存病鏀跺埌鏈夋晥鐨?/span> RPCs锛岄偅灏遍€変妇瓒呮椂锛屼粬浼氬彉鎴愬€欓€夎€呫€?/span>
鍊欓€夎€?/span>
瑕佸彉涓哄€欓€夎€咃紝涔熷氨鎰忓懗鐫€浠栬寮€鍚竴杞柊鐨勯€変妇銆傞偅涔堣窡闅忚€呬細澧炲姞鑷繁鐨勪换鏈熷彿锛岃浆涓哄€欓€夎€呫€?/span>
鎴愪负鍊欓€夎€呭悗锛岃嚜宸变細骞惰鍙戦€佷竴涓负鑷繁鎶曠エ鐨?/span> RPCs 璇锋眰 缁欏叾浠栨湇鍔″櫒銆?/span>
鎴愪负鍊欓€夎€呯殑鏁翠釜杩囩▼涔熶細淇濇寔褰撳墠鐘舵€侊紝鐭ラ亾婊¤冻涓嬮潰涓変釜鏉′欢
-
浠栬耽寰楅€変妇锛岃浆鍙樹负棰嗗鍒?/span> -
鍏朵粬鑺傜偣璧簡锛屼粬杞负璺熼殢鑰?/span> -
涓€娈垫椂闂存病鏈変换浣曚汉鑾疯儨銆傝鏄庨€夌エ琚摐鍒嗭紝閲嶅鎵ц銆?/span>
杩欓噷闇€瑕佹敞鎰忕殑鐐癸細
1. 瀵逛簬鍚屼竴浠绘湡鍙凤紝姣忎釜鑺傜偣涓€浼氭姇涓€寮犵エ銆傛瘮濡傛湇鍔″櫒 A 浣滀负鍊欓€夎€呯敓鎴愪簡缂栧彿涓?5 鐨勪换鏈熷彿锛岄偅涔堝鏋滄帴鏀跺埌鍏朵粬鑺傜偣鐨勭紪鍙蜂负 5 鐨勪换鏈熷彿鐨勬姇绁ㄨ姹傦紝浠栦細蹇界暐銆傝繖涓繃绋嬮伒寰殑鏄厛鏉ュ厛鎶曠殑鍘熷垯銆?br>2. 鍊欓€夎€呮帴鏀跺埌棰嗗鑰呯殑澹版槑銆備細鍒ゅ0鏄庝腑 RPC 鐨勪换鏈熷彿锛屽鏋滄瘮鑷繁鐨勮繕灏忥紝閭d箞浠栬繕浼氫繚鎸佸€欓€夎€呰韩浠斤紝鍚﹀垯杞负璺熼殢鑰呫€?br>3. 瀵逛簬涓婇潰绗笁鐐癸紝閲嶅鎵ц锛孯aft 閲囩敤闅忔満瓒呮椂閫変妇鏃堕棿杩涜浜嗕紭鍖栥€傞檷浣庨€夌エ鐡滃垎鐨勫彲鑳芥€с€?/span>
鍥涖€丷aft 鏃ュ織澶嶅埗
鐩存帴鍘荤悊瑙f棩蹇楀鍒讹紝鏄緢瀹规槗鐨勶紝瀹㈡埛绔殑涓€鏉℃寚浠ゅ埌杈撅紝棰嗗鑰呬細涓鸿繖鏉℃寚浠ゅ垱寤轰竴鏉℃棩蹇楁潯鐩紝骞朵笖骞惰鍙戦€佸埌鍏朵粬璺熼殢鑰呫€傚綋鏃ュ織琚畨鍏ㄥ鍒?/span>锛堟墍璋撳畨鍏ㄥ鍒跺悗闈細鏈夛級锛岄瀵间細灏嗘棩蹇楀簲鐢ㄥ埌鐘舵€佹満锛堟瘮濡傚鏋滄槸 mysql 鐨?insert锛岄偅涔堝氨鏄墽琛?insert 鎿嶄綔锛?/span>锛岀劧鍚庡搷搴斿鎴风銆?/span>
1. 濡傛灉鍦ㄤ笉鍚岀殑鏃ュ織涓殑涓や釜鏉$洰鎷ユ湁鐩稿悓鐨勭储寮曞拰浠绘湡鍙凤紝閭d箞浠栦滑瀛樺偍浜嗙浉鍚岀殑鎸囦护銆?br>2. 濡傛灉鍦ㄤ笉鍚岀殑鏃ュ織涓殑涓や釜鏉$洰鎷ユ湁鐩稿悓鐨勭储寮曞拰浠绘湡鍙凤紝閭d箞浠栦滑涔嬪墠鐨勬墍鏈夋棩蹇楁潯鐩篃鍏ㄩ儴鐩稿悓銆?/span>
绗簩鐐癸細瑕佹兂鍏ㄩ儴鐩稿悓锛屽氨瑕佷繚璇佽窡闅忚€呭緱鍒扮殑鏃ュ織鏄瀵艰€呭彂閫佺殑椤哄簭闄勫姞涓婂幓鐨勩€傞瀵艰€呭湪鍙戦€佹柊鐨勬棩蹇楁椂锛屼細闄勫姞杩欐潯鏃ュ織涔嬪墠鏃ュ織鐨勭储寮曞拰浠绘湡鍙枫€傚鏋滆窡闅忚€呭彂鐜版暟鎹尮閰嶏紝鎵嶄細闄勫姞涓婂幓锛屽惁鍒欐嫆缁濄€傚氨鏄竴涓釜鐘舵€佷繚璇佷簡鏃ュ織鐨勫尮閰嶇壒鎬с€?/span>
瀵逛簬鏃ュ織涓嶄竴鑷寸殑鐜拌薄锛宺aft 鏄€氳繃璺熼殢鑰呭己鍒跺鍒堕瀵艰€呯殑鏃ュ織鏉ヤ繚璇佺殑銆?/span>
鍏跺疄灏辨槸閫氳繃鏃ュ織瑕嗙洊瑙e喅銆備絾鏄浜庢棩蹇楄鐩栵紝鎴戜滑灏变細鎯冲埌涓€涓棶棰橈紝浼氫笉浼氳鐩栧凡缁忔彁浜ょ殑鏃ュ織 锛堟棩蹇楀搴旀寚浠ゅ凡缁忚繑鍥炵粰瀹㈡埛绔級 銆傞偅褰撶劧涓嶄細锛屽鏋滅湡鏈夎繖鏍凤紝灏变細鏈変笉涓€鑷达紝鎴栬€呮寚浠や涪澶辩幇璞°€?/span>
浜斻€佸畨鍏ㄦ€?/span>
鍏跺疄瑙e喅杩欎釜鍔炴硶寰堢畝鍗曪紝灏辨槸鍦ㄩ瀵奸€変妇鐨勬椂鍊欙紝鍙兘璁╁畨鍏ㄧ殑鑺傜偣褰?leader锛屾墍璋撳畨鍏紝灏辨槸瀵瑰簲鑺傜偣鎷ユ湁褰撳墠棰嗗鑰呭凡缁忔彁浜ょ殑鎵€鏈夋棩蹇椼€俁aft 灏辨槸杩欎箞鍋氱殑銆?
1. 鏈€鍚庝竴鏉℃棩蹇楃殑浠绘湡鍙枫€傚鏋滃ぇ璇存槑鏂般€傚鏋滃皬锛岃鏄庝笉鏂般€傚鏋滅浉绛夈€傝烦鍒?
2. 鍒ゆ柇绱㈠紩闀垮害銆傚ぇ鐨勬洿鏂般€?/span>
鎵€浠?raft 姘歌繙涓嶄細閫氳繃璁$畻鍓湰鏁扮洰鐨勬柟寮?/span> 锛堝ぇ澶氭暟瀛樺湪锛?/span> 鍘绘彁浜や竴涓箣鍓嶄换鏈熷唴 锛堜换鏈?2锛?/span> 鐨勬棩蹇楁潯鐩€傚彧鏈夐瀵间汉褰撳墠浠绘湡閲岀殑鏃ュ織鏉$洰閫氳繃璁$畻鍓湰鏁扮洰鍙互琚彁浜?/span> 锛坋 闃舵锛?/span> 銆傝繖鏍蜂箣鍓嶄换鏈熺殑鏁版嵁涔熶細琚彁浜ゃ€?/span>
鍏€佽窡闅忚€呭拰鍊欓€変汉瀹曟満
鎵€浠ュ浜庢瘡涓?/span> RPCs 锛屽仛鍒板箓绛夊拰鏃犻檺閲嶈瘯锛屽湪鑺傜偣鎭㈠鍚庯紝灏辫繕鏄細淇濊瘉涓€鑷存€х姸鎬併€?/span>
涓冦€丷aft 闆嗙兢鎴愬憳鍙樺寲
濡傚浘锛岃摑鑹蹭唬琛ㄦ柊鐨勯厤缃€傜豢鑹蹭唬琛ㄨ€佺殑閰嶇疆銆侽ld 鐘舵€佹湁涓夊彴鏈哄櫒 Server1銆?銆?銆侼ew 鍔犲叆涓ゅ彴 server4銆?銆?
姣斿 server1銆? 浣跨敤鑰侀厤缃紝閭d箞1鍜?閮芥湁鍙兘琚綋閫変负 leader 銆?/span> 3銆?銆? 浣跨敤蹇冨緱閰嶇疆锛屼粬浠箣涓殑涓€涓篃浼氳褰撻€変负 leader 銆?/span>
瀵逛簬 C锛坥ld銆乶ew锛夐厤缃紝鍏跺疄灏辨槸鍙湁鍚屾椂婊¤冻 old 鍜?new 閰嶇疆鐨勬椂鍊欐墠浼氱敓鏁堛€?
鍏€佷釜浜烘兂娉曟€荤粨
瀵逛簬 raft 鐨勮璁★紝鍏朵腑浠绘湡杩欎釜姒傚康鏄潪甯哥粷濡欑殑銆傛棤璁烘槸鍦ㄩ瀵间汉閫変妇锛屾棩蹇楀鍒剁殑鏃跺€欙紝閮芥槸闈炲父渚濊禆浠绘湡鐨勩€?/span>
鐒跺悗 raft 閫氳繃鍊欓€変汉鐨勬渶鍚庢棩蹇楁潯鐩殑绱㈠紩鍊煎拰鍊欓€変汉鏈€鍚庢棩蹇楁潯鐩殑浠绘湡鍙锋潵鍐冲畾鏄惁鑳藉綋閫夈€?/span>
以上是关于鑱婅亰 Raft 绠楁硶的主要内容,如果未能解决你的问题,请参考以下文章