微服务商城构筑其一
真正需要启动的地方使用SpringBoot项目,其他地方使用普通的JavaMaven项目即可。
父项目只有一个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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.situ</groupId><artifactId>shoplook2025-parent</artifactId><version>1.0.0</version><packaging>pom</packaging><modules><module>shoplook2025-core</module><module>shoplook2025-spi</module><module>shoplook2025-api</module></modules><properties><maven.compiler.source>21</maven.compiler.source><maven.compiler.target>21</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>3.4.8</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-jsqlparser</artifactId><version>3.5.12</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.12</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2024.0.2</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2023.0.3.3</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.38</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-annotation</artifactId><version>3.5.12</version></dependency></dependencies></dependencyManagement><build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.14.0</version></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>3.5.5</version></plugin></plugins></pluginManagement></build>
</project>
这里的是api层,也就是启动所在的项目,需要引入切实的插件和依赖
<?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"><modelVersion>4.0.0</modelVersion><parent><groupId>com.situ</groupId><artifactId>shoplook2025-parent</artifactId><version>1.0.0</version></parent><modules><module>shoplook2025-category-api</module><module>shoplook2025-brand-api</module></modules><artifactId>shoplook2025-api</artifactId><version>1.0.0</version><packaging>pom</packaging><properties><maven.compiler.source>21</maven.compiler.source><maven.compiler.target>21</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-jsqlparser</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><parameters>true</parameters><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></path></annotationProcessorPaths></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build>
</project>
像spring-boot-starter-web这种根基依赖,导入到该层的父项目即可,不能确保某些依赖会全部用得到,因此首先思考单独导入某些依赖,严谨!
目录结构顺序:
- 父项目,仅pom
- core层,目前存放工具类
- spi层,功能子模块下存放对应的实体类、service接口
- api层,存放dao层和serviceImpl、api层、config层,是微服务架构最重要的一层
像core层,一般有个lombok就够了,校验可以加validation,json数据格式化可以加jackson-annotations
最顶层父项目可以进行如spring-boot-dependencies、spring-cloud-dependencies、spring-cloud-alibaba-dependencies、lombok等依赖的管理,使用频率还是蛮高的
记得在真正插件生效的pom里,加
<parameters>true</parameters>、
<executions><execution><goals><goal>repackage</goal></goals></execution></executions>
还记得如果pom作为父项目,就要写modules,子模块就要写parent,当然了二者可以共存,父项目还要记得
<packaging>pom</packaging>
的书写,是你的父项目仅作为提供依赖的包