创建推荐微服务编码
前文主要介绍功能模块的由来及表结构字段设计,接下来讲解具体的代码改造过程。本节的源代码是在 newbee-mall-cloud-dev-step07
工程的基础上改造的,将工程命名为 newbee-mall-cloud-dev-step08
。
在工程中新增一个 newbee-mall-cloud-recommend-service
模块,并在 pom.xml
主文件中增加该模块的配置,代码如下:
<modules>
<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-admin</module>
<module>newbee-mall-cloud-common</module>
</modules>
该模块的目录结构设置与其他的功能模块的目录结构设置类似,如下所示:
newbee-mall-cloud-recommend-service // 推荐微服务
├── newbee-mall-cloud-recommend-api // 存放商品推荐管理模块中暴露的用于远程调用的 FeignClient 类
└── newbee-mall-cloud-recommend-web // 商品推荐管理模块 API 的代码及逻辑
在新增推荐微服务时,主要参考了当时新增商品微服务时的步骤。笔者直接将 newbee-mall-cloud-dev-step05
源代码下 newbee-mall-cloud-goods-service
模块中的代码复制了过来,并修改了模块名称和目录名称。
最终,子节点 newbee-mall-cloud-recommend-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/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ltd.newbee.cloud</groupId>
<artifactId>newbee-mall-cloud-recommend-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>newbee-mall-cloud-recommend-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-recommend-web</module>
<module>newbee-mall-cloud-recommend-api</module>
</modules>
<dependencies>
</dependencies>
</project>
主要的配置项是模块名称、模块的打包方式及模块间的父子关系。其中,重点是配置父模块 newbee-mall-cloud
,配置的两个子模块分别是 newbee-mall-cloud-recommend-api
和 newbee-mall-cloud-recommend-web
。
newbee-mall-cloud-recommend-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.newbee.cloud</groupId>
<artifactId>newbee-mall-cloud-recommend-api</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>newbee-mall-cloud-recommend-api</name>
<description>推荐服务 openfeign</description>
<parent>
<groupId>ltd.newbee.cloud</groupId>
<artifactId>newbee-mall-cloud-recommend-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-recommend-service
的关系,打包方式 packaging
配置项的值为 jar
。同时,由于后续可能要将接口暴露,因此这里添加 FeignClient
类,引入了 OpenFeign
的依赖项。
newbee-mall-cloud-recommend-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.newbee.cloud</groupId>
<artifactId>newbee-mall-cloud-recommend-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>newbee-mall-cloud-recommend-web</name>
<description>推荐微服务</description>
<parent>
<groupId>ltd.newbee.cloud</groupId>
<artifactId>newbee-mall-cloud-recommend-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>
</dependencies>
</project>
这里定义了与父模块 newbee-mall-cloud-recommend-service
的关系,同时将相关的业务依赖项移到该配置文件中,因为轮播图管理和商品推荐管理模块的主要业务代码都写在这个模块中。
因为改造过程中直接复制了 newbee-mall-cloud-dev-step05
源代码下 newbee-mall-cloud-goods-service
模块中的代码,所以在修改完依赖配置后,就要修改包名,把 ltd.goods.cloud.xxx
的名称修改为 ltd.recommend.cloud.xxx
,之后修改 config
包中的代码,包括全局异常处理配置类、Swagger
配置类、自定义 MVC
配置类,主要修改了这些类的类名。这样就完成了一个推荐微服务的初始构建工作,此时的 newbee-mall-cloud-recommend-service
模块中并没有业务代码,目录结构如图 5-6 所示。

本节中并未涉及具体的业务编码,主要介绍推荐微服务的模块功能和表结构设计,以及在项目中完成推荐微服务的初始化构建,后续关于推荐微服务改造的实战章节都是基于当前项目完成的。读者可以根据这些代码自己动手完成服务化编码,如拿到这份代码之后,以此为基础,自行完成推荐微服务模块所有代码的功能。如果自己实现耗费时间,也可以按照笔者给出的步骤完成服务化拆分。