PAYATHON 2026

Spring Boot Maven 插件 start 多实例但 stop 只停一个

数据小陈

start 之所以能启动多个实例,是因为每次 execution 都会启动一个新的 JVM;stop 默认没有实例级标识。未配置 jmxPort 时,stop 依赖项目构建目录中的进程信息来停止应用。多个 start execution 共用同一个位置时,后启动的实例会覆盖前一个,所以最后只能停掉一个。给每个实例使用唯一的 jmxPort,并在对应的 stop execution 里传入同一个 jmxPort,stop 就会通过 JMX 连接到指定实例,而不是靠共享的 PID 记录。

需要改的地方

保留原来的 start 配置不动,重点是在 post-integration-test-* 的 stop execution 中分别补上对应的 jmxPort:

<execution>
  <id>post-integration-test-fr</id>
  <phase>post-integration-test</phase>
  <goals>
    <goal>stop</goal>
  </goals>
  <configuration>
    <jmxPort>9001</jmxPort>
  </configuration>
</execution>

<execution>
  <id>post-integration-test-de</id>
  <phase>post-integration-test</phase>
  <goals>
    <goal>stop</goal>
  </goals>
  <configuration>
    <jmxPort>9002</jmxPort>
  </configuration>
</execution>

<execution>
  <id>post-integration-test-uk</id>
  <phase>post-integration-test</phase>
  <goals>
    <goal>stop</goal>
  </goals>
  <configuration>
    <jmxPort>9003</jmxPort>
  </configuration>
</execution>

这样 FR 的 start 用 9001,FR 的 stop 也连 9001;DE 用 9002;UK 用 9003。jmxPort 就是这里可用的实例级标识,不需要额外自定义 identifier。

注意事项

  • 三个 jmxPort 必须互不相同,并且不能被其他进程占用。
  • start 和 stop 中的端口必须一一对应,不能只给 start 配而 stop 不配。
  • 如果应用启用了 JMX 认证、SSL 或只监听特定网卡,Maven 进程要能连上对应的 JMX 端口。
  • 这些 execution 最好按顺序执行,不要在同一个项目里用并行构建同时跑多个 start,否则端口和启动顺序容易出现竞态。
  • 如果 stop 仍然只停一个,先检查 Maven 实际传给插件的配置是否生效,以及 spring-boot-maven-plugin 版本是否支持对应参数;不同大版本的参数细节可能有差异,以所用版本文档为准。

备注:内容仅供参考。