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

如何一次性运行GitHub上含Angular与2个Spring Boot模块的项目?

解决一次性运行Angular + 两个Spring Boot模块并测试的问题

首先,咱们先搞清楚你分别启动后无法正常工作的核心原因——大概率是跨域问题:Angular默认跑在http://localhost:4200,而两个Spring Boot服务通常用不同的端口(比如8080、8081),浏览器的同源策略会阻止跨域请求,导致UI和后端无法通信。先把这个问题解决,再谈一次性启动:

先修复分别启动后的跨域问题

方法1:在Spring Boot中配置跨域

给每个Spring Boot模块添加跨域配置类,允许来自Angular端口的请求:

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:4200")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true);
    }
}

方法2:在Angular中配置代理转发

创建proxy.conf.json文件,把API请求转发到对应的Spring Boot服务:

{
  "/api/v1/*": {
    "target": "http://localhost:8080",
    "secure": false,
    "changeOrigin": true
  },
  "/api/v2/*": {
    "target": "http://localhost:8081",
    "secure": false,
    "changeOrigin": true
  }
}

然后修改angular.json里的启动配置,添加代理参数:

"serve": {
  "builder": "@angular-devkit/build-angular:dev-server",
  "options": {
    "browserTarget": "你的Angular项目名:build",
    "proxyConfig": "proxy.conf.json"
  }
}

之后用ng serve启动Angular,请求API时直接用/api/v1/xxx或/api/v2/xxx,会自动转发到对应的后端服务。


一次性运行所有模块的实用方案

方案1:用脚本一键启动(简单快捷)

根据你的系统写个启动脚本,一次性拉起所有服务,还能一键停止:

Linux/macOS(bash脚本 start-all.sh)

#!/bin/bash

# 启动第一个Spring Boot模块
cd ./spring-boot-module-1
mvn spring-boot:run &
PID1=$!

# 启动第二个Spring Boot模块
cd ../spring-boot-module-2
mvn spring-boot:run &
PID2=$!

# 启动Angular项目
cd ../angular-ui
ng serve &
PID3=$!

echo "✅ 所有服务已启动!按回车停止所有服务..."
read

# 停止所有进程
kill $PID1 $PID2 $PID3
echo "🛑 所有服务已停止。"

给脚本加执行权限:chmod +x start-all.sh,然后运行./start-all.sh就行。

Windows(bat脚本 start-all.bat)

@echo off

REM 启动第一个Spring Boot模块
start cmd /k "cd spring-boot-module-1 && mvn spring-boot:run"

REM 启动第二个Spring Boot模块
start cmd /k "cd spring-boot-module-2 && mvn spring-boot:run"

REM 启动Angular项目
start cmd /k "cd angular-ui && ng serve"

echo 所有服务已启动!按任意键停止所有服务...
pause >nul

REM 停止相关进程(注意:如果有其他Java/ng进程也会被终止,谨慎使用)
taskkill /f /im java.exe
taskkill /f /im ng.exe
echo 所有服务已停止。

双击脚本就能启动,按任意键停止。

方案2:用Docker Compose(推荐,环境一致性强)

如果想避免环境依赖问题,用Docker Compose是最佳选择,把每个服务打包成镜像,一键启动:

  1. 给每个Spring Boot模块写Dockerfile(以第一个为例):
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY target/spring-boot-module-1-*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

第二个Spring Boot模块的Dockerfile只需修改端口和jar文件名。

  1. Angular项目的Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# 用Nginx部署编译后的静态文件
FROM nginx:alpine
COPY --from=0 /app/dist/你的Angular项目名 /usr/share/nginx/html
# 可选:配置Nginx反向代理,避免跨域
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
  1. 编写docker-compose.yml:
version: '3.8'
services:
  spring-boot-1:
    build: ./spring-boot-module-1
    ports:
      - "8080:8080"
    networks:
      - app-network

  spring-boot-2:
    build: ./spring-boot-module-2
    ports:
      - "8081:8081"
    networks:
      - app-network

  angular-ui:
    build: ./angular-ui
    ports:
      - "4200:80"
    networks:
      - app-network
    depends_on:
      - spring-boot-1
      - spring-boot-2

networks:
  app-network:
    driver: bridge

然后运行docker-compose up --build,所有服务会自动启动,浏览器访问http://localhost:4200即可测试。停止服务用docker-compose down。

方案3:Maven聚合启动(仅Spring Boot模块)

如果两个Spring Boot是Maven子模块,可以在父pom.xml中配置,一键启动两个后端服务,再单独启动Angular:

<modules>
  <module>spring-boot-module-1</module>
  <module>spring-boot-module-2</module>
</modules>

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>3.2.0</version>
      <executions>
        <execution>
          <goals>
            <goal>run</goal>
          </goals>
          <phase>none</phase>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

在父目录运行:

# Linux/macOS
mvn spring-boot:run -pl spring-boot-module-1 & mvn spring-boot:run -pl spring-boot-module-2 & ng serve

Windows可以开三个命令行窗口,分别运行对应的启动命令。


测试步骤

  1. 验证服务启动状态:
    • 访问http://localhost:8080/actuator/health(如果Spring Boot开启了actuator),返回UP说明后端正常。
    • 第二个Spring Boot服务同理访问对应端口的健康接口。
    • 访问http://localhost:4200,确认Angular页面加载正常。
  2. 测试API交互:在Angular页面触发后端请求,检查是否能成功获取数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:57:40