當前位置: 妍妍網 > 碼農

20.7k Star! 推薦一款基於 DDD 領域驅動模型開發的企業級 TOC 商城

2024-05-08碼農

一、計畫簡介

一個從零到一的C端商城計畫,包含商城核心業務和基礎架構兩大模組。推出使用者、訊息、商品、訂單、優惠券、支付、閘道器、購物車等業務模組,透過商城系統中復雜場景,給出對應解決方案。使用 DDD 模型開發系統功能,幫助對 DDD 一知半解的開發者樹立正確地開發思路。


如果你能夠認真且堅持把計畫看完,我相信你會收獲包括不限於下面這些知識點:

  • 基於 DDD 領域驅動模型設計實作的商品、購物車、訂單、使用者、訊息以及支付服務。

  • 掌握分布式鎖、分布式事務、分布式搜尋、分布式緩存、分布式限流以及分庫分表等核心技術。

  • 完成基礎元件抽象,規約、緩存、冪等、分布式 ID、數據持久層、脫敏以及日誌等底層元件庫。

  • 基於 Agent 開發字節碼流量監控,監控計畫介面 QPS、響應時間和異常請求等核心指標。

  • 掌握常用設計模式實戰場景,策略、責任鏈、裝飾器、觀察者以及介面卡等設計模式。

  • 二、 如何開始

    商城核心有兩塊,分別是商城業務和基礎架構,透過認真學習分別可以收獲以下兩種能力提升。

  • 商城業務:透過學習剛果商城中復雜業務處理場景,增加自己的復雜業務處理能力。

  • 基礎架構:嘗試跟著基礎架構部份自己把輪子都造一遍,以此提高自己方案設計和公共程式碼開發能力。

  • 三 、商城核心業務

    學習流程,包括安裝中漸漸以及啟動計畫等事項:

    1. 初始化資料庫,比如商品庫、訂單庫、使用者庫、支付庫、購物車庫等;

    2. 透過 Docker 安裝計畫中依賴的中介軟體,比如 Nacos2、MySQL、Seata、RocketMQ 等;

    3. 學習介面呼叫流程,計畫介面目前全量放入 Apifox 軟體中,可透過線上存取;

    4. 檢視不同微服務之間的依賴關系,並根據文件中的描述進行修改指定參數;

    5. 找到自己感興趣的模組功能 Debug 原始碼,參考程式碼設計。

    四、基礎架構

    基礎架構相關的程式碼都在 congomall-framework-all 模組中,可以透過官網檢視如何實作。

    五、介面文件

    目前已開發的介面已匯總至下述介面文件中,本地啟動對應計畫,透過介面文件存取檢視效果。

    https://www.apifox.cn/web/project/1038592/apis/api-50106328-run

    六、計畫結構說明

    ├── congomall-basic-data || -- # 基礎數據服務
    │ ├── congomall-basic-data-application
    │ ├── congomall-basic-data-domain
    │ ├── congomall-basic-data-infrastructure
    │ ├── congomall-basic-data-interface
    ├── congomall-bff || -- # 商城 BFF 聚合層
    │ ├── congomall-bff-biz
    │ ├── congomall-bff-remote
    │ ├── congomall-bff-web
    ├── congomall-cart || -- # 購物車服務
    │ ├── congomall-cart-application
    │ ├── congomall-cart-domain
    │ ├── congomall-cart-infrastructure
    │ ├── congomall-cart-interface
    ├── congomall-coupon || -- # 優惠券服務
    ├── congomall-customer-user || -- # C端使用者服務
    │ ├── congomall-customer-user-application
    │ ├── congomall-customer-user-domain
    │ ├── congomall-customer-user-infrastructure
    │ ├── congomall-customer-user-interface
    │ ├── congomall-customer-user-mock
    ├── congomall-framework-all || -- # 基礎元件
    │ ├── congomall-base-spring-boot-starter || -- # 頂層抽象基礎元件
    │ ├── congomall-cache-spring-boot-starter || -- # 緩存元件
    │ ├── congomall-common-spring-boot-starter || -- # 公共工具包元件
    │ ├── congomall-convention-spring-boot-starter || -- # 計畫規約元件
    │ ├── congomall-database-spring-boot-starter || -- # 資料庫持久層元件
    │ ├── congomall-ddd-framework-core || -- # DDD抽象介面元件
    │ ├── congomall-designpattern-spring-boot-starter || -- # 設計模式抽象元件
    │ ├── congomall-distributedid-spring-boot-starter || -- # 分布式ID元件
    │ ├── congomall-flow-monitor-agent || -- # 微服務流量監控元件
    │ ├── congomall-httputil-spring-boot-starter || -- # Http網路呼叫元件
    │ ├── congomall-idempotent-spring-boot-starter || -- # 分布式冪等元件
    │ ├── congomall-log-spring-boot-starter || -- # 日誌打印元件
    │ ├── congomall-minio-spring-boot-starter || -- # 檔儲存元件
    │ ├── congomall-openfeign-spring-boot-starter || -- # 微服務呼叫元件
    │ ├── congomall-rocketmq-spring-boot-starter || -- # 分布式訊息佇列元件
    │ ├── congomall-sensitive-spring-boot-starter || -- # 前端返回數據脫敏元件
    │ ├── congomall-swagger-spring-boot-starter || -- # 文件API元件
    │ ├── congomall-web-spring-boot-starter || -- # Web元件
    │ ├── congomall-xxljob-spring-boot-starter || -- # 定時任務元件
    ├── congomall-gateway || -- # 閘道器服務
    ├── congomall-message || -- # 訊息服務
    │ ├── congomall-message-application
    │ ├── congomall-message-domain
    │ ├── congomall-message-infrastructure
    │ ├── congomall-message-interface
    ├── congomall-order || -- # 訂單服務
    │ ├── congomall-order-application
    │ ├── congomall-order-domain
    │ ├── congomall-order-infrastructure
    │ ├── congomall-order-interface
    ├── congomall-pay || -- # 支付服務
    │ ├── congomall-pay-application
    │ ├── congomall-pay-domain
    │ ├── congomall-pay-infrastructure
    │ ├── congomall-pay-interface
    ├── congomall-product || -- # 商品服務
    │ ├── congomall-product-application
    │ ├── congomall-product-domain
    │ ├── congomall-product-infrastructure
    │ ├── congomall-product-interface
    │ ├── congomall-product-job
    ├── congomall-test-all || -- # 測試用例
    │ ├── congomall-flow-monitor-agent-test
    │ ├── congomall-h2-test
    │ ├── congomall-oom-test
    │ ├── congomall-smooth-sharding-test
    │ ├── congomall-yaml-test
    ├── dev-support || -- # 開發工具包

    七、服務模組核心流程

    使用者服務

    使用者服務,提供驗證碼發送、使用者註冊、使用者登入以及收貨地址管理等能力。

    ├── congomall-customer-user
    │ ├── congomall-customer-user-application # 使用者服務套用層
    │ ├── congomall-customer-user-domain # 使用者服務領域層
    │ ├── congomall-customer-user-infrastructure # 使用者服務基礎層
    │ ├── congomall-customer-user-interface # 使用者服務使用者介面層
    │ ├── congomall-customer-user-mock # 使用者服務數據模擬層

    核心流程

    1)使用者數據使用 ShardingSphere 框架 HASH_MOD 分片演算法對使用者 ID 進行分片。

    2)使用者相關敏感資訊采用 ShardingSphere 框架 AES 加密演算法對手機號、信箱欄位進行加密儲存。

    3)使用者驗證碼發送使用策略模式、樣版方法模式抽象。

  • 程式碼地址: org.opengoofy.congomall.biz.customer.user.web.controller.CustomerUserController.verifyCodeSend

  • 4)使用者變更日誌透過 RocketMQ 異步儲存。

  • 程式碼地址: org.opengoofy.congomall.biz.customer.user.infrastructure.mq.produce.CustomerUserOperationLogProduce.recordCustomerUserOperationLog

  • 購物車服務

    購物車服務,提供購物車新增、查詢、修改以及刪除等能力。

    ├── congomall-cart
    │ ├── congomall-cart-application # 購物車服務套用層
    │ ├── congomall-cart-domain # 購物車服務領域層
    │ ├── congomall-cart-infrastructure # 購物車服務基礎層
    │ ├── congomall-cart-interface # 購物車服務使用者介面層

    核心流程

    1)購物車表按照使用者 ID HASH_MODE 方式分表。

    商品服務

    商品服務,提供商品分類查詢、商品詳情查詢、商品庫存查詢等能力。

    ├── congomall-product
    │ ├── congomall-product-application # 商品服務套用層
    │ ├── congomall-product-domain # 商品服務領域層
    │ ├── congomall-product-infrastructure # 商品服務基礎層
    │ ├── congomall-product-interface # 商品服務使用者介面層
    │ ├── congomall-product-job # 商品服務任務層

    核心流程

    1)千萬數據透過並行編程高效能匯入三方資料庫,比如 ElasticSearch。

  • 程式碼地址: congomall-product/congomall-product-job/src/main/java/org/opengoofy/congomall/biz/product/job/handler/InitializeProductJobHandler.java

  • 2)商品 SPU 表按照店鋪 ID 分表,同時支持按照 ID 分片查詢,基於雪花演算法基因實作。

  • 程式碼地址: org.opengoofy.congomall.biz.product.infrastructure.algorithm.ProductSnowflakeServiceShardingAlgorithm

  • 3)商品 SKU 表按照店鋪 ID 分表,同時支持按照商品 ID 分片查詢,基於雪花演算法基因實作。

  • 程式碼地址: org.opengoofy.congomall.biz.product.infrastructure.algorithm.ProductSkuSnowflakeServiceShardingAlgorithm

  • 4)商品評價表按照商品 ID HASH_MODE 方式分表。

    訊息服務

    訊息服務,提供信箱訊息發送、簡訊訊息發送、微信公眾號以及微信訂閱號發送等能力。

    ├── congomall-message
    │ ├── congomall-message-application # 訊息服務套用層
    │ ├── congomall-message-domain # 訊息服務領域層
    │ ├── congomall-message-infrastructure # 訊息服務基礎層
    │ ├── congomall-message-interface # 訊息服務使用者介面層

    核心流程

    1)自訂復合分片演算法,根據訊息 ID 以及訊息發送時間分片儲存。

    復合分片配置檔相對單分片來說,越顯復雜,需要仔細梳理配置項。簡單來說復合分片的邏輯:透過訊息發送時間進行分片儲存,同時支持按照訊息 ID 精確查詢。

  • 程式碼地址: org.opengoofy.congomall.biz.message.infrastructure.algorithm.SnowflakeDateShardingAlgorithm

  • 2)訊息發送透過 RocketMQ 異步發送,減輕系統壓力。

  • 程式碼地址: org.opengoofy.congomall.biz.message.infrastructure.mq.consume.MailMessageSendConsume.mailMessageSend

  • 訂單服務

    訂單服務,提供商品下單、訂單查詢、訂單確認等能力。

    ├── congomall-order
    │ ├── congomall-order-application # 訂單服務套用層
    │ ├── congomall-order-domain # 訂單服務領域層
    │ ├── congomall-order-infrastructure # 訂單服務基礎層
    │ ├── congomall-order-interface # 訂單服務使用者介面層

    核心流程

    1)商品下單引入分布式事務。商品服務 -> 購物車服務 -> 商品服務。

    2)訂單建立使用責任鏈模式驗證參數必填、參數正確性、商品庫存是否正確等邏輯。

  • 程式碼地址: org.opengoofy.congomall.biz.order.application.service.impl.OrderServiceImpl.createOrder

  • 3)訂單建立邏輯使用觀察者模式解耦合,基於 Spring ApplicationEvent 實作。

  • 程式碼地址: org.opengoofy.congomall.biz.order.application.service.impl.OrderServiceImpl.createOrder

  • 4)商品下單後,透過 RocketMQ 延遲訊息關閉訂單。

  • 程式碼地址: org.opengoofy.congomall.biz.order.infrastructure.mq.provide.DelayCloseOrderProvide

  • 5)訂單按照使用者 ID 分表,同時支持按照訂單號分片查詢,基於雪花演算法基因實作。

  • 程式碼地址: org.opengoofy.congomall.biz.order.infrastructure.algorithm.OrderSnowflakeServiceShardingAlgorithm

  • 6)訂單明細表按照訂單號 HASH_MOD 方式分表。

    支付服務

    支付服務,提供支付寶、微信、銀行卡等支付方式,支持支付查詢以及退款等功能。

    ├── congomall-order
    │ ├── congomall-pay-application # 支付服務套用層
    │ ├── congomall-pay-domain # 支付服務領域層
    │ ├── congomall-pay-infrastructure # 支付服務基礎層
    │ ├── congomall-pay-interface # 支付服務使用者介面層

    核心流程:

    1)透過策略模式封裝支付渠道和支付場景,使用者支付時動態選擇對應的支付元件。

  • 程式碼地址: org.opengoofy.congomall.biz.pay.application.service.impl.PayServiceImpl#commonPay

  • 2)透過策略模式封裝支付回呼場景,三方支付平台回呼時動態選擇對應的支付回呼元件。

  • 程式碼地址: org.opengoofy.congomall.biz.pay.application.service.impl.PayServiceImpl#callback

  • 頁面 截圖

    首頁:

    熱門商品:

    品牌周邊:

    品牌精選:

    原始碼下載地址:

    https://gitee.com/nageoffer/congomall.git

    看到最後,如果這個方法對你有用,一定要給我點個「 在看 」。