企業中臺化落地:從戰略分析到戰術實踐及架構演進過程

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"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":"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":"codeinline","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","text":"經過較長時間的思考、學習和實踐,我發現瞭解得越多越不敢講自己做的稱之爲"},{"type":"codeinline","content":[{"type":"text","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":"codeinline","content":[{"type":"text","text":"中臺系統"}]},{"type":"text","text":"自然也不一樣,但是對企業經營有有效的提升是顯爾易見的,所以設定好可量化的指標尤爲重要。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"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":"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":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1. 業務願景分析"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"增加企業經營效益:通過錢包預付款功能,增加資金沉澱,增強資金利用效率,同時降低顧客的促銷成本"}]}]},{"type":"listitem","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}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2. 業務模式分析"}]},{"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":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"會員錢包功能:與會員系統打通,與會員系統強綁定,實現會員專屬虛擬賬戶功能"}]}]},{"type":"listitem","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":"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":"bulletedlist","content":[{"type":"listitem","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}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3. 業務場景分析"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ec/ec3f8f14a764dfb8c136615801a4df02.png","alt":"業務構架圖","title":null,"style":null,"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":"主要爲線上線下結合,不同的終端不一樣的使用場景"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"線上微信、APP實現可信任的快速支付服務"}]}]},{"type":"listitem","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}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"4. 業務功能分析"}]},{"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":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"會員錢包:充值、消費等"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"消費卡:充值、消費、密碼、轉賬、掛失、開卡、銷戶等"}]}]},{"type":"listitem","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}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"5. 系統建設目標"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲滿足銷售任務的達成需要快速響應前端各業務場景的需求變化"}]}]},{"type":"listitem","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}},{"type":"heading","attrs":{"align":null,"level":2},"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作戰步驟需要緊貼公司戰略步驟制定,根據當下的實際的資源情況進行合理的配置"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"戰略目標較爲寬大,且較爲耗時耗力,需要先選擇一個較爲容易實現的目標,取得階段性的成果"}]}]},{"type":"listitem","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}},{"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":"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":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"三、 戰術落地"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1. 邏輯結構分析"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c6/c66b1753e2f2143933044bdad97f72b5.png","alt":"領域結構圖","title":null,"style":null,"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":"(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},"content":[{"type":"text","text":"我們採用DDD("},{"type":"codeinline","content":[{"type":"text","text":"領域驅動設計"}]},{"type":"text","text":")思想來分析業務:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"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":"italic"}],"text":"交易流水"},{"type":"text","text":"*劃分爲兩個"},{"type":"codeinline","content":[{"type":"text","text":"領域實體"}]},{"type":"text","text":",形成"},{"type":"codeinline","content":[{"type":"text","text":"聚合"}]}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用"},{"type":"codeinline","content":[{"type":"text","text":"命令模式"}]},{"type":"text","text":"驅動業務操作,以交易流水實體爲"},{"type":"codeinline","content":[{"type":"text","text":"聚合根"}]},{"type":"text","text":"驅動錢包賬戶實體的變化"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用"},{"type":"codeinline","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","text":"此階段可以以"},{"type":"codeinline","content":[{"type":"text","text":"事件風暴"}]},{"type":"text","text":"的形式,與領域專家一起使用"},{"type":"codeinline","content":[{"type":"text","text":"通用語言"}]},{"type":"text","text":"來展開討論,以達到業務、技術認識一致性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(2)抽象能力"}]},{"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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/de/de6ba786968e979b4e894388a13e43b9.png","alt":"業務抽象邏輯","title":null,"style":null,"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":"這個項目的業務共性就是所有的業務操作都是可以以"},{"type":"text","marks":[{"type":"strong"}],"text":"交易流水"},{"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},"content":[{"type":"text","text":"可變的內容抽象爲"},{"type":"codeinline","content":[{"type":"text","text":"業務行爲"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"業務規則"}]},{"type":"text","text":",不可變的就是"},{"type":"codeinline","content":[{"type":"text","text":"交易處理"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"交易完成"}]},{"type":"text","text":"和"},{"type":"codeinline","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}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(3)系統擴展性"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0d/0d209d776f5e0328131dfe330a065bf1.png","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":"這裏主要指系統間的擴展性。需要定義好相互通訊的協議和標準,通過定義好的流程將數字錢包系統與其它系統融合成一個整體。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2. 邏輯結構設計"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/10/1013fd3646a4e16747f4b86fbab32d2f.png","alt":"程序結構圖","title":null,"style":null,"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":"上圖爲系統架構的核心邏輯,主要有3大部分組成"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(1)WalletService 核心交易服務接口"}]},{"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":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public interface WalletService {\n void done();\n}"}]},{"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","marks":[{"type":"strong"}],"text":"DefaultService"},{"type":"text","text":" 默認的抽象類,主要實現"},{"type":"codeinline","content":[{"type":"text","text":"CheckPolicy"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"Behavior"}]},{"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":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class ConsumeService extends DefaultService {\n public ConsumeService(Wallet wallet, BigDecimal tradeAmount) {\n super(TradeRecord.builder().wallet(wallet).tradeAmount(tradeAmount).build());\n }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(2)Behavior 交易行爲接口"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public interface Behavior {\n void doAction();\n InOutFlag getInOutFlag();\n}\n\npublic class CreditBehavior extends DefaultBehavior {\n private final BigDecimal tradeAmount;\n\n public CreditBehavior(Wallet wallet, BigDecimal tradeAmount) {\n super(wallet);\n this.tradeAmount = tradeAmount;\n }\n\n @Override\n public void doAction() {\n super.doAction();\n wallet.setBalance(wallet.getBalance().add(tradeAmount));\n }\n\n @Override\n public InOutFlag getInOutFlag() {\n return InOutFlag.IN;\n }\n}"}]},{"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},"content":[{"type":"codeinline","content":[{"type":"text","text":"getInOutFlag()"}]},{"type":"text","text":"是對行爲產生的資金進出結果的配置"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(3)CheckPolicy 交易規則接口"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public interface CheckPolicy {\n void check();\n}\n\npublic class NoOverdraftAllowed implements CheckPolicy {\n private final Wallet wallet;\n private final BigDecimal tradeAmount;\n\n public NoOverdraftAllowed(Wallet wallet, BigDecimal tradeAmount) {\n this.wallet = wallet;\n this.tradeAmount = tradeAmount;\n }\n\n @Override\n public void check() {\n if (wallet.getBalance().compareTo(tradeAmount) < 0){\n throw new BizException(\"餘額不足\");\n }\n }\n}"}]},{"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":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3. 設計模式的運用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(1) 模板模式"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public abstract class DefaultService implements WalletService {\n\n protected abstract static class TradeConfig {\n public abstract TradeType tradeType();\n public abstract Behavior behavior();\n public abstract List checkPolicies();\n }\n\n protected abstract TradeConfig tradeConfig();\n\n @Override\n public void done() {\n check();\n tradeConfig().behavior().doAction();\n tradeRecord.setBalance(tradeRecord.getWallet().getBalance());\n tradeRecord.setTradeStatus(TradeStatus.SUCCEED);\n }\n}\n\npublic class LockService extends DefaultService {\n\n public LockService(Wallet wallet) {\n super(TradeRecord.builder().wallet(wallet).build());\n }\n\n @Override\n protected TradeConfig tradeConfig() {\n return new TradeConfig() {\n @Override\n public TradeType tradeType() {\n return TradeType.LOCK;\n }\n\n @Override\n public Behavior behavior() {\n return new LockBehavior(getWallet());\n }\n\n @Override\n public List checkPolicies() {\n return CheckPolicyBuilder.builder()\n .add(new NoAvailableStatusAllowed(getWallet()))\n .build();\n }\n };\n }\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在主交易流程中,將共有的流程放在"},{"type":"codeinline","content":[{"type":"text","text":"done()"}]},{"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}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"模板模式的優點:"}]},{"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":"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":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(2) 策略模式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class RechargeRollbackService extends DefaultService {\n private final TradeRecord sourceTrade;\n\n public RechargeRollbackService(TradeRecord sourceTrade) {\n // ...\n }\n\n @Override\n protected DefaultService.TradeConfig tradeConfig() {\n return new TradeConfig() {\n @Override\n public TradeType tradeType() {\n return TradeType.RECHARGE_ROLLBACK;\n }\n\n @Override\n public Behavior behavior() {\n return new DebitBehavior(getWallet(), getTradeAmount());\n }\n\n @Override\n public List checkPolicies() {\n return CheckPolicyBuilder.builder()\n .add(new NoRechargeTypeAllowed(sourceTrade))\n .add(new NoTimeoutAllowed(sourceTrade))\n .add(new NoOverdraftAllowed(getWallet(), getTradeAmount()))\n .add(new NoAvailableStatusAllowed(getWallet()))\n .build();\n }\n };\n }\n}\n"}]},{"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":"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":"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}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(3) 組合模式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public abstract class DefaultTowPCService extends DefaultService implements TwoPCWalletService {\n\n public DefaultTowPCService(TradeRecord tradeRecord) {\n super(tradeRecord);\n }\n\n @Override\n public void process() {\n check();\n getTradeRecord().setTradeStatus(TradeStatus.PROCESSING);\n }\n}\n\npublic class RechargeService extends DefaultTowPCService {\n public RechargeService(Wallet wallet, BigDecimal tradeAmount){\n super(TradeRecord.builder().wallet(wallet).tradeAmount(tradeAmount).build());\n }\n}"}]},{"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":"TwoPCWalletService"},{"type":"text","text":",*"},{"type":"text","marks":[{"type":"italic"}],"text":"DefaultTowPCService"},{"type":"text","text":"* 用於在原有接口基礎上擴展的二階段提交功能,此處爲了保持"},{"type":"text","marks":[{"type":"strong"}],"text":"DefaultService"},{"type":"text","text":"功能的單一性,並沒有在原有類上進行功能擴展,而是使用"},{"type":"codeinline","content":[{"type":"text","text":"組合模式"}]},{"type":"text","text":"進行功能擴展"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此現實類就現實了"},{"type":"codeinline","content":[{"type":"text","text":"WalletService"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"TwoPCWalletService"}]},{"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":"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"四、 系統架構演進"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1. 結構說明"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5b/5b19caeebce91980295693a111c157fc.png","alt":"程序層次結構圖","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"wallet-common"},{"type":"text","text":"爲內部各層的共用實體對象"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"wallet-domain"},{"type":"text","text":"爲抽象的業務基礎邏輯與規則,一般不具體直接業務場景支持。需要與"},{"type":"text","marks":[{"type":"strong"}],"text":"wallet-service"},{"type":"text","text":"結合實現完成的業務邏輯。在團隊開發層面,這一個層次的獨立可以有效控制基礎規則的代碼穩定性"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"wallet-repository"},{"type":"text","text":"專注數據對象的持久化,與業務邏輯進行隔離"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"wallet-service"},{"type":"text","text":"爲符合業務場景的業務功能實現,主要依靠"},{"type":"text","marks":[{"type":"strong"}],"text":"wallet-domain"},{"type":"text","text":"與*"},{"type":"text","marks":[{"type":"italic"}],"text":"wallet-repository"},{"type":"text","text":"*的相互組合來完成"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"wallet-client"},{"type":"text","text":"用於對外的前端接口層"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"wallet-provider"},{"type":"text","text":"用於對內部微服務的接口層"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2. 第一階段 單一結構模式"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dd/ddf66a97761a7e4211df09f1cd964b96.png","alt":"單一運行結構圖","title":null,"style":null,"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":"這一階段,業務模式單一,業務功能單一,業務量較少,開發人員也較少,將所有模塊打包運行在一個jvm中"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3. 第二階段 多業務結構模式"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7f/7f497fddf1ea5917679f02a51d6b5ff8.png","alt":"多業務運行結構圖","title":null,"style":null,"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":"這一階段,隨着業務模式和業務量的增加,模式間的業務功能也不相同或有互斥性,單一結構已不能滿足,故將基礎模塊封閉爲"},{"type":"codeinline","content":[{"type":"text","text":"SDK"}]},{"type":"text","text":",每種業務模式單獨一套系統獨立維護"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"4. 第三階段 業務中臺化"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7b/7b1601f909c49f3e822e1d0fc1860c2c.png","alt":"業務中臺結構圖","title":null,"style":null,"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":"隨着業務模式、業務量、業務功能需求持續增加。由於各業務線獨立運營,導致SDK版本不一致,增加了維護成本。多團隊維護架構導致功能重複且實現過程參差不齊,帶來一定維護成本且系統間無法實現複用,同時每條業務線獨立運營也帶來用人成本的增加。"}]},{"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":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"業務能力("},{"type":"text","marks":[{"type":"strong"}],"text":"wallet-domain"},{"type":"text","text":"):將業務高度抽象形成一個一個基礎能力"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"業務域("},{"type":"text","marks":[{"type":"strong"}],"text":"wallet-service"},{"type":"text","text":"):將業務能力根據業務場景進行組合編排形成功能域"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"持久層("},{"type":"text","marks":[{"type":"strong"}],"text":"wallet-repository"},{"type":"text","text":"):根據不同的情況,將數據持久化到中臺或前臺"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前臺觸點("},{"type":"text","marks":[{"type":"strong"}],"text":"wallet-client"},{"type":"text","text":"):根據不同的場景定製不用的前臺API"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"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":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"六、源代碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文中代碼由於篇幅原因有一定省略並不是完整邏輯,如有興趣請Fork源代碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://gitee.com/hypier/barry-wallet","title":""},"content":[{"type":"text","text":"https://gitee.com/hypier/barry-wallet"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"七、請關注我的公衆號"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/62/629ab9a8e71fdadbe20e9f7c365b04bf.jpeg","alt":"請關注我的公衆號","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章