创建购物车微服务模块

本节介绍具体的代码改造过程,本节的源代码是在 newbee-mall-cloud-dev-step13-2 工程的基础上改造的,将工程命名为 newbee-mall-cloud-dev-step14

在工程中新增一个 newbee-mall-cloud-shop-cart-service 模块,并在 pom.xml 主文件中增加该模块的配置,代码如下:

<modules>
    <module>newbee-mall-cloud-shop-cart-service</module>
    <module>newbee-mall-cloud-recommend-service</module>
    <module>newbee-mall-cloud-goods-service</module>
    <module>newbee-mall-cloud-user-service</module>
    <module>newbee-mall-cloud-gateway-mall</module>
    <module>newbee-mall-cloud-gateway-admin</module>
    <module>newbee-mall-cloud-common</module>
</modules>

该模块的目录结构设置与其他功能模块的目录结构设置类似,如下所示:

newbee-mall-cloud-shop-cart-service         // 购物车微服务
├── newbee-mall-cloud-shop-cart-api         // 存放购物车模块中暴露的用于远程调用的 FeignClient 类
└── newbee-mall-cloud-shop-cart-web         // 购物车 API 的代码及逻辑

在新增购物车微服务时,主要参考了新增商品微服务时的步骤。笔者直接将 newbee-mall-cloud-dev-step05 源代码下 newbee-mall-cloud-goods-service 模块中的代码复制过来,之后对模块名称和目录名称进行修改。

最终,子节点 newbee-mall-cloud-shop-cart-service 模块的 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/xsd/maven-4.0.0.xsd https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ltd.newbee.cloud</groupId>
    <artifactId>newbee-mall-cloud-shop-cart-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>newbee-mall-cloud-shop-cart-service</name>
    <description>购物车模块</description>

    <parent>
        <groupId>ltd.newbee.cloud</groupId>
        <artifactId>newbee-mall-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <modules>
        <module>newbee-mall-cloud-shop-cart-web</module>
        <module>newbee-mall-cloud-shop-cart-api</module>
    </modules>

    <dependencies>
    </dependencies>
</project>

主要的配置项是模块名称、模块的打包方式及模块间的父子关系。其中,重点配置了父模块 newbee-mall-cloud,两个子模块分别是 newbee-mall-cloud-shop-cart-apinewbee-mall-cloud-shop-cart-web

newbee-mall-cloud-shop-cart-api 模块的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ltd.shopcart.newbee.cloud</groupId>
    <artifactId>newbee-mall-cloud-shop-cart-api</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>newbee-mall-cloud-shop-cart-api</name>
    <description>购物车微服务 openfeign</description>

    <parent>
        <groupId>ltd.newbee.cloud</groupId>
        <artifactId>newbee-mall-cloud-shop-cart-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>ltd.newbee.cloud</groupId>
            <artifactId>newbee-mall-cloud-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

上述代码定义了与父模块 newbee-mall-cloud-shop-cart-service 的关系,打包方式 packaging 配置项的值为 jar。同时,由于后续可能要将接口暴露,因此这里引入了 OpenFeign 的依赖项。

newbee-mall-cloud-shop-cart-web 模块的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ltd.shopcart.newbee.cloud</groupId>
    <artifactId>newbee-mall-cloud-shop-cart-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>newbee-mall-cloud-shop-cart-web</name>
    <description>购物车微服务</description>
    <parent>
        <groupId>ltd.newbee.cloud</groupId>
        <artifactId>newbee-mall-cloud-shop-cart-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <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>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>ltd.newbee.cloud</groupId>
            <artifactId>newbee-mall-cloud-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>ltd.user.newbee.cloud</groupId>
            <artifactId>newbee-mall-cloud-user-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>ltd.goods.newbee.cloud</groupId>
            <artifactId>newbee-mall-goods-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>
</project>

上述代码定义了与父模块 newbee-mall-cloud-shop-cart-service 的关系,同时将相关的业务依赖项也移到该配置文件中,因为购物车模块的主要业务代码都写在这个模块中。

由于改造过程中直接复制了 newbee-mall-cloud-dev-step05 源代码下 newbee-mall-cloud-goods-service 模块中的代码,因此在修改完依赖配置后,需要修改包名,把 ltd.goods.cloud.xxx 的名称修改为 ltd.shopcart.cloud.xxx,并修改 config 包中的代码,包括全局异常处理配置类、Swagger 配置类、自定义 MVC 配置类,主要修改了这些类的类名。这样就完成了一个购物车微服务的初始构建工作。