七張圖完全講清楚ZooKeeper分佈式鎖的實現原理【石杉的架構筆記】

歡迎關注微信公衆號:石杉的架構筆記(id:shishan100)node

個人新課**《C2C 電商系統微服務架構120天實戰訓練營》在公衆號儒猿技術窩**上線了,感興趣的同窗,能夠點擊下方連接瞭解詳情:面試

《C2C 電商系統微服務架構120天實戰訓練營》算法

1、寫在前面

以前寫過一篇文章(《拜託,面試請不要再問我Redis分佈式鎖的實現原理》),給你們說了一下Redisson這個開源框架是如何實現Redis分佈式鎖原理的,這篇文章再給你們聊一下ZooKeeper實現分佈式鎖的原理。性能優化

同理,我是直接基於比較經常使用的Curator這個開源框架,聊一下這個框架對ZooKeeper(如下簡稱zk)分佈式鎖的實現。微信

通常除了大公司是自行封裝分佈式鎖框架以外,建議你們用這些開源框架封裝好的分佈式鎖實現,這是一個比較快捷省事兒的方式。markdown

2、ZooKeeper分佈式鎖機制

接下來咱們一塊兒來看看,多客戶端獲取及釋放zk分佈式鎖的整個流程及背後的原理。架構

首先你們看看下面的圖,若是如今有兩個客戶端一塊兒要爭搶zk上的一把分佈式鎖,會是個什麼場景?併發

若是你們對zk還不太瞭解的話,建議先自行百度一下,簡單瞭解點基本概念,好比zk有哪些節點類型等等。app

參見上圖。zk裏有一把鎖,這個鎖就是zk上的一個節點。而後呢,兩個客戶端都要來獲取這個鎖,具體是怎麼來獲取呢?框架

我們就假設客戶端A搶先一步,對zk發起了加分佈式鎖的請求,這個加鎖請求是用到了zk中的一個特殊的概念,叫作**「臨時順序節點」。**

簡單來講,就是直接在"my_lock"這個鎖節點下,建立一個順序節點,這個順序節點有zk內部自行維護的一個節點序號。

好比說,第一個客戶端來搞一個順序節點,zk內部會給起個名字叫作:xxx-000001。而後第二個客戶端來搞一個順序節點,zk可能會起個名字叫作:xxx-000002。你們注意一下,最後一個數字都是依次遞增的,從1開始逐次遞增。zk會維護這個順序。

因此這個時候,假如說客戶端A先發起請求,就會搞出來一個順序節點,你們看下面的圖,Curator框架大概會弄成以下的樣子:

你們看,客戶端A發起一個加鎖請求,先會在你要加鎖的node下搞一個臨時順序節點,這一大坨長長的名字都是Curator框架本身生成出來的。

而後,那個最後一個數字是"1"。你們注意一下,由於客戶端A是第一個發起請求的,因此給他搞出來的順序節點的序號是"1"。

接着客戶端A建立完一個順序節點。還沒完,他會查一下"my_lock"這個鎖節點下的全部子節點,而且這些子節點是按照序號排序的,這個時候他大概會拿到這麼一個集合:

接着客戶端A會走一個關鍵性的判斷,就是說:唉!兄弟,這個集合裏,我建立的那個順序節點,是否是排在第一個啊?

若是是的話,那我就能夠加鎖了啊!由於明明我就是第一個來建立順序節點的人,因此我就是第一個嘗試加分佈式鎖的人啊!

bingo!加鎖成功!你們看下面的圖,再來直觀的感覺一下整個過程。

接着假如說,客戶端A都加完鎖了,客戶端B過來想要加鎖了,這個時候他會幹同樣的事兒:先是在"my_lock"這個鎖節點下建立一個臨時順序節點,此時名字會變成相似於:

你們看看下面的圖:

客戶端B由於是第二個來建立順序節點的,因此zk內部會維護序號爲"2"。

接着客戶端B會走加鎖判斷邏輯,查詢"my_lock"鎖節點下的全部子節點,按序號順序排列,此時他看到的相似於:

同時檢查本身建立的順序節點,是否是集合中的第一個?

明顯不是啊,此時第一個是客戶端A建立的那個順序節點,序號爲"01"的那個。因此加鎖失敗

