学无止境
欢迎关注我的公众号
接收定期更新文章
【微服务实战】Spring Boot 多模块项目搭建:为微服务拆分打好基础
2026-01-12 11:47:27
732

 

多模块项目准备

Spring Boot 是 Spring 官方推出的快速开发框架,内嵌服务器、自动配置,无需 XML,几行代码即可构建生产级应用。它简化依赖管理,支持微服务与云原生,让 Java 开发更高效、更轻量。
spring Boot 版本 3.3.7

  • common 模块:存放公共工具类、实体、常量等

  • home 模块:Web 应用,端口 8081

  • user 模块:Web 应用,端口 8082


1、整体项目结构

      
1
2
3
4
5
6
7
8
9
10
11
12
13

springboot/     ← 根目录(聚合项目)
├── pom.xml  ← 父 POM(继承 spring-boot-starter-parent)
├── common/
│   └── pom.xml
│   └── src/main/java/com/tsphp/common/...
├── home/
│   └── pom.xml
│   └── src/main/java/com/tsphp/home/HomeApplication.java
│   └── src/main/resources/application.yml
└── user/
    └── pom.xml
    └── src/main/java/com/tsphp/user/UserApplication.java
    └── src/main/resources/application.yml


2、父 POM:springboot/pom.xml

      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

<?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.tsphp</groupId>
    <artifactId>springboot</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>common</module>
        <module>home</module>
        <module>user</module>
    </modules>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.7</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
 
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
</project>

📌 注意:<packaging>pom</packaging> 表示这是聚合项目,不生成 JAR。


3、common 模块:common/pom.xml

      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

<?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>springboot</artifactId>
        <groupId>com.tsphp</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>common</artifactId>
 
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
</project>

📌 common 模块 不要加 spring-boot-starter-web,它只是普通 JAR。


4、home 模块:home/pom.xml

      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

<?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>springboot</artifactId>
        <groupId>com.tsphp</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>home</artifactId>
 
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>com.tsphp</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
 
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**.*</include>
                    <include>**/**.*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.tsphp.home.HomeApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

home/src/main/resources/application.yml

      
1
2
3
4
5

server:
  port: 8081
spring:
  application:
    name: home-service

home/src/main/java/com/tsphp/home/HomeApplication.java

      
1
2
3
4
5
6
7
8
9
10
11

package com.tsphp.home;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class HomeApplication {
    public static void main(String[] args) {
        SpringApplication.run(HomeApplication.class, args);
    }
}


5、user 模块:user/pom.xml

      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

<?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>springboot</artifactId>
        <groupId>com.tsphp</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>user</artifactId>
 
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>com.tsphp</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
 
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**.*</include>
                    <include>**/**.*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.tsphp.user.UserApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

user/src/main/resources/application.yml

      
1
2
3
4
5
6

server:
  port: 8082
 
spring:
  application:
    name: user-service

user/src/main/java/com/tsphp/user/UserApplication.java

      
1
2
3
4
5
6
7
8
9
10
11

package com.tsphp.user;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
}

6、注意事项

事项说明
包路径所有模块的 Java 类必须在 com.tsphp.xxx 下,否则 Spring Boot 扫描不到
端口冲突确保 8081 和 8082 未被占用
模块依赖子模块之间不能循环依赖(如 home 依赖 user,user 又依赖 home)

7、验证是否成功

home 中新建 Controller:

      
1
2
3
4
5
6
7
8
9

@RestController
@RequestMapping("/home")
public class Index {
 
    @RequestMapping("/index")
    public Result index() {
        return ResultUnit.wrapSuccess("hello world");
    }
}

访问 http://localhost:8081/home/index 应返回
{"errcode":0,"errmsg":"","data":"hello world"}


user 中新建 Controller:

      
1
2
3
4
5
6
7
8
9

@RestController
@RequestMapping("/user")
public class Index {
 
    @RequestMapping("/index")
    public Result index() {
        return ResultUnit.wrapSuccess("ok");
    }
}

访问 http://localhost:8082/user/index 应返回
{"errcode":0,"errmsg":"","data":"ok"}

gitee源码: https://gitee.com/wangshi90/springboot


【小结】

今天成功搭好了多模块骨架,终于告别“所有代码塞一个包”的混沌时代!
模块清晰、职责分明,连 IDE 自动补全都变快了!✨
这不是简单的工程拆分,而是为微服务架构打下第一块基石!
接下来,每个模块都能独立演变成真正的微服务,自由注册、互相调用、弹性伸缩~
如果这套结构让你眼前一亮、心头一松,甚至少翻了几页祖传屎山代码——
下期见!
愿你的项目结构清爽如风,依赖关系清晰如镜,
启动秒开不卡顿,打包一次就成功,
服务稳如磐石,协作丝滑如德芙 —— 连 PM 都忍不住夸你“这周上线好稳”! 🚀

 


Spring Boot
多模块项目
Maven聚合
Web应用
欢迎关注我的公众号,获取更多文章