前言:在上一章节中我们创建了两个个SpringCloud工程,但在两个工程中分别存在着一些重复的部分,例如重复的实体类(如图所示),这样会造成系统的冗余,所以我们需要把公共的类提取到一个工程里,让另外两个工程可以公用。(除了用于放置重复的代码。也可以用于放置服务接口、工具类、第三方接口等有需要公用的代码)
1、在父工程下新建模块
2、选择模块的项目类型为Maven并选择模块要使用的JDK版本
3、填写子模块的名称,然后点完成即可完成创建
效果图:
4、修改api-commons子模块的pom.xml文件,然后reolad一下,下载依赖
例:
<?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"><parent><artifactId>springcloud01</artifactId><groupId>com.ken.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>api-commons</artifactId><dependencies><!--热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!--lombok插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--hutool工具包--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.1.0</version></dependency></dependencies>
</project>
5、新建实体类包com.ken.springcloud.entities
效果图:
6、复制公共的实体类,然后粘贴到api-commons的子模块下的实体类包entities包下
(1)复制实体类
(2)把实体类粘贴到api-commons子模块里的entities包里
效果图:
7、把项目打包发布上传到本地Maven仓库里,供其他两个项目公用实体类
(1)打开Maven窗口,执行clean命令,测试Maven环境配置及清理target包
效果图:
(2)执行install命令,下载安装到我们的本地Maven仓库里
效果图:
8、分别删除其他两个子模块里的entities包
(1)删除consumer-order80模块下的entities包
(2)删除provider-payment8001模块下的entities包
效果图:
9、从Maven仓库引入install后打包成的jar包
(1)修改consumer-order80模块的pom.xml文件内容,从而引入jar包
例:
<?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"><parent><artifactId>springcloud01</artifactId><groupId>com.ken.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>provider-payment8001</artifactId><dependencies><!--以下依赖都没写版本号,没写版本号的情况下会引用父项目的版本--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--监控--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--Mybatis和SpringBoot的整合--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId></dependency><!--mysql-connector-java--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--jdbc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!--热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!--lombok插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--引入自己定义的api调用包,可以使用Payment模块的entity--><dependency><groupId>com.ken.springcloud</groupId><artifactId>api-commons</artifactId><version>${project.version}</version></dependency></dependencies>
</project>
效果图:
(2)修改provider-payment8001模块的pom.xml文件内容,从而引入jar包
例:
<?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"><parent><artifactId>springcloud01</artifactId><groupId>com.ken.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>provider-payment8001</artifactId><dependencies><!--以下依赖都没写版本号,没写版本号的情况下会引用父项目的版本--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--监控--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--Mybatis和SpringBoot的整合--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId></dependency><!--mysql-connector-java--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--jdbc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!--热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><!--lombok插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--引入自己定义的api调用包,可以使用Payment模块的entity--><dependency><groupId>com.ken.springcloud</groupId><artifactId>api-commons</artifactId><version>${project.version}</version></dependency></dependencies></project>
效果图:
ps:引入后需要重新加载Maven项目从而引入jar包,否则idea会因为检测不了jar包从而报错
或者
10、分别运行consumer_order80模块和provider-payment8001模块
consumer_order80模块运行效果图
provider-payment8001模块运行效果图
11、用工具测试接口看模块是否正常运行(我这里测试接口用的工具的postman)
(1)测试新增
在地址栏输入http://localhost/consumer/insert?serial=ken3并点击send调用接口,可以看到数据插入成功
效果图:
(2)测试查询
在地址栏输入http://localhost/consumer/get/3并点击send调用接口,可以看到数据返回成功
效果图:
结语:
至此,我们完成了微服务工程公共部分的提取