大型分佈式 Web 系統的架構演進

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"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":"Java Web","attrs":{}}],"attrs":{}},{"type":"text","text":" 爲例,來搭建一個簡單的電商系統,看看這個系統可以如何一步步演變。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"該系統具備的功能:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用戶模塊:用戶註冊和管理","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"商品模塊:商品展示和管理","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"交易模塊:創建交易和管理","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"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":"Tomcat","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Jetty","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Jboss","attrs":{}}],"attrs":{}},{"type":"text","text":",然後直接使用JSP/Servlet技術,或者使用一些開源的框架如","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Maven + Spring + Struts + Hibernate","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Maven + Spring + Spring MVC + Mybatis","attrs":{}}],"attrs":{}},{"type":"text","text":"。最後再選擇一個數據庫管理系統來存儲數據,如","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MySQL","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SqlServer","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Oracle","attrs":{}}],"attrs":{}},{"type":"text","text":",然後通過","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"JDBC","attrs":{}}],"attrs":{}},{"type":"text","text":"進行數據庫的連接和操作。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"把以上的所有軟件包括數據庫、應用程序都裝載同一臺機器上,應用跑起來了,也算是一個小系統了。此時系統結果如下:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/cb/cbfaece1de17183f2384e6920ce3b7fd.webp","alt":"圖片","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":"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":"text","marks":[{"type":"strong","attrs":{}}],"text":"代碼層面","attrs":{}},{"type":"text","text":" 已難以優化,在不提高單臺機器的性能的情況下,採用","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"增加機器","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":"text","marks":[{"type":"strong","attrs":{}}],"text":"數據庫服務器","attrs":{}},{"type":"text","text":" 和","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Web服務器","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9a/9a5b09c164f268e8c5e1a7e85de7a2a8.webp","alt":"圖片","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":"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":"text","marks":[{"type":"strong","attrs":{}}],"text":"一臺","attrs":{}},{"type":"text","text":" 變成了","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"兩臺甚至多臺","attrs":{}},{"type":"text","text":" ,把用戶的請求分散到不同的服務器中,從而提高負載能力。而","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"多臺應用服務器","attrs":{}},{"type":"text","text":" 之間沒有直接的交互,他們都是依賴數據庫各自對外提供服務。著名的做","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"故障切換","attrs":{}},{"type":"text","text":" 的軟件有","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"KeepAlived","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"KeepAlived","attrs":{}}],"attrs":{}},{"type":"text","text":"是一個類似於Layer3、4、7交換機制的軟件,他不是某個具體軟件故障切換的專屬品,而是可以適用於各種軟件的一款產品。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"KeepAlived","attrs":{}}],"attrs":{}},{"type":"text","text":"配合上","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ipvsadm","attrs":{}}],"attrs":{}},{"type":"text","text":"又可以做","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"負載均衡","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f3/f3a0b2e219e9c50fd9f0aa7614ad456c.webp","alt":"圖片","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":"text","marks":[{"type":"strong","attrs":{}}],"text":"四個問題","attrs":{}},{"type":"text","text":" :","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"用戶的請求由誰來轉發到到具體的應用服務器?","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"有那些轉發的算法和策略可以使用?","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"應用服務器如何返回用戶的請求?","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"用戶如果每次訪問到的服務器不一樣,那麼如何維護session的一致性?","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"針對以上問題,常用的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"解決方案","attrs":{}},{"type":"text","text":" 如下:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"1、負載均衡的問題","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般以下有5種解決方案:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1、HTTP重定向","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"HTTP","attrs":{}}],"attrs":{}},{"type":"text","text":"重定向就是應用層的請求轉發。用戶的請求其實已經到了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"HTTP","attrs":{}}],"attrs":{}},{"type":"text","text":"重定向負載均衡服務器,服務器根據算法要求用戶重定向,用戶收到重定向請求後,再次請求真正的集羣","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:簡單易用;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:性能較差。","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":"2、DNS域名解析負載均衡","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"DNS","attrs":{}}],"attrs":{}},{"type":"text","text":"域名解析負載均衡就是在用戶請求DNS服務器,獲取域名對應的IP地址時,DNS服務器直接給出負載均衡後的服務器IP。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:交給","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DNS","attrs":{}}],"attrs":{}},{"type":"text","text":",不用我們去維護負載均衡服務器;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:當一個應用服務器掛了,不能及時通知","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DNS","attrs":{}}],"attrs":{}},{"type":"text","text":",而且","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DNS","attrs":{}}],"attrs":{}},{"type":"text","text":"負載均衡的控制權在域名服務商那裏,網站無法做更多的改善和更強大的管理。","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":"3、反向代理服務器","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在用戶的請求到達反向代理服務器時(已經到達網站機房),由反向代理服務器根據算法轉發到具體的服務器。常用的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Apache","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"attrs":{}},{"type":"text","text":"都可以充當反向代理服務器。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:部署簡單;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:代理服務器可能成爲性能的瓶頸,特別是一次上傳大文件。","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":"4、IP層負載均衡","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在請求到達負載均衡器後,負載均衡器通過修改請求的目的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IP","attrs":{}}],"attrs":{}},{"type":"text","text":"地址,從而實現請求的轉發,做到負載均衡。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:性能更好;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:負載均衡器的寬帶成爲瓶頸。","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":"5、數據鏈路層負載均衡","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在請求到達負載均衡器後,負載均衡器通過修改請求的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MAC","attrs":{}}],"attrs":{}},{"type":"text","text":"地址,從而做到負載均衡,與","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IP","attrs":{}}],"attrs":{}},{"type":"text","text":"負載均衡不一樣的是,當請求訪問完服務器之後,直接返回客戶。而無需再經過負載均衡器。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"2、集羣調度轉發算法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1、rr輪詢調度算法","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"顧名思義,輪詢分發請求。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:實現簡單","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:不考慮每臺服務器的處理能力","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":"2、wrr加權調度算法","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們給每個服務器設置權值","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Weight","attrs":{}}],"attrs":{}},{"type":"text","text":",負載均衡調度器根據權值調度服務器,服務器被調用的次數跟權值成正比。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:考慮了服務器處理能力的不同","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":"3、sh原地址散列算法","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"提取用戶","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IP","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":"value","attrs":{}}],"attrs":{}},{"type":"text","text":",即目標服務器","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IP","attrs":{}}],"attrs":{}},{"type":"text","text":"。過目標機器超負荷,則返回空。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:實現同一個用戶訪問同一個服務器。","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":"4、dh目標地址散列算法","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原理同上,只是現在提取的是目標地址的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IP","attrs":{}}],"attrs":{}},{"type":"text","text":"來做哈希。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:實現同一個用戶訪問同一個服務器。","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":"5、lc最少連接算法","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優先把請求轉發給連接數少的服務器。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:使得集羣中各個服務器的負載更加均勻。","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":"6、wlc加權最少連接算法","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"lc","attrs":{}}],"attrs":{}},{"type":"text","text":"的基礎上,爲每臺服務器加上權值。算法爲:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(活動連接數 * 256 + 非活動連接數) ÷ 權重","attrs":{}}],"attrs":{}},{"type":"text","text":",計算出來的值小的服務器優先被選擇。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:可以根據服務器的能力分配請求。","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":"7、sed最短期望延遲算法","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實sed跟wlc類似,區別是不考慮非活動連接數。算法爲:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(活動連接數 +1 ) * 256 ÷ 權重","attrs":{}}],"attrs":{}},{"type":"text","text":",同樣計算出來的值小的服務器優先被選擇。","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":"8、nq永不排隊算法","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"改進的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sed","attrs":{}}],"attrs":{}},{"type":"text","text":"算法。我們想一下什麼情況下才能“永不排隊”,那就是服務器的連接數爲0的時候,那麼假如有服務器連接數爲0,均衡器直接把請求轉發給它,無需經過sed的計算。","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":"9、LBLC基於局部性最少連接算法","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"負載均衡器根據請求的目的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IP","attrs":{}}],"attrs":{}},{"type":"text","text":"地址,找出該","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IP","attrs":{}}],"attrs":{}},{"type":"text","text":"地址最近被使用的服務器,把請求轉發之。若該服務器超載,最採用最少連接數算法。","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":"10、LBLCR帶複製的基於局部性最少連接算法","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"負載均衡器根據請求的目的IP地址,找出該IP地址最近使用的“服務器組”,注意,並不是具體某個服務器,然後採用最少連接數從該組中挑出具體的某臺服務器出來,把請求轉發之。若該服務器超載,那麼根據最少連接數算法,在集羣的非本服務器組的服務器中,找出一臺服務器出來,加入本服務器組,然後把請求轉發。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"3、集羣請求返回模式問題","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1、NAT","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"負載均衡器接收用戶的請求,轉發給具體服務器,服務器處理完請求返回給均衡器,均衡器再重新返回給用戶。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2、DR","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"負載均衡器接收用戶的請求,轉發給具體服務器,服務器出來玩請求後直接返回給用戶。需要系統支持","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IP Tunneling","attrs":{}}],"attrs":{}},{"type":"text","text":"協議,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"難以跨平臺","attrs":{}},{"type":"text","text":" 。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3、TUN","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同上,但無需","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IP Tunneling","attrs":{}}],"attrs":{}},{"type":"text","text":"協議,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"跨平臺性好","attrs":{}},{"type":"text","text":" ,大部分系統都可以支持。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"4、集羣Session一致性問題","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1、Session Sticky","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Session sticky","attrs":{}}],"attrs":{}},{"type":"text","text":"就是把同一個用戶在某一個會話中的請求,都分配到固定的某一臺服務器中,這樣我們就不需要解決跨服務器的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"session","attrs":{}}],"attrs":{}},{"type":"text","text":"問題了,常見的算法有","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ip_hash","attrs":{}}],"attrs":{}},{"type":"text","text":"算法,即上面提到的兩種散列算法。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:實現簡單;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:應用服務器重啓則session消失。","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":"2、Session Replication","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Session replication","attrs":{}}],"attrs":{}},{"type":"text","text":"就是在集羣中複製","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"session","attrs":{}}],"attrs":{}},{"type":"text","text":",使得每個服務器都保存有全部用戶的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"session","attrs":{}}],"attrs":{}},{"type":"text","text":"數據。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:減輕負載均衡服務器的壓力,不需要要實現ip_hasp算法來轉發請求;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:複製時網絡帶寬開銷大,訪問量大的話","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Session","attrs":{}}],"attrs":{}},{"type":"text","text":"佔用內存大且浪費。","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":"3、Session數據集中存儲","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Session","attrs":{}}],"attrs":{}},{"type":"text","text":"數據集中存儲就是利用數據庫來存儲","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"session","attrs":{}}],"attrs":{}},{"type":"text","text":"數據,實現了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"session","attrs":{}}],"attrs":{}},{"type":"text","text":"和應用服務器的解耦。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:相比","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Session replication","attrs":{}}],"attrs":{}},{"type":"text","text":"的方案,集羣間對於寬帶和內存的壓力大幅減少;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:需要維護存儲","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Session","attrs":{}}],"attrs":{}},{"type":"text","text":"的數據庫。","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":"4、Cookie Base","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Cookie base","attrs":{}}],"attrs":{}},{"type":"text","text":"就是把","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Session","attrs":{}}],"attrs":{}},{"type":"text","text":"存在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cookie","attrs":{}}],"attrs":{}},{"type":"text","text":"中,由瀏覽器來告訴應用服務器我的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"session","attrs":{}}],"attrs":{}},{"type":"text","text":"是什麼,同樣實現了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"session","attrs":{}}],"attrs":{}},{"type":"text","text":"和應用服務器的解耦。","attrs":{}}]}],"attrs":{}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:實現簡單,基本免維護。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:cookie長度限制,安全性低,帶寬消耗。","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":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"attrs":{}},{"type":"text","text":"目前支持的負載均衡算法有","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"wrr","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sh","attrs":{}}],"attrs":{}},{"type":"text","text":"(支持一致性哈希)、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"fair","attrs":{}}],"attrs":{}},{"type":"text","text":"(lc)。但","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"attrs":{}},{"type":"text","text":"作爲均衡器的話,還可以一同作爲","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"靜態資源服務器","attrs":{}},{"type":"text","text":" 。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Keepalived + ipvsadm","attrs":{}}],"attrs":{}},{"type":"text","text":"比較強大,目前支持的算法有:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"rr","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"wrr","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"lc","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"wlc","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"lblc","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sh","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dh","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Keepalived","attrs":{}}],"attrs":{}},{"type":"text","text":"支持集羣模式有:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"NAT","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DR","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TUN","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Nginx","attrs":{}}],"attrs":{}},{"type":"text","text":"本身並沒有提供","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"session","attrs":{}}],"attrs":{}},{"type":"text","text":"同步的解決方案,而","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Apache","attrs":{}}],"attrs":{}},{"type":"text","text":"則提供了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"session","attrs":{}}],"attrs":{}},{"type":"text","text":"共享的支持。","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c4/c40b35051a3d03daf1d7dc2e8e03ae80.webp","alt":"圖片","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":"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":"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":"但對於數據庫來說,並沒有那麼簡單。假如我們簡單的把數據庫一分爲二,然後對於數據庫的請求,分別負載到A機器和B機器,那麼顯而易見會造成兩臺數據庫數據不統一的問題。那麼對於這種情況,我們可以先考慮使用","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"讀寫分離","attrs":{}},{"type":"text","text":" 和","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"主從複製","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d7/d7ae36e29a9c9f56efe65bca95fbc6ff.webp","alt":"圖片","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":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"主從數據庫之間數據同步問題。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"應用對於數據源的選擇問題。","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":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MySQL","attrs":{}}],"attrs":{}},{"type":"text","text":"自帶的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Master + Slave","attrs":{}}],"attrs":{}},{"type":"text","text":"的方式實現","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"主從複製","attrs":{}},{"type":"text","text":" 。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"採用","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"第三方數據庫中間件","attrs":{}},{"type":"text","text":" ,例如","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MyCat","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MyCat","attrs":{}}],"attrs":{}},{"type":"text","text":"是從","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cobar","attrs":{}}],"attrs":{}},{"type":"text","text":"發展而來的,而","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cobar","attrs":{}}],"attrs":{}},{"type":"text","text":"是阿里開源的數據庫中間件,後來停止開發。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MyCat","attrs":{}}],"attrs":{}},{"type":"text","text":"是國內比較好的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MySql","attrs":{}}],"attrs":{}},{"type":"text","text":"開源數據庫分庫分表中間件。","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":"text","text":"數據庫做讀庫的話,常常對","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"模糊查找","attrs":{}},{"type":"text","text":" 力不從心,即使做了讀寫分離,這個問題還未能解決。以我們所舉的交易網站爲例,發佈的商品存儲在數據庫中,用戶最常使用的功能就是查找商品,尤其是根據商品的標題來查找對應的商品。對於這種需求,一般我們都是通過","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"like","attrs":{}}],"attrs":{}},{"type":"text","text":"功能來實現的,但是這種方式的代價非常大,而且結果非常不準確。此時我們可以使用","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"搜索引擎","attrs":{}},{"type":"text","text":" 的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"倒排索引","attrs":{}},{"type":"text","text":" 來完成。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"搜索引擎具有的優點:它能夠大大提高查詢速度和搜索準確性。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"引入搜索引擎的開銷","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"帶來大量的維護工作,我們需要自己實現索引的構建過程,設計全量/增加的構建方式來應對非實時與實時的查詢需求。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需要維護搜索引擎集羣","attrs":{}}]}]}],"attrs":{}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"搜索引擎並不能替代數據庫,它解決了某些場景下的精準、快速、高效的“讀”操作,是否引入搜索引擎,需要綜合考慮整個系統的需求。","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/83/8357dac84133267f67720a6ffbf114ab.webp","alt":"圖片","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":"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":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"應用層和數據庫層的緩存","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隨着訪問量的增加,逐漸出現了許多用戶訪問同一部分","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"熱門內容","attrs":{}},{"type":"text","text":" 的情況,對於這些比較熱門的內容,沒必要每次都從數據庫讀取。我們可以使用","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"緩存技術","attrs":{}},{"type":"text","text":" ,例如可以使用Google的開源緩存技術","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Guava","attrs":{}}],"attrs":{}},{"type":"text","text":"或者使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Memecahed","attrs":{}}],"attrs":{}},{"type":"text","text":"作爲","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"應用層","attrs":{}},{"type":"text","text":" 的緩存,也可以使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Redis","attrs":{}}],"attrs":{}},{"type":"text","text":"作爲","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數據庫層","attrs":{}},{"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":"另外,在某些場景下,關係型數據庫並不是很適合,例如我想做一個“每日輸入密碼錯誤次數限制”的功能,思路大概是在用戶登錄時,如果登錄錯誤,則記錄下該用戶的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"IP","attrs":{}}],"attrs":{}},{"type":"text","text":"和錯誤次數,那麼這個數據要放在哪裏呢?假如放在內存中,那麼顯然會佔用太大的內容;假如放在關係型數據庫中,那麼既要建立數據庫表,還要簡歷對應的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Java bean","attrs":{}}],"attrs":{}},{"type":"text","text":",還要寫","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SQL","attrs":{}}],"attrs":{}},{"type":"text","text":"等等。而分析一下我們要存儲的數據,無非就是類似","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"{ip:errorNumber}","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":"NOSQL","attrs":{}}],"attrs":{}},{"type":"text","text":"數據庫來代替傳統的關係型數據庫。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"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":"HTML5","attrs":{}}],"attrs":{}},{"type":"text","text":"的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"localstroage","attrs":{}}],"attrs":{}},{"type":"text","text":"或者","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Cookie","attrs":{}}],"attrs":{}},{"type":"text","text":"。除了頁面緩存帶來的性能提升外,對於併發訪問且頁面置換頻率小的頁面,應儘量使用頁面靜態化技術。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優點:減輕數據庫的壓力, 大幅度提高訪問速度;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:需要維護緩存服務器,提高了編碼的複雜性。","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":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"緩存集羣的調度算法不同與上面提到的應用服務器和數據庫。最好採用","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"一致性哈希算","attrs":{}},{"type":"text","text":" ,這樣才能提高","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"命中率","attrs":{}},{"type":"text","text":" 。","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/50/50e9837fc90bf81807492827e0dac20b.webp","alt":"圖片","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":"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":"text","marks":[{"type":"strong","attrs":{}}],"text":"緩存","attrs":{}},{"type":"text","text":" 和","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"讀寫分離","attrs":{}},{"type":"text","text":" 的方式,但隨着數據庫的壓力繼續增加,數據庫數據量的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"瓶頸","attrs":{}},{"type":"text","text":" 越來越突出,此時,我們可以有數據","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"垂直拆分","attrs":{}},{"type":"text","text":" 和","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"水平拆分","attrs":{}},{"type":"text","text":" 兩種選擇。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"數據垂直拆分","attrs":{}}]},{"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},"content":[{"type":"text","text":"優點:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"解決了原來把所有業務放在一個數據庫中的壓力問題;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以根據業務的特點進行更多的優化。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"缺點:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需要維護多個數據庫的狀態一致性和數據同步。","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":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需要考慮原來跨業務的事務;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"跨數據庫的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Join","attrs":{}}],"attrs":{}},{"type":"text","text":"。","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":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"應該在應用層儘量避免跨數據庫的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"分佈式事務","attrs":{}},{"type":"text","text":" ,如果非要跨數據庫,儘量在","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"代碼","attrs":{}},{"type":"text","text":" 中控制。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"第三方中間件","attrs":{}},{"type":"text","text":" 來解決,如上面提到的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MyCat","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MyCat","attrs":{}}],"attrs":{}},{"type":"text","text":"提供了豐富的跨庫","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Join","attrs":{}}],"attrs":{}},{"type":"text","text":"方案,詳情可參考","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MyCat","attrs":{}}],"attrs":{}},{"type":"text","text":"官方文檔。","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d2/d23078459276a80c60a7e44a6bffb7c8.webp","alt":"圖片","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":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"數據水平拆分","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"水平拆分","attrs":{}},{"type":"text","text":" 就是把同一個表中的數據拆分到","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"兩個","attrs":{}},{"type":"text","text":" 甚至","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"多個","attrs":{}},{"type":"text","text":" 數據庫中。產生數據水平拆分的原因是某個業務的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數據量","attrs":{}},{"type":"text","text":" 或者更新量到達了","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"單個數據庫","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":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果能克服以上問題,那麼我們將能夠很好地對數據量及寫入量增長的情況。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"問題:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"訪問用戶信息的應用系統需要解決","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SQL路由","attrs":{}}],"attrs":{}},{"type":"text","text":"的問題,因爲現在用戶信息分在了兩個數據庫中,需要在進行數據操作時瞭解需要操作的數據在哪裏。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"主鍵","attrs":{}},{"type":"text","text":" 的處理也變得不同,例如原來","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"自增字段","attrs":{}},{"type":"text","text":" ,現在不能簡單地繼續使用。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果需要","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"分頁","attrs":{}},{"type":"text","text":" 查詢,那就更加麻煩。","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":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們還是可以通過可以解決第三方中間件,如","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MyCat","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"MyCat","attrs":{}}],"attrs":{}},{"type":"text","text":"可以通過","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SQL","attrs":{}}],"attrs":{}},{"type":"text","text":"解析模塊對我們的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SQL","attrs":{}}],"attrs":{}},{"type":"text","text":"進行解析,再根據我們的配置,把請求轉發到具體的某個數據庫。 我們可以通過","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"UUID","attrs":{}}],"attrs":{}},{"type":"text","text":"保證唯一或自定義ID方案來解決。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"MyCat","attrs":{}}],"attrs":{}},{"type":"text","text":"也提供了豐富的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"分頁查詢方案","attrs":{}},{"type":"text","text":" ,比如先從每個數據庫做分頁查詢,再合併數據做一次分頁查詢等等。","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b7/b7ff29ab3292da9407cbb04bdcfb0fd9.webp","alt":"圖片","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":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"階段八、應用的拆分","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"按微服務拆分應用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隨着業務的發展,業務越來越多,應用越來越大。我們需要考慮如何避免讓應用越來越","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"臃腫","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5e/5e3ef7215f31c99490f4c7cff3a2c2a5.webp","alt":"圖片","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":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣拆分後,可能會有一些相同的代碼,如用戶相關的代碼,商品和交易都需要用戶信息,所以在兩個系統中都保留差不多的操作用戶信息的代碼。如何保證這些代碼可以複用是一個需要解決的問題。","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":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過走服務化SOA的路線來解決頻繁公共的服務。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"走SOA服務化治理道路","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了解決上面拆分應用後所出現的問題,我們把","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"公共","attrs":{}},{"type":"text","text":" 的服務拆分出來,形成一種","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"服務化","attrs":{}},{"type":"text","text":" 的模式,簡稱","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SOA","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8c/8cf28d8049a17aa24d1a295f95bf7455.webp","alt":"圖片","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":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相同的代碼不會散落在不同的應用中了,這些實現放在了","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"各個服務中心","attrs":{}},{"type":"text","text":" ,使代碼得到更好的維護。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們把對數據庫的交互業務放在了各個服務中心,讓前端的Web應用更注重與瀏覽器交互的工作。","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":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如何進行遠程的服務調用?","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":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以通過下面的引入消息中間件來解決。","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":"text","text":"隨着網站的繼續發展,的系統中可能出現","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"不同語言","attrs":{}},{"type":"text","text":" 開發的子模塊和部署在不同平臺的子系統。此時我們需要一個平臺來傳遞可靠的,與平臺和語言無關的數據,並且能夠把","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"負載均衡透明化","attrs":{}},{"type":"text","text":" ,能在調用過程中","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"收集","attrs":{}},{"type":"text","text":" 並","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"分析","attrs":{}},{"type":"text","text":" 調用數據,推測出","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"網站的訪問增長率","attrs":{}},{"type":"text","text":" 等等一系列需求,對於網站應該如何成長做出預測。開源消息中間件有阿里的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Dubbo","attrs":{}}],"attrs":{}},{"type":"text","text":",可以搭配Google開源的分佈式程序協調服務","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Zookeeper","attrs":{}}],"attrs":{}},{"type":"text","text":"實現服務器的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"註冊","attrs":{}},{"type":"text","text":" 與","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"發現","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/da/dab72c7a9e8a54c63de4527976aa79e2.webp","alt":"圖片","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":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上的演變過程只是一個例子,並不適合所有的網站,實際中網站演進過程與自身業務和不同遇到的問題有密切的關係,沒有固定的模式。只有認真的分析和不斷地探究,才能發現適合自己網站的架構。","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章