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

多Spring Boot应用共享同一端口的配置及打包问题咨询

How to Run Multiple Spring Boot Apps on the Same Port After Packaging

First, let's break down why your setup works locally but fails after packaging:

  • Local success with mvn spring-boot:run: Chances are you're running multiple modules from a parent Spring Boot project, where all apps are loaded into a single embedded Tomcat instance. This lets them share the same port while using distinct contextPath values.
  • Packaging failure: When you package each app as a standalone JAR, each JAR includes its own embedded Tomcat container. Trying to start multiple JARs with the same server.port will cause a port conflict—since each Tomcat instance tries to bind to the same port, the second app will fail to start.

Here are three practical solutions to fix this:

Solution 1: Package as WARs and Deploy to a Single External Tomcat

This replicates your local setup but uses a dedicated external Tomcat to host all apps:

  1. Modify each app's packaging type in pom.xml:
    <packaging>war</packaging>
    
  2. Exclude embedded Tomcat to avoid conflicts with the external one:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Add Tomcat Servlet API (only needed for compilation) -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-servlet-api</artifactId>
        <version>9.0.65</version> <!-- Match your Spring Boot compatible Tomcat version -->
        <scope>provided</scope>
    </dependency>
    
  3. Update the main application class to extend SpringBootServletInitializer:
    @SpringBootApplication
    public class App1Application extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(App1Application.class);
        }
    
        public static void main(String[] args) {
            SpringApplication.run(App1Application.class, args);
        }
    }
    
  4. Package and deploy:
    • Run mvn clean package -Pdev for each app to generate WAR files.
    • Copy all WARs to your external Tomcat's webapps directory. Tomcat will use the WAR filename as the default contextPath, or you can customize it via Tomcat's server.xml.

All apps will now run on the external Tomcat's port (e.g., 8597) with their respective context paths.

Solution 2: Use Spring Cloud Gateway as a Unified Entry Point

This approach keeps each app as a standalone JAR but uses a gateway to route traffic from a single port to different apps:

  1. Create a Spring Cloud Gateway app with port 8597:
    • Add the spring-cloud-starter-gateway dependency to its pom.xml.
  2. Configure routing rules in application.yml:
    server:
      port: 8597
    spring:
      cloud:
        gateway:
          routes:
            - id: app1_route
              uri: http://localhost:8081
              predicates:
                - Path=/app1/**
              filters:
                - StripPrefix=1 # Remove the /app1 prefix when forwarding to app1
            - id: app2_route
              uri: http://localhost:8082
              predicates:
                - Path=/app2/**
              filters:
                - StripPrefix=1
    
  3. Configure each backend app with unique ports (e.g., app1 uses 8081, app2 uses 8082), package them as JARs, and start each one.
  4. Access apps via the gateway:
    • Visit http://localhost:8597/app1 to reach app1, and http://localhost:8597/app2 to reach app2.

Solution 3: Merge Apps into a Single Multi-Module Spring Boot Project (If Possible)

If your apps are closely related, you can bundle them into one project:

  1. Structure your project with a parent Spring Boot module and child modules for each app.
  2. Configure each child module's context path in its application.properties:
    server.servlet.context-path=/app1
    
  3. Start the parent module with mvn spring-boot:run—all child apps will load into the same embedded Tomcat instance.
  4. Package the parent module as a single JAR; running it will host all apps on the same port with their distinct context paths.

Key Notes

  • Double-check your local setup: If you're running each app individually with mvn spring-boot:run, you should see a port conflict—so you likely started them from a parent module that loads all apps into one Tomcat instance.
  • For WAR deployment, ensure your external Tomcat version is compatible with your Spring Boot version (e.g., Spring Boot 2.7.x works with Tomcat 9.x; Spring Boot 3.x requires Tomcat 10.x).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:41:53