加鎖失敗了之後,客戶端B就會經過ZK的API對他的順序節點的**上一個順序節點加一個監聽器。**zk自然就能夠實現對某個節點的監聽。

若是你們還不知道zk的基本用法,能夠百度查閱,很是的簡單。客戶端B的順序節點是:

他的上一個順序節點,不就是下面這個嗎?

即客戶端A建立的那個順序節點!

因此,客戶端B會對:

這個節點加一個監聽器,監聽這個節點是否被刪除等變化!你們看下面的圖。

接着,客戶端A加鎖以後,可能處理了一些代碼邏輯,而後就會釋放鎖。那麼,釋放鎖是個什麼過程呢?

其實很簡單,就是把本身在zk裏建立的那個順序節點,也就是:

這個節點給刪除。

刪除了那個節點以後,zk會負責通知監聽這個節點的監聽器,也就是客戶端B以前加的那個監聽器,說:兄弟,你監聽的那個節點被刪除了,有人釋放了鎖。

此時客戶端B的監聽器感知到了上一個順序節點被刪除,也就是排在他以前的某個客戶端釋放了鎖。

此時,就會通知客戶端B從新嘗試去獲取鎖,也就是獲取"my_lock"節點下的子節點集合,此時爲:

集合裏此時只有客戶端B建立的惟一的一個順序節點了!

而後呢,客戶端B判斷本身竟然是集合中的第一個順序節點,bingo!能夠加鎖了!直接完成加鎖,運行後續的業務代碼便可,運行完了以後再次釋放鎖。

3、總結

其實若是有客戶端C、客戶端D等N個客戶端爭搶一個zk分佈式鎖,原理都是相似的。

  • 你們都是上來直接建立一個鎖節點下的一個接一個的臨時順序節點
  • 若是本身不是第一個節點,就對本身上一個節點加監聽器
  • 只要上一個節點釋放鎖,本身就排到前面去了,至關因而一個排隊機制。

並且用臨時順序節點的另一個用意就是,若是某個客戶端建立臨時順序節點以後,不當心本身宕機了也不要緊,zk感知到那個客戶端宕機,會自動刪除對應的臨時順序節點,至關於自動釋放鎖,或者是自動取消本身的排隊。

最後,我們來看下用Curator框架進行加鎖和釋放鎖的一個過程:

其實用開源框架就是這點好,方便。這個Curator框架的zk分佈式鎖的加鎖和釋放鎖的實現原理,其實就是上面咱們說的那樣子。

可是若是你要手動實現一套那個代碼的話。仍是有點麻煩的,要考慮到各類細節,異常處理等等。因此你們若是考慮用zk分佈式鎖,能夠參考下本文的思路。

END

若有收穫,請幫忙轉發,您的鼓勵是做者最大的動力,謝謝!

一大波微服務、分佈式、高併發、高可用的原創系列文章正在路上

歡迎掃描下方二維碼,持續關注:

石杉的架構筆記(id:shishan100)

十餘年BAT架構經驗傾囊相授

