Skip to content

温馨提示

通过 Docker Compose 编排项目的 JAR 包,实现一键部署。使用 Dockerfile 构建应用的镜像,并通过文件定义服务依赖和网络配置。

准备工作

确保以下工具已安装:

打包项目

IntelliJ IDEA 工具中 配置Maven插件,使用 cleanpackage 命令编译和打包 Java 项目为 JAR 文件。

温馨提示

也可以执行命令 mvn clean package 编译打包项目,生成工程应用 JAR 包文件。

编译打包后会在每个服务工程的 target 目录下生成一个 JAR 文件。

包名称端口号包描述说明
lanjing-nacos8848Nacos注册中心
lanjing-seata7091Seata分布式事务
lanjing-monitor9000健康监控
lanjing-gateway9010网关服务
lanjing-auth9020认证服务
lanjing-system-biz9040系统服务
lanjing-user-biz9042用户服务预留扩展模块
lanjing-cms-biz9043CMS服务
lanjing-order-biz9045订单服务预留扩展模块
lanjing-product-biz9046产品服务预留扩展模块
lanjing-job9044定时任务调度服务
lanjing-generator9041代码工具服务
lanjing-admin-biz9030后台管理服务
lanjing-web-biz9031网站服务
lanjing-uniapp-biz9032客户端服务预留扩展模块

创建构建文件

以下是整个应用基于 Docker 容器化部署的配置文件、JAR包等结构;

js
lanjing-nacos-antdvue
├─ lanjing-admin-biz
│  ├─ Dockerfile
│  ├─ lanjing-admin-biz.jar
├─ lanjing-auth
│  ├─ Dockerfile
│  ├─ lanjing-auth.jar
├─ lanjing-cms-biz
│  ├─ Dockerfile
│  ├─ lanjing-cms-biz.jar
├─ lanjing-gateway
│  ├─ Dockerfile
│  ├─ lanjing-gateway.jar
├─ lanjing-generator
│  ├─ Dockerfile
lanjing-generator.jar
├─ lanjing-job
│  ├─ Dockerfile
│  ├─ lanjing-job.jar
├─ lanjing-monitor
│  ├─ Dockerfile
│  ├─ lanjing-monitor.jar
├─ lanjing-nacos
│  ├─ Dockerfile
│  ├─ lanjing-nacoss.jar
├─ lanjing-order-biz
│  ├─ Dockerfile
│  ├─ lanjing-order-biz.jar
├─ lanjing-product-biz
│  ├─ Dockerfile
│  ├─ lanjing-product-biz.jar
├─ lanjing-seata
│  ├─ Dockerfile
│  ├─ lanjing-seatas.jar
├─ lanjing-system-biz
│  ├─ Dockerfile
│  ├─ lanjing-system-biz.jar
├─ lanjing-uniapp-biz
│  ├─ Dockerfile
│  ├─ lanjing-uniapp-biz.jar
├─ lanjing-user-biz
│  ├─ Dockerfile
│  ├─ lanjing-user-biz.jar
├─ lanjing-web-biz
│  ├─ Dockerfile
│  ├─ lanjing-web-biz.jar
├─ docker-compose.yml

以下是 Dockerfile 文件案例,用于构建应用的 Docker 镜像:

js
# 基础镜像
FROM openjdk:17-jre-alpine
# 维护作者
MAINTAINER 单体架构
# 设置默认时区
ENV TZ=Asia/Shanghai
# 创建目录
RUN mkdir -p /opt/apps
# 指定工作区
WORKDIR /opt/apps
# 添加应用JAR包到容器对应目录下
ADD lanjing-admin-biz.jar /opt/apps
# 暴露容器端口号
EXPOSE 8081
# 容器启动之后执行的命令,参数使用逗号隔开
ENTRYPOINT ["java","-jar","lanjing-admin-biz.jar"]

温馨提示

上述是 lanjing-admin-biz 服务的 Dockerfile 配置文件,其他模块服务基本类似,修改对应的 JAR 包名;

创建部署文件

在项目根目录下创建 docker-compose.yml 文件,定义应用及其依赖服务:

js
version: '3.7'

