You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven多模块项目中如何按序运行所有Spring Boot应用?

我之前也遇到过一模一样的问题!mvn spring-boot:run在多模块项目里确实不会自动遍历所有子模块启动——它本质是个单模块运行目标,父目录执行时只会触发第一个匹配的子模块,而且就算你强制让它跑所有模块,Eureka Client也会因为Server还没就绪而注册失败。给你几个实用的解决方案,按推荐程度排序:

方案1:用Maven Profile + 顺序启动 + 延迟等待

这种方法完全基于Maven配置,适合本地开发时一键启动,能精准控制启动顺序和等待时间:

  1. 在父模块的pom.xml中添加一个专属Profile,定义启动流程:
<profiles>
    <profile>
        <id>start-all-services</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <!-- 第一步:启动Eureka Server -->
                        <execution>
                            <id>launch-eureka-server</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>start</goal>
                            </goals>
                            <configuration>
                                <!-- 替换成你的Server主类全路径 -->
                                <mainClass>com.example.EurekaServerApplication</mainClass>
                                <module>eureka-server-spring-boot-app</module>
                            </configuration>
                        </execution>
                        <!-- 第二步:等待10秒(可根据你的Server启动速度调整) -->
                        <execution>
                            <id>wait-for-server-ready</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <mainClass>java.lang.Thread</mainClass>
                                <arguments>
                                    <argument>sleep</argument>
                                    <argument>10000</argument>
                                </arguments>
                            </configuration>
                        </execution>
                        <!-- 第三步:启动第一个Client -->
                        <execution>
                            <id>launch-client-1</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>start</goal>
                            </goals>
                            <configuration>
                                <mainClass>com.example.EurekaClient1Application</mainClass>
                                <module>eureka-client-spring-boot-app-1</module>
                            </configuration>
                        </execution>
                        <!-- 后续Client依此类推 -->
                        <execution>
                            <id>launch-client-2</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>start</goal>
                            </goals>
                            <configuration>
                                <mainClass>com.example.EurekaClient2Application</mainClass>
                                <module>eureka-client-spring-boot-app-2</module>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
  1. 在父目录执行以下命令,一键构建并启动所有服务:
mvn clean install -P start-all-services

方案2:自定义启动脚本(更灵活)

如果喜欢用脚本控制,这种方式自由度更高,还能方便管理进程:

  1. 在父目录创建start-all.sh(Windows用户改成start-all.bat):
#!/bin/bash

# 先启动Eureka Server,后台运行
echo "Starting Eureka Server..."
cd eureka-server-spring-boot-app
mvn spring-boot:run &
SERVER_PID=$!

# 等待Server就绪(这里设15秒,可根据实际调整)
echo "Waiting 15 seconds for Eureka Server to initialize..."
sleep 15

# 启动Client 1
echo "Starting Eureka Client 1..."
cd ../eureka-client-spring-boot-app-1
mvn spring-boot:run &
CLIENT1_PID=$!

# 启动Client 2
echo "Starting Eureka Client 2..."
cd ../eureka-client-spring-boot-app-2
mvn spring-boot:run &
CLIENT2_PID=$!

# 保存进程ID到文件,方便后续停止服务
echo $SERVER_PID > ../server.pid
echo $CLIENT1_PID > ../client1.pid
echo $CLIENT2_PID > ../client2.pid

echo "All services started successfully!"
  1. 给脚本加执行权限(Linux/Mac):
chmod +x start-all.sh
  1. 直接运行脚本即可:
./start-all.sh

方案3:Spring Cloud CLI(专业微服务启动工具)

如果你的项目是Spring Cloud生态,Spring Cloud CLI能自动处理服务依赖顺序,不用手动加延迟:

  1. 先安装Spring Cloud CLI:
spring install org.springframework.cloud:spring-cloud-cli:latest.release
  1. 先打包所有模块:
mvn clean package -DskipTests
  1. 在父目录创建application.yml,配置所有服务的Jar包路径:
spring:
  cloud:
    deployer:
      local:
        working-directories: target
    app:
      eureka-server:
        path: eureka-server-spring-boot-app/target/eureka-server-spring-boot-app-1.0.0.jar
      client-1:
        path: eureka-client-spring-boot-app-1/target/eureka-client-spring-boot-app-1-1.0.0.jar
      client-2:
        path: eureka-client-spring-boot-app-2/target/eureka-client-spring-boot-app-2-1.0.0.jar
  1. 一键启动所有服务,CLI会自动等待Server就绪后再启动Client:
spring cloud eureka-server client-1 client-2

几个关键注意点

  • 端口冲突:确保每个服务的server.port配置不重复,否则启动会直接失败。
  • 启动延迟:如果你的Eureka Server启动较慢,要适当延长等待时间(比如改成20秒),可以通过访问http://localhost:8761确认Server是否就绪。
  • 依赖构建:启动前最好先执行mvn clean install,确保所有子模块的Jar包和依赖都正确生成。

内容的提问来源于stack exchange,提问作者Ashutosh Vaidya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:37:46