本篇是基于 COLA 架构的 Spring Cloud Alibaba 系列的开篇,主要是先介绍一下项目的基本架构和相关知识。在本系列中,我们将基于 COLA 架构,搭建各个微服务工程,然后整合 Spring Cloud Alibaba。在参照 COLA 规则搭建微服务的过程中,我们也不完全遵循 COLA 的规则,结合实际项目,适当调整。

1. 关于 COLA

COLA(Clean Object-Oriented and Layered Architecture 的缩写,意为“整洁面向对象分层架构”)是关于应用系统分层管理的一套架构规则,目前最新的版本是 COLA 4.0,关于 COLA 详细的介绍,请参照官方博文,开源项目地址(https://github.com/alibaba/COLA)中也有关于 COLA 的介绍。

2. 版本选择

2.1. COLA

目前 COLA 最新的版本是 4.0,我们就参照该版本。

2.2. Spring Cloud Alibaba

Spring Cloud Alibaba 最近正式发布了 2022.0.0.0 版本,该版本支持 Spring Boot 3.0.x,最低支持 JDK 17,那我们就选择 Spring Cloud Alibaba 的 2022.0.0.0 版本

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_Spring Boot3

2.3. Spring Cloud

由于 Spring Cloud Alibaba 2022.0.0.0 版本使用的 Spring Cloud 版本为 2022.0.0,因此我们的 Spring Cloud 版本为也选择 2022.0.0 的版本。

2.4. Spring Boot

由于 Spring Cloud Alibaba 2022.0.0.0 版本使用的 Spring Boot 版本为 3.0.2,因此我们的 Spring Boot 版本为也选择 3.0.2 的版本。

3. 模块设计

本系列微服务将搭建账户、商品、订单三个模块。各个模块分别建立数据库。

3.1. 账户模块

账户模块数据库取名为:mall-account,在 mall-account 数据库下创建表 mall_account。主要存储用户的账户信息,例如账户code、账户名称、金额等。

mall_account 表模型如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_项目架构_02

mall_account 建表语句如下。

CREATE TABLE `mall_account` (
  `id` bigint NOT NULL COMMENT '主键id',
  `user_id` bigint DEFAULT NULL COMMENT '用户id',
  `account_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '账户code',
  `account_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '账户名称',
  `amount` decimal(10,2) DEFAULT '0.00' COMMENT '金额',
  `is_deleted` tinyint(1) DEFAULT '0' COMMENT '是否被删除',
  `created_by` bigint DEFAULT NULL COMMENT '创建人',
  `created_time` datetime DEFAULT NULL COMMENT '创建时间',
  `updated_by` bigint DEFAULT NULL COMMENT '修改人',
  `updated_time` datetime DEFAULT NULL COMMENT '修改时间',
  `reversion` int DEFAULT '0' COMMENT '版本号',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_account_code` (`account_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

3.2. 商品模块

商品模块数据库取名为:mall-product,在 mall-product 数据库下创建表 mall_product。主要存储商品信息,例如商品编码、商品名称、库存数量、单价等。

mall_product 表模型如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_Spring Boot3_03

mall_product 建表语句如下。

CREATE TABLE `mall_product` (
  `id` bigint NOT NULL COMMENT '主键id',
  `product_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '商品编码',
  `product_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '商品名称',
  `count` int DEFAULT '0' COMMENT '库存数量',
  `price` decimal(10,2) DEFAULT '0.00' COMMENT '单价',
  `is_deleted` tinyint(1) DEFAULT '0' COMMENT '是否被删除',
  `created_by` bigint DEFAULT NULL COMMENT '创建人',
  `created_time` datetime DEFAULT NULL COMMENT '创建时间',
  `updated_by` bigint DEFAULT NULL COMMENT '修改人',
  `updated_time` datetime DEFAULT NULL COMMENT '修改时间',
  `reversion` int DEFAULT '0' COMMENT '版本号',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_product_code` (`product_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

3.3. 订单模块

订单模块数据库取名为:mall-order,在 mall-order 数据库下创建表 mall_order。主要存储订单信息,例如订单编号、账户code、商品编号、数量、金额等。

mall_order 表模型如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_Spring Cloud Alibaba_04

mall_product 建表语句如下。

CREATE TABLE `mall_order` (
  `id` bigint NOT NULL COMMENT '主键id',
  `order_no` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '订单编号',
  `account_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '账户code',
  `product_code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '商品编号',
  `count` int DEFAULT '0' COMMENT '数量',
  `amount` decimal(10,2) DEFAULT '0.00' COMMENT '金额',
  `is_deleted` tinyint(1) DEFAULT '0' COMMENT '是否被删除',
  `created_by` bigint DEFAULT NULL COMMENT '创建人',
  `created_time` datetime DEFAULT NULL COMMENT '创建时间',
  `updated_by` bigint DEFAULT NULL COMMENT '修改人',
  `updated_time` datetime DEFAULT NULL COMMENT '修改时间',
  `reversion` int DEFAULT '0' COMMENT '版本号',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_order_no` (`order_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

4. 项目结构

我们将搭建一个名称为 mall-demo 的工程,作为父级工程。在 mall-demo 下将创建 mall-account-center、mall-product-center、mall-order-center 等子工程。各子工程项目结构参照 COLA 4.0 进行分层,各层的分工如下。

4.1. 适配层(Adapter Layer)

负责对前端展示(web,wireless,wap)的路由和适配,对于传统B/S系统而言,adapter 就相当于MVC 中的 controller。以账户模块为例,账户模块的工程名称为 mall-account-center,我们将创建名称为 mall-account-center-adapter 的工程。由于我们使用的是 Spring Cloud Alibaba,涉及到OpenFeign 接口提供调用,adapter 除了适配 web,wireless,wap外,还将适配 OpenFeign。

4.2. 应用层(Application Layer)

主要负责获取输入,组装上下文,参数校验,调用领域层做业务处理,如果需要的话,发送消息通知等。层次是开放的,应用层也可以绕过领域层,直接访问基础实施层。以账户模块为例,账户模块的工程名称为 mall-account-center,我们将创建名称为 mall-account-center-app 的工程。

4.3. 领域层(Domain Layer)

主要是封装了核心业务逻辑,并通过领域服务(Domain Service)和领域对象(Domain Entity)的方法对 App 层提供业务实体和业务逻辑计算。领域是应用的核心,不依赖任何其他层次。以账户模块为例,账户模块的工程名称为 mall-account-center,我们将创建名称为 mall-account-center-domain 的工程。

4.4. 基础实施层(Infrastructure Layer)

主要负责技术细节问题的处理,比如数据库的 CRUD、搜索引擎、文件系统、分布式服务的 RPC 等。此外,领域防腐的重任也落在这里,外部依赖需要通过 gateway 的转义处理(本系列不受该约束),才能被上面的 App 层和Domain 层使用。以账户模块为例,账户模块的工程名称 为 mall-account-center,我们将创建名称为 mall-account-center-infra 的工程(Infrastructure 名字太长,简写为infra)。

4.5. Client SDK

服务对外透出的 API,我们使用的是 Spring Cloud Alibaba,在此将 OpenFeign 接口以 SDK 的形式对外输出 API。

5. 项目搭建

5.1. 父级工程

创建父级工程,工程名称为:mall-demo。

mall-demo的pom.xml 内容如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>mall-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>mall-demo</name>
  <description>父模块</description>

  <modules>
    <module>mall-component</module>
    <module>mall-account-center</module>
    <module>mall-product-center</module>
    <module>mall-order-center</module>
  </modules>
  <packaging>pom</packaging>


  <!-- 统一管理jar包版本 -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

</project>

5.2. 组件模块

在 mall-demo 工程下创建组件子工程,名称为:mall-component。该工程主要用来集成一些基础的组件,例如公共组件、异常组件等。

mall-component 的 pom.xml 内容如下。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>mall-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>org.example.component</groupId>
    <artifactId>mall-component</artifactId>
    <name>mall-component</name>
    <description>基础组件</description>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>mall-component-common</module>
        <module>mall-component-exception</module>
    </modules>

    <!-- 统一管理jar包版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

</project>

5.2.1. 公共组件工程

在 mall-component 工程下创建公共组件子工程,名称为:mall-component-common。该工程主要用来集成一些工具类、全局常量等。

mall-component-common 的 pom.xml 内容如下。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example.component</groupId>
        <artifactId>mall-component</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>mall-component-common</artifactId>
    <name>mall-component-common</name>
    <description>组件工具</description>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
        </dependency>
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.32</version>
        </dependency>
        <!-- https://www.pangugle.com/lib/maven/view/cn.hutool/hutool-all.html -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.19</version>
        </dependency>
    </dependencies>

</project>

mall-component-common 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_Spring Cloud_05

5.2.2. 异常组件工程

在 mall-component 工程下创建异常组件子工程,名称为:mall-component-exception。该工程主要用来集成一些对异常处理的工具类。COLA 官方项目中也有非常好用的异常组件,可以拷贝一些过来使用。

mall-component-exception 的 pom.xml 内容如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example.component</groupId>
        <artifactId>mall-component</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>mall-component-exception</artifactId>
    <name>mall-component-exception</name>
</project>

mall-component-exception 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_项目架构_06

5.3. 账户模块

在 mall-demo 工程下创建账户模块工程,名称为:mall-account-center。该工程主要用来管理账户模块各子工程。

mall-account-center 工程的 pom.xml 文件内容如下。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>mall-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>org.example.account</groupId>
    <artifactId>mall-account-center</artifactId>
    <name>mall-account-center</name>
    <description>账户模块</description>
    <packaging>pom</packaging>

    <modules>
        <module>mall-account-center-dto</module>
        <module>mall-account-center-client</module>
        <module>mall-account-center-adapter</module>
        <module>mall-account-center-app</module>
        <module>mall-account-center-domain</module>
        <module>mall-account-center-infra</module>
        <module>mall-account-center-start</module>
    </modules>

    <!-- 统一管理jar包版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <spring-boot.version>3.0.2</spring-boot.version>
        <spring-cloud.version>2022.0.0</spring-cloud.version>
        <spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version>
        <project.version>1.0.0-SNAPSHOT</project.version>
    </properties>

    <dependencyManagement>
    <dependencies>
        <!--Project modules-->
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-dto</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-client</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-adapter</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-app</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-domain</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-infra</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!--spring-boot-dependencies-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
          <!--spring cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version> ${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--spring cloud alibaba -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version> ${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

    </dependencies>
</dependencyManagement>

</project>

5.3.1. adapter 层工程

在 mall-account-center 工程下创建适配层子工程,名称为:mall-account-center-adapter。

mall-account-center-adapter 工程的 pom.xml 文件内容如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example.account</groupId>
        <artifactId>mall-account-center</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>mall-account-center-adapter</artifactId>
    <name>mall-account-center-adapter</name>
    <description>适配层</description>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-app</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

mall-account-center-adapter 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_Spring Boot3_07

其中 org.example.account.adapter 包目录下的各个子目录,分别用来存放适配各种端的适配器。

5.3.2. app 层工程

在 mall-account-center 工程下创建应用层子工程,名称为:mall-account-center-app。

mall-account-center-app 工程的 pom.xml 文件内容如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example.account</groupId>
        <artifactId>mall-account-center</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>mall-account-center-app</artifactId>
    <name>mall-account-center-app</name>
    <description>应用层</description>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--mall-account-center-infra-->
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-infra</artifactId>
        </dependency>
        <!--mall-component-exception-->
        <dependency>
            <groupId>org.example.component</groupId>
            <artifactId>mall-component-exception</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

mall-account-center-adapter 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_Spring Cloud Alibaba_08

本系列中主要用到 org.example.account.app.executor 包目录,用于获取输入,组装上下文,参数校验,调用领域层做业务处理。

5.3.3. domain 层工程

在 mall-account-center 工程下创建领域层子工程,名称为:mall-account-center-domain。

mall-account-center-domain 工程的 pom.xml 文件内容如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example.account</groupId>
        <artifactId>mall-account-center</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>mall-account-center-domain</artifactId>
    <name>mall-account-center-domain</name>
    <description>领域层</description>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--mall-account-center-dto-->
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-dto</artifactId>
        </dependency>
    </dependencies>

</project>

mall-account-center-domain 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_COLA_09

其中 org.example.account.domain.model 包目录用于存放实体类,org.example.account.domain.service 包目录用于存放核心业务逻辑接口,接口最终由基础实施层(infra层工程)实现。

5.3.4. infra 层工程

在 mall-account-center 工程下创建基础实施层子工程,名称为:mall-account-center-infra

mall-account-center-infra 工程的 pom.xml 文件内容如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example.account</groupId>
        <artifactId>mall-account-center</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>mall-account-center-infra</artifactId>
    <name>mall-account-center-infra</name>
    <description>基础实施层</description>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--DIP here, Infrastructure depends on Domain-->
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-domain</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
    </dependencies>

</project>

mall-account-center-infra 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_Spring Boot3_10

其中,org.example.account.infra.mapper 包目录用于存放 mybatis 相关 mapper 接口,org.example.account.infra.impl 主要用于存放领域层(domain层)的接口实现类。

5.3.5. client 组件工程

在 mall-account-center 工程下创建 client sdk 子工程,名称为:mall-account-center-client

mall-account-center-client 工程的 pom.xml 文件内容如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example.account</groupId>
        <artifactId>mall-account-center</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>mall-account-center-client</artifactId>
    <name>mall-account-center-client</name>
    <description>api输出层</description>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
  
    <dependencies>
        
    </dependencies>
  
</project>

mall-account-center-client 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_项目架构_11

其中 org.example.account.client 包目录用于存放 OpenFeign 对外的接口。

5.3.6. start 组件工程

在 mall-account-center 工程下创建start组件子工程,名称为:mall-account-center-start。该工程主要负责启动账户模块的项目。

mall-account-center-start 工程的 pom.xml 文件内容如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example.account</groupId>
        <artifactId>mall-account-center</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>mall-account-center-start</artifactId>
    <name>mall-account-center-start</name>
    <description>启动层</description>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.example.account</groupId>
            <artifactId>mall-account-center-adapter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

mall-account-center-start 工程的 application.yml 文件内容如下。

server:
  port: 7030
spring:
  application:
    name: mall-account-service

# mysql configruation
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mall-account?serverTimezone=Asia/Shanghai&userUnicode=true&characterEncoding=utf-8
    username: root
    password: root

mall-account-center-start 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_项目架构_12

5.3.7. dto 组件工程

在 mall-account-center 工程下创建 dto 组件子工程,名称为:mall-account-center-dto。虽然COLA 规定 DTO 用于传输层,但考虑到很多情况下,数据对象从基础实施层出来,经过领域层、应用层,最后到达适配层的对象,字段都相差不大,为减少各层中对象的转换,同时减少 VO、DO 文件,加上本系列将整合 MyBatis-Plus,因此,本系列中,将 DTO 抽出来作为模块组件,在各个层中均可使用。当出现需要使用几个 DTO 组合作为接口返回的时候,则使用 VO 进行组装。

mall-account-center-dto 工程的 pom.xml 文件内容如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example.account</groupId>
        <artifactId>mall-account-center</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>mall-account-center-dto</artifactId>
    <name>mall-account-center-dto</name>

    <dependencies>
        <!--mall-component-common-->
        <dependency>
            <groupId>org.example.component</groupId>
            <artifactId>mall-component-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

mall-account-center-dto 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_Spring Cloud Alibaba_13

最后,账户模块工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_Spring Boot3_14

5.4. 商品模块

在 mall-demo 工程下创建商品模块工程,名称为:mall-product-center。该工程主要用来管理商品模块各子工程。参照账户模块,创建各层及组件子工程。

mall-product-center 工程的 pom.xml 文件内容如下。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>mall-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>org.example.product</groupId>
    <artifactId>mall-product-center</artifactId>
    <name>mall-product-center</name>
    <description>商品</description>
    <packaging>pom</packaging>

    <modules>
        <module>mall-product-center-dto</module>
        <module>mall-product-center-client</module>
        <module>mall-product-center-adapter</module>
        <module>mall-product-center-app</module>
        <module>mall-product-center-domain</module>
        <module>mall-product-center-infra</module>
        <module>mall-product-center-start</module>
    </modules>

    <!-- 统一管理jar包版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
 				<spring-boot.version>3.0.2</spring-boot.version>
        <spring-cloud.version>2022.0.0</spring-cloud.version>
        <spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version>
        <project.version>1.0.0-SNAPSHOT</project.version>
    </properties>

   <!-- 锁定版本,只是声明依赖,并未实现引入,子项目需要显示的声明需要使用的依赖 ,但不需要写groupId和version-->
    <dependencyManagement>
    <dependencies>
        <!--Project modules-->
        <dependency>
            <groupId>org.example.product</groupId>
            <artifactId>mall-product-center-dto</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.product</groupId>
            <artifactId>mall-product-center-client</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.product</groupId>
            <artifactId>mall-product-center-adapter</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.product</groupId>
            <artifactId>mall-product-center-app</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.product</groupId>
            <artifactId>mall-product-center-domain</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.product</groupId>
            <artifactId>mall-product-center-infra</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!--spring-boot-dependencies-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
         <!--spring cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version> ${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--spring cloud alibaba -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version> ${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

    </dependencies>
</dependencyManagement>

</project>

mall-product-center-start 工程中 application.yml 文件内容如下。

server:
  port: 7040
spring:
  application:
    name: mall-product-service

# mysql configruation
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mall-product?serverTimezone=Asia/Shanghai&userUnicode=true&characterEncoding=utf-8
    username: root
    password: root

mall-product-center 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_COLA_15

5.5. 订单模块

在 mall-demo 工程下创建订单模块工程,名称为:mall-order-center。该工程主要用来管理订单模块各子工程。参照账户模块,创建各层及组件子工程。

mall-order-center 工程的 pom.xml 文件内容如下。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>mall-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>org.example.order</groupId>
    <artifactId>mall-order-center</artifactId>
    <name>mall-order-center</name>
    <description>订单</description>
    <packaging>pom</packaging>

    <modules>
        <module>mall-order-center-dto</module>
        <module>mall-order-center-client</module>
        <module>mall-order-center-adapter</module>
        <module>mall-order-center-app</module>
        <module>mall-order-center-domain</module>
        <module>mall-order-center-infra</module>
        <module>mall-order-center-start</module>
    </modules>

    <!-- 统一管理jar包版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <spring-boot.version>3.0.2</spring-boot.version>
        <spring-cloud.version>2022.0.0</spring-cloud.version>
        <spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version>
        <project.version>1.0.0-SNAPSHOT</project.version>
    </properties>

   <!-- 锁定版本,只是声明依赖,并未实现引入,子项目需要显示的声明需要使用的依赖 ,但不需要写groupId和version-->
    <dependencyManagement>
    <dependencies>
        <!--Project modules-->
        <dependency>
            <groupId>org.example.order</groupId>
            <artifactId>mall-order-center-dto</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.order</groupId>
            <artifactId>mall-order-center-client</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.order</groupId>
            <artifactId>mall-order-center-adapter</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.order</groupId>
            <artifactId>mall-order-center-app</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.order</groupId>
            <artifactId>mall-order-center-domain</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.example.order</groupId>
            <artifactId>mall-order-center-infra</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--spring-boot-dependencies-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--spring cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version> ${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--spring cloud alibaba -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version> ${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

    </dependencies>
</dependencyManagement>

</project>

mall-order-center-start 工程中 application.yml 文件内容如下。

server:
  port: 7050
spring:
  application:
    name: mall-order-service

# mysql configruation
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mall-order?serverTimezone=Asia/Shanghai&userUnicode=true&characterEncoding=utf-8
    username: root
    password: root

mall-order-center 工程结构如下。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_COLA_16

最后,mall-demo 工程结构如下所示。

基于 COLA 架构的 Spring Cloud Alibaba(一)项目架构_Spring Cloud_17


6. 总结

本篇是基于 COLA 架构的 Spring Cloud Alibaba 系列的开篇,先简单的介绍了 COLA 架构和 Spring Cloud Alibaba 最新版本。然后介绍了账户模块、商品模块、订单模块的数据库设计。最后介绍了 mall-demo 工程中,组件模块、账户模块、商品模块、订单模块等工程的搭建。


基础篇项目代码:链接地址