多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 distinctcontextPathvalues. - 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.portwill 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:
- Modify each app's packaging type in
pom.xml:<packaging>war</packaging> - 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> - 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); } } - Package and deploy:
- Run
mvn clean package -Pdevfor each app to generate WAR files. - Copy all WARs to your external Tomcat's
webappsdirectory. Tomcat will use the WAR filename as the defaultcontextPath, or you can customize it via Tomcat'sserver.xml.
- Run
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:
- Create a Spring Cloud Gateway app with port 8597:
- Add the
spring-cloud-starter-gatewaydependency to itspom.xml.
- Add the
- 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 - Configure each backend app with unique ports (e.g., app1 uses 8081, app2 uses 8082), package them as JARs, and start each one.
- Access apps via the gateway:
- Visit
http://localhost:8597/app1to reach app1, andhttp://localhost:8597/app2to reach app2.
- Visit
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:
- Structure your project with a parent Spring Boot module and child modules for each app.
- Configure each child module's context path in its
application.properties:server.servlet.context-path=/app1 - Start the parent module with
mvn spring-boot:run—all child apps will load into the same embedded Tomcat instance. - 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
相关产品推荐
相关产品推荐

