当前位置: 欣欣网 > 码农

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

    看到最后,如果这个方法对你有用,一定要给我点个「 在看 」。