# 应用服务
services:
  # Nacos服务
  lanjing-nacos:
    # 镜像名称
    image: lanjing-nacos
    # 容器名称
    container_name: lanjing-nacos
    # 构建镜像
    build:
      context: ./lanjing-nacos
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 8848:8848
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # Seata服务
  lanjing-seata:
    # 镜像名称
    image: lanjing-seata
    # 容器名称
    container_name: lanjing-seata
    # 构建镜像
    build:
      context: ./lanjing-seata
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 7091:7091
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # Monitor服务
  lanjing-monitor:
    # 镜像名称
    image: lanjing-monitor
    # 容器名称
    container_name: lanjing-monitor
    # 构建镜像
    build:
      context: ./lanjing-monitor
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9000:9000
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 网关服务
  lanjing-gateway:
    # 镜像名称
    image: lanjing-gateway
    # 容器名称
    container_name: lanjing-gateway
    # 构建镜像
    build:
      context: ./lanjing-gateway
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9010:9010
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 认证服务
  lanjing-auth:
    # 镜像名称
    image: lanjing-auth
    # 容器名称
    container_name: lanjing-auth
    # 构建镜像
    build:
      context: ./lanjing-auth
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9020:9020
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 系统服务
  lanjing-system-biz:
    # 镜像名称
    image: lanjing-system-biz
    # 容器名称
    container_name: lanjing-system-biz
    # 构建镜像
    build:
      context: ./lanjing-system-biz
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9040:9040
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 用户服务
  lanjing-user-biz:
    # 镜像名称
    image: lanjing-user-biz
    # 容器名称
    container_name: lanjing-user-biz
    # 构建镜像
    build:
      context: ./lanjing-user-biz
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9042:9042
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # CMS服务
  lanjing-cms-biz:
    # 镜像名称
    image: lanjing-cms-biz
    # 容器名称
    container_name: lanjing-cms-biz
    # 构建镜像
    build:
      context: ./lanjing-cms-biz
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9043:9043
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 订单服务
  lanjing-order-biz:
    # 镜像名称
    image: lanjing-order-biz
    # 容器名称
    container_name: lanjing-order-biz
    # 构建镜像
    build:
      context: ./lanjing-order-biz
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9045:9045
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 产品服务
  lanjing-product-biz:
    # 镜像名称
    image: lanjing-product-biz
    # 容器名称
    container_name: lanjing-product-biz
    # 构建镜像
    build:
      context: ./lanjing-product-biz
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9046:9046
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 定时任务服务
  lanjing-job:
    # 镜像名称
    image: lanjing-job
    # 容器名称
    container_name: lanjing-job
    # 构建镜像
    build:
      context: ./lanjing-job
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9044:9044
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 代码工具服务
  lanjing-generator:
    # 镜像名称
    image: lanjing-generator
    # 容器名称
    container_name: lanjing-generator
    # 构建镜像
    build:
      context: ./lanjing-generator
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9041:9041
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 后台服务
  lanjing-admin-biz:
    # 镜像名称
    image: lanjing-admin-biz
    # 容器名称
    container_name: lanjing-admin-biz
    # 构建镜像
    build:
      context: ./lanjing-admin-biz
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9030:9030
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 网站服务
  lanjing-web-biz:
    # 镜像名称
    image: lanjing-web-biz
    # 容器名称
    container_name: lanjing-web-biz
    # 构建镜像
    build:
      context: ./lanjing-web-biz
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9031:9031
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"
  # 客户端服务
  lanjing-uniapp-biz:
    # 镜像名称
    image: lanjing-uniapp-biz
    # 容器名称
    container_name: lanjing-uniapp-biz
    # 构建镜像
    build:
      context: ./lanjing-uniapp-biz
      dockerfile: Dockerfile
    # 将容器运行在特权模式下,意味着容器内的进程将具
    # 有访问宿主机的权限,包括文件系统、设备和系统功能等
    privileged: true
    # 指定容器中运行的用户
    user: root
    # 启动策略为始终重启
    restart: always
    # 设置网络模式为host模式
    network_mode: host
    # 端口映射
    ports:
      - 9032:9032
    # 文件挂载
    volumes:
      - $PWD/logs:/opt/apps/logs
      - $PWD/upload:/opt/apps/upload
    # 日志配置
    logging:
      driver: "json-file"
      options:
        max-size: "500m"
        max-file: "10"

构建并启动服务

在项目根目录下运行以下命令,使用 Docker Compose 构建镜像并启动服务:

js
docker-compose up -d

如果需要停止服务,可以运行以下命令:

js
docker-compose down

温馨提示

应用部署成功后,即可通过服务有效的地址验证服务是否正确部署并对外提供接口服务。

总结

通过 Docker Compose,你可以轻松部署项目及其依赖服务。使用 Dockerfile 构建应用的镜像,并通过 docker-compose.yml 文件定义服务编排,实现一键部署和管理。这种方法非常适合开发、测试和生产环境。

蓝鲸云团队 · 提供技术支持