微服务模块搭建与解析
1 微服务模块的搭建
一般来说微服务工程主要分为三大类工程:
- 父工程、基础工程 和微服务工程。
最终项目结构:
此处只创建了content一个微服务,其他服务模块类似
1.1 新建一个项目springcloud-plus-pro
1.2 新建springcloud-plus-parent
将springcloud-plus-parent设置为pom
<?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>com.example</groupId>
<artifactId>springcloud-plus-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-plus-parent</name>
<description>springcloud-plus-parent</description>
<!--设置为pom文件-->
<packaging>pom</packaging>
<!--版本管理-->
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
<spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
<org.projectlombok.version>1.18.8</org.projectlombok.version>
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<fastjson.version>1.2.83</fastjson.version>
<druid-spring-boot-starter.version>1.2.8</druid-spring-boot-starter.version>
<mysql-connector-java.version>8.0.30</mysql-connector-java.version>
<mybatis-plus-boot-starter.version>3.4.1</mybatis-plus-boot-starter.version>
<commons-lang.version>2.6</commons-lang.version>
<minio.version>8.4.3</minio.version>
<xxl-job-core.version>2.3.1</xxl-job-core.version>
<swagger-annotations.version>1.5.20</swagger-annotations.version>
<commons-lang3.version>3.10</commons-lang3.version>
<okhttp.version>4.8.1</okhttp.version>
<swagger-spring-boot-starter.version>1.9.0.RELEASE</swagger-spring-boot-starter.version>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
<dependencyManagement>
<dependencies>
<!--通用依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
<!-- lombok,简化类的构建-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
<!-- fastjson ,json解析工具 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- druid 连接池管理 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-spring-boot-starter.version}</version>
</dependency>
<!-- mySQL数据库驱动包管理 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<!-- mybatis plus 集成Spring Boot启动器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.example.SpringCloudPlusParentApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
项目结构:
1.3 新建springcloud-plus-base
新建基础工程,父工程为springcloud-plus-parent,目录上平级,因此pom依然需要跳一级(
../
)
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>
<parent>
<artifactId>springcloud-plus-parent</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../springcloud-plus-parent</relativePath>
</parent>
<groupId>com.example</groupId>
<artifactId>springcloud-plus-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-plus-base</name>
<description>springcloud-plus-base</description>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--此处只展示部分依赖...-->
</dependencies>
</project>
注意:
base模块下的配置文件可以先删除或者不配置(application.properties)
1.4 新建springcloud-plus-content及其子模块(api、service、model)
新建微服务模块,以:springcloud-plus-content(内容管理模块)为例
将项目修改为pom,删除多余部分
新建其子模块:
1. 右击springcloud-plus-content
2. 选择new - module
分别新建springcloud-plus-content-api、springcloud-plus-content-model、springcloud-plus-content-service模块
- springcloud-plus-content的pom文件
<parent>
<artifactId>springcloud-plus-parent</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
<!--因为目录上面是平级,因此需要先../-->
<relativePath>../springcloud-plus-parent</relativePath>
</parent>
<artifactId>springcloud-plus-content</artifactId>
<packaging>pom</packaging>
<modules>
<module>springcloud-plus-content-api</module>
<module>springcloud-plus-content-model</module>
<module>springcloud-plus-content-service</module>
</modules>
- springcloud-plus-content-api模块的pom文件(
model、service同理
)
<parent>
<artifactId>springcloud-plus-content</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>springcloud-plus-content-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-plus-content-api</name>
<description>springcloud-plus-content-api</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--只展示了部分依赖-->
</dependencies>
最终项目结构:
2 解析微服务模块(搭建流程)
- 创建空项目
将所有项目全部放在该文件夹下
例如:springcloud-plus-pro
- 创建parent项目
创建总体的父工程,其他不必要文件删除,只留下pom.xml和.iml文件(package为pom),主要管理依赖版本
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
...
</dependencies>
</dependencyManagement>
<build>
...
</build>
例如:
springcloud-plus-parent
- 创建base项目
创建项目总体的base工程,基础工程,pom文件中,依赖spring-plus-parent,文件目录上与springcloud-plus-parent平级【其实也可以将base模块也理解为一个服务】
例如:springcloud-plus-base
- 创建微服务模块
创建具体模块,依赖于
springcloud-plus-parent
总体的pom,以内容管理模块为例(content),文件目录与springcloud-plus-parent平级
- 创建微服务的父工程
创建具体服务的父工程,删除不必要文件,留下pom.xml,依赖于springcloud-plus-parent,里面添加模块,管理后面创建的api、service、model
例如:springcloud-plus-contnet
- 创建微服务中的子模块之model
创建内容管理模块中的model,pom依赖于springcloud-plus-content
- 创建微服务中的子模块之api
创建内容管理模块中的api,pom依赖于springcloud-plus-content
api的配置文件使用bootstrap.yml,其他model、service使用application.yml,文件优先级。api中的pom依赖content-model和content-service,这样数据库配置可以只加载一次
<dependency>
<groupId>com.zi</groupId>
<artifactId>zi-plus-content-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.zi</groupId>
<artifactId>zi-plus-content-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- 创建微服务中的子模块之service
创建内容管理模块中的service,pom依赖于springcloud-plus-content
- 添加模块管理
在内容管理模块中,添加子模块管理
<modules>
<module>springcloud-plus-content-api</module>
<module>springcloud-plus-content-model</module>
<module>springcloud-plus-content-service</module>
</modules>