需求案例:搭建一个电商平台项目,该平台包括用户服务、订单服务、通用工具模块等。
项目架构:
- 用户服务:负责处理用户相关的逻辑,例如用户信息的管理、用户注册、登录等。
- 订单服务:负责处理订单相关的逻辑,例如订单的创建、订单支付、退货、订单查看等。
- 通用模块:负责存储其他服务需要通用工具类,其他服务依赖此模块。
服务依赖:
- 用户服务 (1.0.1)
- spring-context 6.0.6
- spring-core 6.0.6
- spring-beans 6.0.6
- jackson-databind / jackson-core / jackson-annotations 2.15.0
- 订单服务 (1.0.1)
- shiro-core 1.10.1
- spring-context 6.0.6
- spring-core 6.0.6
- spring-beans 6.0.6
- 通用模块 (1.0.1)
- commons-io 2.11.0
1 创建父工程 maven-micro-shop
<?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>per.mjn</groupId>
<artifactId>maven-micro-shop</artifactId>
<version>1.0.1</version>
<packaging>pom</packaging>
<modules>
<module>user-service</module>
<module>order-service</module>
<module>common-service</module>
</modules>
<properties>
<spring.version>6.0.6</spring.version>
<jackson.version>2.15.0</jackson.version>
<shiro.version>1.10.1</shiro.version>
<io.version>2.11.0</io.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${io.version}</version>
</dependency>
<dependency>
<groupId>per.mjn</groupId>
<artifactId>common-service</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2 创建通用模块 common-service
<?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>per.mjn</groupId>
<artifactId>maven-micro-shop</artifactId>
<version>1.0.1</version>
</parent>
<artifactId>common-service</artifactId>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>
</project>
使用maven中的构建命令install,将这个项目放到maven本地仓库,以供其他两个模块(user-service 和 order-service)使用。
显示出BUILD SUCCESS表示构建成功。
3 创建用户模块 user-service
<?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>per.mjn</groupId>
<artifactId>maven-micro-shop</artifactId>
<version>1.0.1</version>
</parent>
<artifactId>user-service</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- 引入自己的工程 -->
<dependency>
<groupId>per.mjn</groupId>
<artifactId>common-service</artifactId>
</dependency>
</dependencies>
</project>
4 创建订单模块 order-service
<?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>per.mjn</groupId>
<artifactId>maven-micro-shop</artifactId>
<version>1.0.1</version>
</parent>
<artifactId>order-service</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
<!-- 引入自己的工程 -->
<dependency>
<groupId>per.mjn</groupId>
<artifactId>common-service</artifactId>
</dependency>
</dependencies>
</project>