创建商品微服务编码
前文主要介绍商品信息表的结构字段及主要的功能,接下来讲解具体的代码改造过程。本节的源代码是在 newbee-mall-cloud-dev-step04
工程的基础上改造的,将工程命名为 newbee-mall-cloud-dev-step05
。
在工程中新增一个 newbee-mall-cloud-goods-service
模块,并在 pom.xml
主文件中增加该模块的配置,代码如下:
<modules>
<module>newbee-mall-cloud-goods-service</module>
<module>newbee-mall-cloud-user-service</module>
<module>newbee-mall-cloud-gateway-admin</module>
<module>newbee-mall-cloud-common</module>
</modules>
该模块的目录结构设置与 newbee-mall-cloud-user-service
模块的目录结构设置类似,如下所示:
newbee-mall-cloud-goods-service // 商品微服务
├── newbee-mall-cloud-goods-api // 存放商品模块中暴露的用于远程调用的 FeignClient 类
└── newbee-mall-cloud-goods-web // 商品模块 API 的代码及逻辑
在开发时,笔者直接将 newbee-mall-cloud-user-service
模块中的代码复制了过来,之后对模块名称和目录名称进行了修改。
子节点 newbee-mall-cloud-goods-service
模块的 pom.xml
文件代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apa***.org/POM/4.0.0"
xmlns:xsi="http://www.w*.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apa***.org/POM/4.0.0
https://maven.apa***.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ltd.newbee.cloud</groupId>
<artifactId>newbee-mall-cloud-goods-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>newbee-mall-cloud-goods-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-goods-web</module>
<module>newbee-mall-cloud-goods-api</module>
</modules>
<dependencies>
</dependencies>
</project>
主要的配置项是模块名称、模块的打包方式及模块间的父子关系。其中,重点配置了父模块 newbee-mall-cloud
,两个子模块分别是 newbee-mall-cloud-goods-api
和 newbee-mall-cloud-goods-web
。
newbee-mall-cloud-goods-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.user.newbee.cloud</groupId>
<artifactId>newbee-mall-cloud-goods-api</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>newbee-mall-cloud-goods-api</name>
<description>商品微服务 openfeign</description>
<parent>
<groupId>ltd.newbee.cloud</groupId>
<artifactId>newbee-mall-cloud-goods-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>
</dependencies>
</project>
这里定义了与父模块 newbee-mall-cloud-goods-service
的关系,打包方式 packaging
配置项的值为 jar
。同时,因为后面要添加 FeignClient
类,所以这里引入了 OpenFeign
。
newbee-mall-cloud-goods-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.user.newbee.cloud</groupId>
<artifactId>newbee-mall-cloud-goods-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>newbee-mall-cloud-goods-web</name>
<description>商品微服务</description>
<parent>
<groupId>ltd.user.newbee.cloud</groupId>
<artifactId>newbee-mall-cloud-goods-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>
<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>
</dependencies>
</project>
这里定义了与父模块 newbee-mall-cloud-goods-service
的关系,同时将相关的业务依赖项移到该配置文件中,因为商品功能模块的主要业务代码都在这个模块中。
由于改造过程中直接复制了 newbee-mall-cloud-user-service
模块的代码,因此在修改完依赖配置后,要修改包名,把 ltd.user.cloud.xxx
的名称改为 ltd.goods.cloud.xxx
,之后修改和删除复制过来的 Java
文件和 Mapper
文件,因为这些代码与商品微服务并无关系。唯一没有删除的是 config
包中的代码,因为全局异常处理配置类、Swagger
配置类、自定义 MVC
配置类在商品微服务中也是必要的,所以只修改了这些类的类名。最后,修改 application.properties
配置文件,删除与 Redis
连接的相关配置。
这样,就完成了一个新模块的初始构建工作,得到了一份较为纯净的模块代码,此时的 newbee-mall-cloud-goods-service
模块中并没有业务代码,目录结构如图 4-7 所示。在后续开发步骤中,要添加其他微服务到项目中,可以按照以上步骤来完成,或者直接复制 newbee-mall-cloud-dev-step05
源代码下的 newbee-mall-cloud-goods-service
模块到工程中,修改一下名称即可。
本节的内容并未涉及业务编码,主要介绍商品微服务的功能和表结构设计,以及在项目中完成商品微服务的初始化构建。读者可以根据这些代码自己动手完成微服务编码,如拿到这份代码后,以此为基础,自行完成商品微服务所有代码的功能。如果自己实现耗费时间,也可以按照笔者给出的步骤来完成微服务拆分。
