Nacos配置中心交互模型是 push 還是 pull ?你應該這麼回答
{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文案例收錄在 https://github.com/chengxy-nds/Springboot-Notebook","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大家好,我是小富~","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"大家應該都不太陌生,出身阿里名聲在外,能做動態服務發現、配置管理,非常好用的一個工具。然而這樣的技術用的人越多面試被問的概率也就越大,如果只停留在使用層面,那面試可能要喫大虧。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如我們今天要討論的話題,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"在做配置中心的時候,配置數據的交互模式是服務端推過來還是客戶端主動拉的?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8c/8c824870e269bb1a96e51a9dfec3d06a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏我先拋出答案:客戶端主動拉的!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來咱們扒一扒","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"的源碼,來看看它具體是如何實現的?","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"配置中心","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"聊","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"之前簡單回顧下配置中心的由來。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"簡單理解配置中心的作用就是對配置統一管理,修改配置後應用可以動態感知,而無需重啓。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲在傳統項目中,大多都採用靜態配置的方式,也就是把配置信息都寫在應用內的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"yml","attrs":{}}],"attrs":{}},{"type":"text","text":"或","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"properties","attrs":{}}],"attrs":{}},{"type":"text","text":"這類文件中,如果要想修改某個配置,通常要重啓應用纔可以生效。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但有些場景下,比如我們想要在應用運行時,通過修改某個配置項,實時的控制某一個功能的開閉,頻繁的重啓應用肯定是不能接受的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"尤其是在微服務架構下,我們的應用服務拆分的粒度很細,少則幾十多則上百個服務,每個服務都會有一些自己特有或通用的配置。假如此時要改變通用配置,難道要我挨個改幾百個服務配置?很顯然這不可能。所以爲了解決此類問題配置中心應運而生。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/05/055b7a89bd97e0eae765b2244a4ca374.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"推與拉模型","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端與配置中心的數據交互方式其實無非就兩種,要麼推","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"push","attrs":{}}],"attrs":{}},{"type":"text","text":",要麼拉","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"pull","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"推模型","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端與服務端建立","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TCP","attrs":{}}],"attrs":{}},{"type":"text","text":"長連接,當服務端配置數據有變動,立刻通過建立的長連接將數據推送給客戶端。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優勢:長鏈接的優點是實時性,一旦數據變動,立即推送變更數據給客戶端,而且對於客戶端而言,這種方式更爲簡單,只建立連接接收數據,並不需要關心是否有數據變更這類邏輯的處理。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"弊端:長連接可能會因爲網絡問題,導致不可用,也就是俗稱的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"假死","attrs":{}}],"attrs":{}},{"type":"text","text":"。連接狀態正常,但實際上已無法通信,所以要有的心跳機制","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"KeepAlive","attrs":{}}],"attrs":{}},{"type":"text","text":"來保證連接的可用性,纔可以保證配置數據的成功推送。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"拉模型","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端主動的向服務端發請求拉配置數據,常見的方式就是輪詢,比如每3s向服務端請求一次配置數據。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"輪詢的優點是實現比較簡單。但弊端也顯而易見,輪詢無法保證數據的實時性,什麼時候請求?間隔多長時間請求一次?都是不得不考慮的問題,而且輪詢方式對服務端還會產生不小的壓力。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"長輪詢","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"開篇我們就給出了答案,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"採用的是客戶端主動拉","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"pull","attrs":{}}],"attrs":{}},{"type":"text","text":"模型,應用長輪詢(","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Long Polling","attrs":{}}],"attrs":{}},{"type":"text","text":")的方式來獲取配置數據。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"額?以前只聽過輪詢,長輪詢又是什麼鬼?它和傳統意義上的輪詢(暫且叫短輪詢吧,方便比較)有什麼不同呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"短輪詢","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不管服務端配置數據是否有變化,不停的發起請求獲取配置,比如支付場景中前段JS輪詢訂單支付狀態。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣的壞處顯而易見,由於配置數據並不會頻繁變更,若是一直髮請求,勢必會對服務端造成很大壓力。還會造成推送數據的延遲,比如:每10s請求一次配置,如果在第11s時配置更新了,那麼推送將會延遲9s,等待下一次請求。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/26/2682e2cf5eb03137c3d04727aa04efaa.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了解決短輪詢的問題,有了長輪詢方案。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"長輪詢","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"長輪詢可不是什麼新技術,它不過是由服務端控制響應客戶端請求的返回時間,來減少客戶端無效請求的一種優化手段,其實對於客戶端來說與短輪詢的使用並沒有本質上的區別。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端發起請求後,服務端不會立即返回請求結果,而是將請求掛起等待一段時間,如果此段時間內服務端數據變更,立即響應客戶端請求,若是一直無變化則等到指定的超時時間後響應請求,客戶端重新發起長鏈接。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/78/786cbb59a3753a11b5c74ecdd9399b13.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Nacos初識","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了後續演示操作方便我在本地搭了個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"注意:","attrs":{}},{"type":"text","text":" 運行時遇到個小坑,由於","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"默認是以","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"cluster","attrs":{}}],"attrs":{}},{"type":"text","text":"集羣的方式啓動,而本地搭建通常是單機模式","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"standalone","attrs":{}}],"attrs":{}},{"type":"text","text":",這裏需手動改一下啓動腳本","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"startup.X","attrs":{}}],"attrs":{}},{"type":"text","text":"中的啓動模式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fb/fbb7da470fe760067d7965ca5ab201e2.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"直接執行","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"/bin/startup.X","attrs":{}}],"attrs":{}},{"type":"text","text":"就可以了,默認用戶密碼均是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/00/006d1cd64d76537e89faa1d9b992a683.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"幾個概念","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"配置中心的幾個核心概念:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dataId","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"group","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"namespace","attrs":{}}],"attrs":{}},{"type":"text","text":",它們的層級關係如下圖:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/03/039a73915d949373554d7609c47473ca.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"dataId","attrs":{}}],"attrs":{}},{"type":"text","text":":是配置中心裏最基礎的單元,它是一種","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"key-value","attrs":{}}],"attrs":{}},{"type":"text","text":"結構,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"key","attrs":{}}],"attrs":{}},{"type":"text","text":"通常是我們的配置文件名稱,比如:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"application.yml","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mybatis.xml","attrs":{}}],"attrs":{}},{"type":"text","text":",而","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"value","attrs":{}}],"attrs":{}},{"type":"text","text":"是整個文件下的內容。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前支持","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"JSON","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"XML","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"YAML","attrs":{}}],"attrs":{}},{"type":"text","text":"等多種配置格式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/55/55393c35b94ff8970f39c2ebdadfa4bd.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"group","attrs":{}}],"attrs":{}},{"type":"text","text":":dataId配置的分組管理,比如同在dev環境下開發,但同環境不同分支需要不同的配置數據,這時就可以用分組隔離,默認分組","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DEFAULT_GROUP","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"namespace","attrs":{}}],"attrs":{}},{"type":"text","text":":項目開發過程中肯定會有","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dev","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"test","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"pro","attrs":{}}],"attrs":{}},{"type":"text","text":"等多個不同環境,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"namespace","attrs":{}}],"attrs":{}},{"type":"text","text":"則是對不同環境進行隔離,默認所有配置都在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public","attrs":{}}],"attrs":{}},{"type":"text","text":"裏。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"架構設計","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下圖簡要描述了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"配置中心的架構流程。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端、控制檯通過發送Http請求將配置數據註冊到服務端,服務端持久化數據到Mysql。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端拉取配置數據,並批量設置對","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dataId","attrs":{}}],"attrs":{}},{"type":"text","text":"的監聽發起長輪詢請求,如服務端配置項變更立即響應請求,如無數據變更則將請求掛起一段時間,直到達到超時時間。爲減少對服務端壓力以及保證配置中心可用性,拉取到配置數據客戶端會保存一份快照在本地文件中,優先讀取。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fb/fbd2068955c4b154ec6d18bc3e2e7c93.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏我省略了比較多的細節,如鑑權、負載均衡、高可用方面的設計(其實這部分纔是真正值得學的,後邊另出文講吧),主要弄清客戶端與服務端的數據交互模式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下邊我們以Nacos 2.0.1版本源碼分析,2.0以後的版本改動較多,和網上的很多資料略有些不同地址:https://github.com/alibaba/nacos/releases/tag/2.0.1","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"客戶端源碼分析","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Nacos","attrs":{}}],"attrs":{}},{"type":"text","text":"配置中心的客戶端源碼在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"nacos-client","attrs":{}}],"attrs":{}},{"type":"text","text":"項目,其中","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"NacosConfigService","attrs":{}}],"attrs":{}},{"type":"text","text":"實現類是所有操作的核心入口。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"說之前先了解個客戶端數據結構","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"cacheMap","attrs":{}}],"attrs":{}},{"type":"text","text":",這裏大家重點記住它,因爲它幾乎貫穿了Nacos客戶端的所有操作,由於存在多線程場景爲保證數據一致性,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"cacheMap","attrs":{}}],"attrs":{}},{"type":"text","text":"採用了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"AtomicReference","attrs":{}}],"attrs":{}},{"type":"text","text":"原子變量實現。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"/**\n * groupKey -> cacheData.\n */\nprivate final AtomicReference
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.