feat(docker): 添加网关服务,实现前端-》网关-》后端调用链路
- 更新 docker-compose.yml 添加网关服务 - 更新后端 Dockerfile 支持多模块构建 - 更新网关 Dockerfile 支持多模块构建 - 前端 API 请求现在通过网关路由到后端
This commit is contained in:
+35
-2
@@ -30,6 +30,39 @@ services:
|
|||||||
- novalon-network
|
- novalon-network
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
|
# 网关服务
|
||||||
|
gateway:
|
||||||
|
build:
|
||||||
|
context: ./novalon-manage-api
|
||||||
|
dockerfile: manage-gateway/Dockerfile
|
||||||
|
args:
|
||||||
|
- BUILD_VERSION=${BUILD_VERSION:-latest}
|
||||||
|
container_name: novalon-gateway
|
||||||
|
environment:
|
||||||
|
<<: *common-env
|
||||||
|
SPRING_PROFILES_ACTIVE: docker
|
||||||
|
USER_SERVICE_URL: http://backend:8084
|
||||||
|
SPRING_CLOUD_GATEWAY_ROUTES_0_URI: http://backend:8084
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
depends_on:
|
||||||
|
backend:
|
||||||
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 40s
|
||||||
|
networks:
|
||||||
|
- novalon-network
|
||||||
|
restart: unless-stopped
|
||||||
|
logging:
|
||||||
|
driver: "json-file"
|
||||||
|
options:
|
||||||
|
max-size: "10m"
|
||||||
|
max-file: "3"
|
||||||
|
|
||||||
# 后端API服务
|
# 后端API服务
|
||||||
backend:
|
backend:
|
||||||
build:
|
build:
|
||||||
@@ -77,11 +110,11 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "3001:80"
|
- "3001:80"
|
||||||
depends_on:
|
depends_on:
|
||||||
backend:
|
gateway:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
environment:
|
environment:
|
||||||
<<: *common-env
|
<<: *common-env
|
||||||
VITE_API_BASE_URL: http://backend:8084
|
VITE_API_BASE_URL: http://gateway:8080
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "curl", "-f", "http://localhost:80"]
|
test: ["CMD", "curl", "-f", "http://localhost:80"]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
|
|||||||
@@ -1,20 +1,39 @@
|
|||||||
# 多阶段构建优化Dockerfile
|
# 多阶段构建优化Dockerfile - 多模块项目
|
||||||
FROM maven:3.9-eclipse-temurin-21 AS builder
|
FROM maven:3.9-eclipse-temurin-21 AS builder
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# 复制Maven配置文件和源码
|
# 复制根POM和Maven包装器
|
||||||
COPY pom.xml .
|
COPY pom.xml .
|
||||||
COPY mvnw .
|
COPY mvnw .
|
||||||
COPY mvnw.cmd .
|
COPY mvnw.cmd .
|
||||||
COPY .mvn .mvn
|
COPY .mvn .mvn
|
||||||
|
|
||||||
|
# 复制所有子模块POM和源码
|
||||||
|
COPY manage-common/pom.xml manage-common/
|
||||||
|
COPY manage-db/pom.xml manage-db/
|
||||||
|
COPY manage-sys/pom.xml manage-sys/
|
||||||
|
COPY manage-gateway/pom.xml manage-gateway/
|
||||||
|
COPY manage-notify/pom.xml manage-notify/
|
||||||
|
COPY manage-file/pom.xml manage-file/
|
||||||
|
COPY manage-audit/pom.xml manage-audit/
|
||||||
|
COPY manage-app/pom.xml manage-app/
|
||||||
|
|
||||||
# 下载依赖(利用Docker缓存层)
|
# 下载依赖(利用Docker缓存层)
|
||||||
RUN ./mvnw dependency:go-offline -B
|
RUN ./mvnw dependency:go-offline -B
|
||||||
|
|
||||||
# 复制源码并构建
|
# 复制源码
|
||||||
COPY src ./src
|
COPY manage-common/src manage-common/src
|
||||||
RUN ./mvnw clean package -DskipTests
|
COPY manage-db/src manage-db/src
|
||||||
|
COPY manage-sys/src manage-sys/src
|
||||||
|
COPY manage-gateway/src manage-gateway/src
|
||||||
|
COPY manage-notify/src manage-notify/src
|
||||||
|
COPY manage-file/src manage-file/src
|
||||||
|
COPY manage-audit/src manage-audit/src
|
||||||
|
COPY manage-app/src manage-app/src
|
||||||
|
|
||||||
|
# 构建manage-app模块
|
||||||
|
RUN ./mvnw clean package -DskipTests -pl manage-app -am
|
||||||
|
|
||||||
# 运行时镜像
|
# 运行时镜像
|
||||||
FROM eclipse-temurin:21-jre-jammy
|
FROM eclipse-temurin:21-jre-jammy
|
||||||
@@ -30,7 +49,7 @@ RUN groupadd -r novalon && useradd -r -g novalon novalon
|
|||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# 复制构建产物
|
# 复制构建产物
|
||||||
COPY --from=builder --chown=novalon:novalon /app/target/*.jar app.jar
|
COPY --from=builder --chown=novalon:novalon /app/manage-app/target/*.jar app.jar
|
||||||
|
|
||||||
# 设置JVM参数优化
|
# 设置JVM参数优化
|
||||||
ENV JAVA_OPTS="-Xmx512m -Xms256m -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:+UseContainerSupport -Djava.security.egd=file:/dev/./urandom"
|
ENV JAVA_OPTS="-Xmx512m -Xms256m -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:+UseContainerSupport -Djava.security.egd=file:/dev/./urandom"
|
||||||
@@ -46,4 +65,4 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
|||||||
CMD curl -f http://localhost:8084/actuator/health || exit 1
|
CMD curl -f http://localhost:8084/actuator/health || exit 1
|
||||||
|
|
||||||
# 启动命令
|
# 启动命令
|
||||||
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
|
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
|
||||||
|
|||||||
@@ -1,9 +1,68 @@
|
|||||||
FROM openjdk:21-jdk-slim
|
# 多阶段构建优化Dockerfile - 多模块项目(网关)
|
||||||
|
FROM maven:3.9-eclipse-temurin-21 AS builder
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY manage-gateway/target/manage-gateway-1.0.0.jar app.jar
|
# 复制根POM和Maven包装器
|
||||||
|
COPY pom.xml .
|
||||||
|
COPY mvnw .
|
||||||
|
COPY mvnw.cmd .
|
||||||
|
COPY .mvn .mvn
|
||||||
|
|
||||||
|
# 复制所有子模块POM
|
||||||
|
COPY manage-common/pom.xml manage-common/
|
||||||
|
COPY manage-db/pom.xml manage-db/
|
||||||
|
COPY manage-sys/pom.xml manage-sys/
|
||||||
|
COPY manage-gateway/pom.xml manage-gateway/
|
||||||
|
COPY manage-notify/pom.xml manage-notify/
|
||||||
|
COPY manage-file/pom.xml manage-file/
|
||||||
|
COPY manage-audit/pom.xml manage-audit/
|
||||||
|
COPY manage-app/pom.xml manage-app/
|
||||||
|
|
||||||
|
# 下载依赖(利用Docker缓存层)
|
||||||
|
RUN ./mvnw dependency:go-offline -B
|
||||||
|
|
||||||
|
# 复制源码
|
||||||
|
COPY manage-common/src manage-common/src
|
||||||
|
COPY manage-db/src manage-db/src
|
||||||
|
COPY manage-sys/src manage-sys/src
|
||||||
|
COPY manage-gateway/src manage-gateway/src
|
||||||
|
COPY manage-notify/src manage-notify/src
|
||||||
|
COPY manage-file/src manage-file/src
|
||||||
|
COPY manage-audit/src manage-audit/src
|
||||||
|
COPY manage-app/src manage-app/src
|
||||||
|
|
||||||
|
# 构建manage-gateway模块
|
||||||
|
RUN ./mvnw clean package -DskipTests -pl manage-gateway -am
|
||||||
|
|
||||||
|
# 运行时镜像
|
||||||
|
FROM eclipse-temurin:21-jre-jammy
|
||||||
|
|
||||||
|
# 设置时区和语言环境
|
||||||
|
RUN apt-get update && apt-get install -y \
|
||||||
|
curl \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# 创建非root用户运行应用
|
||||||
|
RUN groupadd -r novalon && useradd -r -g novalon novalon
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 复制构建产物
|
||||||
|
COPY --from=builder --chown=novalon:novalon /app/manage-gateway/target/*.jar app.jar
|
||||||
|
|
||||||
|
# 设置JVM参数优化
|
||||||
|
ENV JAVA_OPTS="-Xmx512m -Xms256m -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:+UseContainerSupport -Djava.security.egd=file:/dev/./urandom"
|
||||||
|
|
||||||
|
# 暴露端口
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
ENTRYPOINT ["java", "-jar", "app.jar"]
|
# 切换用户
|
||||||
|
USER novalon
|
||||||
|
|
||||||
|
# 健康检查
|
||||||
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
||||||
|
CMD curl -f http://localhost:8080/actuator/health || exit 1
|
||||||
|
|
||||||
|
# 启动命令
|
||||||
|
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
|
||||||
|
|||||||
Reference in New Issue
Block a user