**> **推薦閱讀:** > > 一、[拜託!面試請不要再問我Spring Cloud底層原理](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Flink.juejin.im%252525252F%252525253Ftarget%252525253Dhttps%25252525253A%25252525252F%25252525252Flink.juejin.im%25252525252F%25252525253Ftarget%25252525253Dhttps%2525252525253A%2525252525252F%2525252525252Fjuejin.im%2525252525252Fpost%2525252525252F5be13b83f265da6116393fc7) > > 二、[【雙11狂歡的背後】微服務註冊中心如何承載大型系統的千萬級訪問?](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Flink.juejin.im%252525252F%252525253Ftarget%252525253Dhttps%25252525253A%25252525252F%25252525252Flink.juejin.im%25252525252F%25252525253Ftarget%25252525253Dhttps%2525252525253A%2525252525252F%2525252525252Fjuejin.im%2525252525252Fpost%2525252525252F5be3f8dcf265da613a5382ca) > > 三、[【性能優化之道】每秒上萬併發下的Spring Cloud參數優化實戰](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Flink.juejin.im%252525252F%252525253Ftarget%252525253Dhttps%25252525253A%25252525252F%25252525252Flink.juejin.im%25252525252F%25252525253Ftarget%25252525253Dhttps%2525252525253A%2525252525252F%2525252525252Fjuejin.im%2525252525252Fpost%2525252525252F5be83e166fb9a049a7115580) > > 四、[微服務架構如何保障雙11狂歡下的99.99%高可用](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Flink.juejin.im%252525252F%252525253Ftarget%252525253Dhttps%25252525253A%25252525252F%25252525252Flink.juejin.im%25252525252F%25252525253Ftarget%25252525253Dhttps%2525252525253A%2525252525252F%2525252525252Fjuejin.im%2525252525252Fpost%2525252525252F5be99a68e51d4511a8090440) > > 五、[兄弟,用大白話告訴你小白都能聽懂的Hadoop架構原理](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Flink.juejin.im%252525252F%252525253Ftarget%252525253Dhttps%25252525253A%25252525252F%25252525252Flink.juejin.im%25252525252F%25252525253Ftarget%25252525253Dhttps%2525252525253A%2525252525252F%2525252525252Fjuejin.im%2525252525252Fpost%2525252525252F5beaf02ce51d457e90196069) > > 六、[大規模集羣下Hadoop NameNode如何承載每秒上千次的高併發訪問](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Flink.juejin.im%252525252F%252525253Ftarget%252525253Dhttps%25252525253A%25252525252F%25252525252Flink.juejin.im%25252525252F%25252525253Ftarget%25252525253Dhttps%2525252525253A%2525252525252F%2525252525252Fjuejin.im%2525252525252Fpost%2525252525252F5bec278c5188253e64332c76) > > 七、【[性能優化的祕密】Hadoop如何將TB級大文件的上傳性能優化上百倍](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Flink.juejin.im%252525252F%252525253Ftarget%252525253Dhttps%25252525253A%25252525252F%25252525252Flink.juejin.im%25252525252F%25252525253Ftarget%25252525253Dhttps%2525252525253A%2525252525252F%2525252525252Fjuejin.im%2525252525252Fpost%2525252525252F5bed82a9e51d450f9461cfc7) > > 八、[拜託,面試請不要再問我TCC分佈式事務的實現原理](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Flink.juejin.im%252525252F%252525253Ftarget%252525253Dhttps%25252525253A%25252525252F%25252525252Fjuejin.im%25252525252Fpost%25252525252F5bf201f7f265da610f63528a)[坑爹呀!](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Fjuejin.im%252525252Fpost%252525252F5bf2c6b6e51d456693549af4) > > 九、[【坑爹呀!】最終一致性分佈式事務如何保障實際生產中99.99%高可用?](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Fjuejin.im%252525252Fpost%252525252F5bf2c6b6e51d456693549af4) > > 十、[拜託,面試請不要再問我Redis分佈式鎖的實現原理!](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Fjuejin.im%252525252Fpost%252525252F5bf3f15851882526a643e207) > > **十一、****[【眼前一亮!】看Hadoop底層算法如何優雅的將大規模集羣性能提高10倍以上?](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Flink.juejin.im%2525253Ftarget%2525253Dhttps%252525253A%252525252F%252525252Fjuejin.im%252525252Fpost%252525252F5bf5396f51882509a768067e)** > > **十二、****[億級流量系統架構之如何支撐百億級數據的存儲與計算](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Flink.juejin.im%25252F%25253Ftarget%25253Dhttps%2525253A%2525252F%2525252Fjuejin.im%2525252Fpost%2525252F5bfab59fe51d4551584c7bcf)** > > 1三、[億級流量系統架構之如何設計高容錯分佈式計算系統](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Flink.juejin.im%253Ftarget%253Dhttps%25253A%25252F%25252Fjuejin.im%25252Fpost%25252F5bfbeeb9f265da61407e9679) > > 1四、[億級流量系統架構之如何設計承載百億流量的高性能架構](https://link.juejin.im/?target=https%3A%2F%2Flink.juejin.im%3Ftarget%3Dhttps%253A%252F%252Fjuejin.im%252Fpost%252F5bfd2df1e51d4574b133dd3a) > > 1五、[億級流量系統架構之如何設計每秒十萬查詢的高併發架構](https://link.juejin.im/?target=https%3A%2F%2Fjuejin.im%2Fpost%2F5bfe771251882509a7681b3a) > > 1六、[億級流量系統架構之如何設計全鏈路99.99%高可用架構](https://juejin.cn/post/6844903727699230734)**
相關文章
相關標籤/搜索