架構模式:可複用的架構問題解決方案

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現如今,互聯網應用面臨着高併發、高可用、大數據、安全環境惡劣等挑戰,即使是傳統的企業應用也需要7x24小時不間斷服務,也越來越重視安全和數據的價值。因此,不管是傳統企業應用還是還是互聯網應用都面臨着同樣的挑戰。而爲了解決這一系列問題和挑戰,大型的互聯網公司在實踐中提出了許多解決方案,這些解決方案又被許多企業驗證,逐漸形成了軟件的架構模式。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"一、互聯網系統架構模式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"架構模式的關鍵在於複用,問題與場景的可重複性帶來了解決方案的可重複使用。互聯網架構模式就是試圖去描述那些爲解決互聯網系統高性能、高可用、易擴展、可伸縮、安全等挑戰,被很多互聯網應用重複使用的一些解決方案,這些解決方案是互聯網軟件系統的重要組成部分,而掌握這些解決方案和設計思路,可以指導我們進行架構設計。"}]},{"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":"一般的互聯網架構模式包含如下要素:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"分層"},{"type":"text","text":":將系統在橫向維度上切分成幾個部分,每個部分負責相對單一的職責,然後通過上層對下層依賴和調用組成一個完整的系統。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"分割"},{"type":"text","text":":將系統在縱向維度上切分,將不同功能和服務分割開來,包裝成高內聚低耦合的模塊單元。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"分佈式"},{"type":"text","text":":大型系統通過分層和分割以後,變成獨立的模塊部署在不同的服務器上,通過遠程調用的方式協同工作。分佈式意味着解決同樣的問題可以使用更多的硬件資源,也就能夠支撐更多的用戶數和數據量。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"集羣"},{"type":"text","text":":分佈式部署的服務需要構成統一的集羣,通過負載均衡設備通過對外提供服務。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"緩存"},{"type":"text","text":":緩存是改善系統性能的第一手段,在複雜的軟件系統中,緩存幾乎無處不在。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"異步"},{"type":"text","text":":將一個業務操作分成多個階段,每個階段之間通過共享數據的方式異步執行進行協作。在單一服務器內部通過多線程的方式實現異步,而分佈式系統中,多個服務器通過消息隊列實現異步。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"冗餘"},{"type":"text","text":":互聯網應用需要7x24小時不間斷提供服務,但服務器總會出現故障。想要在服務器故障時應用依然可以對外提供服務,且數據不丟失,就需要一定程度的服務器冗餘運行,數據冗餘備份。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"自動化"},{"type":"text","text":":自動化測試、自動化發佈可有效減少系統故障;自動化運維可保障系統出現故障快速恢復或通知相關人員。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"安全"},{"type":"text","text":":互聯網的開放性使其從誕生之初就面臨巨大的安全挑戰,雖然目前已有不少的措施來應對這些挑戰,但新的攻擊手段層出不窮,我們也只能打起精神,努力堵住系統可能存在的漏洞,預防可能的攻擊。"}]}]}]},{"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":"這些要素相互組合相互作用,構成了一個典型的互聯網系統架構。我們以微博架構爲例,簡單說明架構模式在微博的應用,下面是微博在2010年時對外分享的架構圖。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5e/5ee82044a15d4e8dcbd213000766acd7.jpeg","alt":"微博的系統架構","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從橫向來看,整個系統分爲3個層次,最上層是API,各種客戶端與第三方應用,通過調用API集成到微博的系統中,共同組成一個生態系統。中間層是平臺服務和應用服務層,是整個業務的基礎。而最下層則提供數據服務和其他基礎技術服務,這些服務支撐了新浪微博的海量數據和高併發訪問,是整個系統的技術基礎。"}]},{"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":"從縱向來看,中間的平臺服務與應用服務層根據不同的業務職責,又被分割成獨立的服務模塊。而下層的基礎服務又根據職責的不同,被分割成數據庫、緩存、存儲、消息隊列、搜索等數據服務,它們之間相互獨立,對上層提供支撐服務。"}]},{"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":"這些被分層和分割後的業務模塊與基礎技術模塊"},{"type":"text","marks":[{"type":"strong"}],"text":"分佈式"},{"type":"text","text":"部署,每個模塊都部署在一組獨立的服務器"},{"type":"text","marks":[{"type":"strong"}],"text":"集羣"},{"type":"text","text":"上,通過遠程調用的方式進行依賴訪問。"}]},{"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":"在線用戶的微博和近期微博緩存在分佈式"},{"type":"text","marks":[{"type":"strong"}],"text":"緩存集羣"},{"type":"text","text":"中,對於微博操作中最常見的“刷微博”操作,幾乎全部都是緩存訪問操作,可以獲得很好的系統性能。"}]},{"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":"爲解決系統的可靠性問題,微博啓用多個數據中心,用戶就近訪問附近數據中心,可加快用戶訪問速度,改善系統性能,同時也是數據"},{"type":"text","marks":[{"type":"strong"}],"text":"冗餘"},{"type":"text","text":"複製的災備中心,所有的用戶數據和微博數據通過遠程消息系統在不同數據中心間同步,以防止用戶數據丟失。"}]},{"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":"除此之外,微博還開發有一系列"},{"type":"text","marks":[{"type":"strong"}],"text":"自動化"},{"type":"text","text":"工具用於改善運維水平;一些列安全策略來保障系統和用戶數據"},{"type":"text","marks":[{"type":"strong"}],"text":"安全"},{"type":"text","text":"。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"二、架構模式實踐"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"模式的作用在於解決問題,在架構設計之初一定要識別出要解決的問題是什麼?而不是直接照搬大公司的解決方案。我經歷過一個小系統,爲了高併發和高可用,上了大量中間件和服務器,結果導致系統的複雜度直線上升,服務器成本也居高不下。到後來,由於系統確實沒有多少用戶,就回收了很多資源。因此,好的設計不是模仿,更不是生搬硬套某些模式,而是對問題深刻理解後的創造與創新。下面是一些在架構設計過程中可能遇到的一些問題,做一些簡單的探討。"}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"2.1 服務如何拆分"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"很多公司都在考慮微服務架構,自己也做了快五年時間,大部分微服務的坑都踩過。除開組織結構、運維層面等因素,最大的坑就是服務拆分。"}]},{"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":"首先,"},{"type":"text","marks":[{"type":"strong"}],"text":"一定要限制微服務數量"},{"type":"text","text":"。很多團隊在一開始根據業務對系統進行拆分,覺得什麼功能都可以被拆分成一個服務,到最後導致服務數量爆了。"}]},{"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":"我們團隊最開始也這樣,一個系統上了20多個微服務,開始還好,畢竟按照最初的需求,各個服務相互獨立,互不影響。但隨着業務的發展,單從業務層面來說,已經很難把某些業務完全獨立,都是相互影響的。後面新增一個需求要同時更新好幾個服務,如果這些服務是由多個團隊負責,那簡直就是災難。"}]},{"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":"因此,我覺得服務拆分的第一原則,一定是要控制好服務數量,最好的情況是兩個人負責一個服務,或者兩個人同時負責兩三個服務。即在每個服務都有人員備份的同時,讓每個人負責儘量少的服務。"}]},{"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":"其次,再考慮"},{"type":"text","marks":[{"type":"strong"}],"text":"業務因素和組織結構"},{"type":"text","text":"。業務因素,最重要的就是考慮邊界問題,這個不同的團隊都有自己的方法,這裏就不再細說。整個系統由一個團隊負責最好,如果是多個團隊,最好是把系統按:公共服務、核心業務、後臺管理、基礎組件四個維度來拆分,分給不同的團隊,然後團隊內部自己再拆。公共服務類似於認證授權、郵件短信等業務;核心業務就是系統最主要的業務;後臺管理就是公司內部的運營系統;基礎組件則是一些中間件和基礎框架的開發與維護等等。"}]},{"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":"最後,再從"},{"type":"text","marks":[{"type":"strong"}],"text":"技術角度"},{"type":"text","text":"考慮,比如某些業務在性能、安全等方面有較高的需求,可以單獨拆分出來,這樣也比較好分配人力和硬件資源。"}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"2.2 技術架構如何抓大放小"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"架構的目的是解決問題,但解決問題的成本有大有小,一個衡量的標準就是收益比。也就是說,我們如何用最小的人力、資源、時間成本來解決問題?下面是曹政(caoz)的幾點建議,更詳細的內容可直接閱讀 "},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s/UuEk1-Na3RUO7HPxlank2w","title":""},"content":[{"type":"text","text":"技術架構如何抓大放小"}]},{"type":"text","text":"。"}]},{"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"}],"text":"捨棄不必要的訴求和所需要的資源"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如,我一直搞不懂爲什麼系統中的所有刪除都要是邏輯刪除。什麼是無用功,這就是。浪費資源去記錄一些永遠無人關心的沉默數據,還要爲這些數據付出性能方面的代價。"}]},{"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"}],"text":"容忍存在一些不那麼高的精確度"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有些數據,能不實時就不實時統計,在系統上儘量做異步更新或提前計算,特別是高併發請求下,容忍一定程度的數據丟失風險。"}]},{"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"}],"text":"忙閒數據的區分處理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個相信大家比我有經驗,就不說啦"}]},{"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"}],"text":"必要時響應降級"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"整個系統出現嚴重故障時,僅維持最基本和必須的功能保證用戶最基本的體驗。比如,以前騰訊郵箱出現嚴重狀況時,信件可讀,但無法發送和回覆。在這種時候,響應降級的設計就很關鍵。"}]},{"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":"響應降級,你就必須明確,系統設計裏,哪部分是最核心的,最基礎的服務,在其他模塊全部崩潰的時候,依然可以倖存的,要儘可能獨立出來,做足準備。這是關鍵時刻救命的設計,可以在最危險的時候,減少你用戶或者客戶的流失。而且,也可以有效減少你緊急恢復系統時,疲於奔命的壓力。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"最後"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"牛逼的架構不是看它有多複雜多龐大,而是看它解決同樣的問題有多簡單。"}]},{"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":"極客大學架構師訓練營第四周學習總結"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"封面圖:"},{"type":"link","attrs":{"href":"https://unsplash.com/@ripato?utmsource=unsplash&utmmedium=referral&utm_content=creditCopyText","title":""},"content":[{"type":"text","text":"Ricardo Gomez Angel"}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"參考資料"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://union-click.jd.com/jdc?e=&p=AyIGZRprFQIQBFYTXRYyVlgNRQQlW1dCFFlQCxxKQgFHREkdSVJKSQVJHFRXFk9FUlpGQUpLCVBaTFhbXQtWVmpSWRtbFwERD1MYa0ZlRHpLaTJtYhZDFkUSU2ZhYRVhBXUOHjdUK1sUAxMGVRxaFwIiN1Uca0NsEgZUGloUBxICVitaJQIWD1cfUhYDEQ5UHV8lBRIOZUYfQVBQVWUraxYyIjdVK1glQHxXVk8MFQcUAV1PDBEHG1ICEloVUBUPAR5T","title":""},"content":[{"type":"text","text":"大型網站技術架構 核心原理與案例分析"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s?__biz=MzI4MTY5NTk4Ng==&mid=2247489220&idx=1&sn=59e8fb1328b27713547031a055b1b20a&source=41","title":""},"content":[{"type":"text","text":"服務拆分要點:不是越細越好"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s/UuEk1-Na3RUO7HPxlank2w","title":""},"content":[{"type":"text","text":"技術架構如何抓大放小"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章