docker-compose 部署java微服务项目

1、准备条件:安装docker,安装docker-compose,docker安装可自行百度,docker-compose安装由于太慢,我这里提供两个版本:win和linux版本的百度网盘版,大家可根据需要自行下载:

链接:https://pan.baidu.com/s/10W81TX6cWQqyi92xyeuZQQ
提取码:2evg

这里一linux系统为例:下载docker-compose-linux-x86_64后上传到linux 的/usr/bin 下,然后使用 mv docker-compose-linux-x86_64 docker-compose 命名后,使用chmod +x docker-compose 赋予该文件执行权限,再复制一份到/usr/local/bin下,至此,你可以使用docker-compose --version 验证docker-compose是否安装ok

 

2、jar包准备:新建一个文件夹,如docker_images,将需要的jar 以 文件夹 + jar + 该jar 的配置文件的形式,如spot-contract/spot-contract.jar 、spot-contract/application.yml,之后编写统一的Dockerfile文件:

2.1、后端服务的Dockerfile

FROM openjdk:8
WORKDIR /javaxh_docker/jdk
VOLUME /tmp
ARG JAR_FILE=./build/libs/*SNAPSHOT.jar
ADD ${JAR_FILE} app.jar ENTRYPOINT ["java", "-Dfile.encoding=UTF8", "-Duser.timezone=GMT+08", "-jar", "app.jar"]

说明:Dockerfile放在项目的根目录下,与src目录平级,使用gradle构建后,服务的目录结构如下:

 

2.2、前端服务的Dockerfile

FROM nginx:latest
COPY ./dist /usr/share/nginx/html

说明:Dockerfile放在项目的根目录下,与npm build 后的dist目录平级,使用npm构建后,服务的目录结构如下:

将该Dockerfile复制一份到 spot-contract下,形成如下目录结构

|-docker_images

|----spot-contract

|--------spot-contract.jar

|--------Dockerfile

|--------application.yml

|----spot-base

|--------spot-base.jar

|--------Dockerfile

|--------application.yml

|----docker-compose.yml

使用docker build -t spot-contract:latest spot-contract/ 构建镜像

其他服务遵循同样的方式

docker安装后修改/etc/docker/daemon.json 添加私服路径

 

3.编写docker-compose-sichuan.yml

version: '3.3'
services:
  spot-web-sichuan:
    image: 121.37.30.109:9000/huadian/spot-web-sichuan:0.0.1
    restart: always
    networks:
      - sichuan-network
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "9088:9088"
    volumes:
      - "./nginxconfs/sichuan:/etc/nginx/conf.d"
  hdpm-platform-sc:
    image: 121.37.30.109:9000/huadian/hdpm-platform-sc:0.0.1
    restart: always
    networks:
      - sichuan-network
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "9090:9090"
    volumes:
      - "./tomcat-sc/apache-tomcat-sc/webapps/:/javaxh_docker/jdk/apache-tomcat-sc/webapps/"
      - "./tomcat-sc/apache-tomcat-sc/conf/:/javaxh_docker/jdk/apache-tomcat-sc/conf"
  spot-public-data-sichuan:
    image: 121.37.30.109:9000/huadian/spot-public-data:sc
    restart: always
    networks:
      - sichuan-network
    environment:
      - TZ=Asia/Shanghai
    command: ["--spring.profiles.active=prod"]
    ports:
      - "8224:8224"
    volumes:
      - "./spot-public-data-sichuan/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-trading-result-sichuan:
    image: 121.37.30.109:9000/huadian/spot-trading-result:sc
    restart: always
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8191:8191"
    volumes:
      - "./spot-trading-result/spot-trading-result-sichuan/application.yml:/javaxh_docker/jdk/config/application-prod.yml"

networks:
  sichuan-network: { }
  fujian-network: { }
  mengxi-network: { }

 docker-compose-common.yml

version: '3.3'
services:
  eureka-server:
    image: harbor.togeek.cn/huadian/eureka:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8761:8761"
    volumes:
      - "./eureka/application-common.yml:/javaxh_docker/jdk/config/application-prod.yml"
  gateway:
    image: 121.37.30.109:9000/huadian/gateway:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8769:8769"
    volumes:
      - "./gateway/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  process-common:
    image: 121.37.30.109:9000/huadian/hdpm-process:common
    networks:
      - sichuan-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8024:8024"
    volumes:
      - "./process/mx/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  configserver:
    image: 121.37.30.109:9000/huadian/configserver:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8090:8090"
    volumes:
      - "./configserver/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-day-ahead:
    image: 121.37.30.109:9000/huadian/spot-day-ahead:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8084:8084"
    volumes:
      - "./spot-day-ahead/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-contract:
    image: 121.37.30.109:9000/huadian/spot-contract:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8755:8755"
    volumes:
      - "./spot-contract/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-base-data:
    image: 121.37.30.109:9000/huadian/spot-base-data:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8094:8094"
    volumes:
      - "./spot-base-data/application.yml:/javaxh_docker/jdk/config/application-prod.yml"

networks:
  sichuan-network: { }
  fujian-network: { }
  mengxi-network: { }

docker-compose.yml

version: '3.3'
services:
  eureka-server:
    image: harbor.togeek.cn/huadian/eureka:common
    restart: always
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8761:8761"
    volumes:
      - "./eureka/application-common.yml:/javaxh_docker/jdk/config/application-prod.yml"
  gateway:
    image: 121.37.30.109:9000/huadian/gateway:common
    restart: always
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8769:8769"
    volumes:
      - "./gateway/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  process-common:
    image: 121.37.30.109:9000/huadian/hdpm-process:common
    restart: always
    networks:
      - sichuan-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8024:8024"
    volumes:
      - "./process/mx/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  configserver:
    image: 121.37.30.109:9000/huadian/configserver:common
    restart: always
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8090:8090"
    volumes:
      - "./configserver/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-day-ahead:
    image: 121.37.30.109:9000/huadian/spot-day-ahead:common
    restart: always
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8084:8084"
    volumes:
      - "./spot-day-ahead/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-contract:
    image: 121.37.30.109:9000/huadian/spot-contract:common
    restart: always
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8755:8755"
    volumes:
      - "./spot-contract/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-base-data:
    image: 121.37.30.109:9000/huadian/spot-base-data:common
    restart: always
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8094:8094"
    volumes:
      - "./spot-base-data/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
#########################################sichuan##################################################
  spot-web-sichuan:
    image: 121.37.30.109:9000/huadian/spot-web-sichuan:0.0.1
    restart: always
    networks:
      - sichuan-network
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "9088:9088"
    volumes:
      - "./nginxconfs/sichuan:/etc/nginx/conf.d"
  hdpm-platform-sc:
    image: 121.37.30.109:9000/huadian/hdpm-platform-sc:0.0.1
    restart: always
    networks:
      - sichuan-network
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "9090:9090"
    volumes:
      - "./tomcat-sc/apache-tomcat-sc/webapps/:/javaxh_docker/jdk/apache-tomcat-sc/webapps/"
      - "./tomcat-sc/apache-tomcat-sc/conf/:/javaxh_docker/jdk/apache-tomcat-sc/conf"
  spot-public-data-sichuan:
    image: 121.37.30.109:9000/huadian/spot-public-data:sc
    restart: always
    networks:
      - sichuan-network
    environment:
      - TZ=Asia/Shanghai
    command: ["--spring.profiles.active=prod"]
    ports:
      - "8224:8224"
    volumes:
      - "./spot-public-data-sichuan/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-trading-result-sichuan:
    image: 121.37.30.109:9000/huadian/spot-trading-result:sc
    restart: always
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8191:8191"
    volumes:
      - "./spot-trading-result/spot-trading-result-sichuan/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  
#######################################################fujian###############################################################
  spot-web-fujian:
    image: 121.37.30.109:9000/huadian/spot-web-fujian:0.0.1
    restart: always
    networks:
      - fujian-network
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "9096:9096"
    volumes:
      - "./nginxconfs/fujian:/etc/nginx/conf.d"
  spot-trading-result-fujian:
    image: 121.37.30.109:9000/huadian/spot-trading-result:fj
    restart: always
    networks:
      - fujian-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8091:8091"
    volumes:
      - "./spot-trading-result/spot-trading-result-fujian/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  hdpm-process-fujian:
    image: 121.37.30.109:9000/huadian/hdpm-process:fj
    restart: always
    networks:
      - fujian-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8020:8020"
    volumes:
      - "./process/fujian//application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-public-data-fujian:
    image: 121.37.30.109:9000/huadian/spot-public-data:fj
    restart: always
    networks:
      - fujian-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8228:8228"
    volumes:
      - "./spot-public-data-fujian/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  hdpm-platform-fujian:
    image: 121.37.30.109:9000/huadian/hdpm-platform-fj:0.0.1
    restart: always
    networks:
      - fujian-network
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "9092:9092"
    volumes:
      - "./tomcat-fj/apache-tomcat-fj/webapps/:/javaxh_docker/jdk/apache-tomcat-fj/webapps/"
      - "./tomcat-fj/apache-tomcat-fj/conf/:/javaxh_docker/jdk/apache-tomcat-fj/conf"
  spot-base-data-fujian:
    image: 121.37.30.109:9000/huadian/spot-base-data:fj
    restart: always
    networks:
      - fujian-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8092:8092"
    volumes:
      - "./spot-base-data-fujian/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  ##############################################mengxi ###################################################
  spot-web-mengxi:
    image: 121.37.30.109:9000/huadian/spot-web-mengxi:0.0.1
    restart: always
    networks:
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "9093:9093"
    volumes:
      - "./nginxconfs/mengxi:/etc/nginx/conf.d"
  spot-trading-result:
    image: 121.37.30.109:9000/huadian/spot-trading-result:common
    restart: always
    networks:
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8097:8097"
    volumes:
      - "./spot-trading-result/spot-trading-result-common/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-public-data-mengxi:
    image: 121.37.30.109:9000/huadian/spot-public-data:mx
    restart: always
    networks:
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8222:8222"
    volumes:
      - "./spot-public-data-mengxi/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  hdpm-platform-mx:
    image: 121.37.30.109:9000/huadian/hdpm-platform-mx:mx
    restart: always
    networks:
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "8075:8075"
    volumes:
      - "./tomcat-mx/apache-tomcat-mx/webapps/:/javaxh_docker/jdk/apache-tomcat-mx/webapps/"
      - "./tomcat-mx/apache-tomcat-mx/conf/:/javaxh_docker/jdk/apache-tomcat-mx/conf"
  spot-emp-mengxi:
    image: 121.37.30.109:9000/huadian/spot-emp-mengxi:mx
    restart: always
    networks:
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.config.location=config/application.yaml" ]
    ports:
      - "8489:8489"
    volumes:
      - "./spot-emp/application.yaml:/javaxh_docker/jdk/config/application.yaml"
      - "./spot-emp/application-dev.yaml:/javaxh_docker/jdk/config/application-dev.yaml"
  tasks:
    image: 121.37.30.109:9000/huadian/tasks:mx
    restart: always
    networks:
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "8888:8888"
    volumes:
      - "./tasks/application.yml:/javaxh_docker/jdk/conf/application-prod.yml"

networks:
  sichuan-network: { }
  fujian-network: { }
  mengxi-network: { }

 

4、服务的启动与关闭

docker-compose -f docker-compose.yml  up -d 启动所有服务

docker-compose -f docker-compose.yml  down 关闭所有服务

docker-compose -f docker-compose.yml  up -d spot-base 只启动spot-base服务

docker-compose -f docker-compose.yml  restart  spot-base 重启spot-base服务

docker-compose -f docker-compose.yml  start spot-base 启动spot-base服务(需要先执行过up操作)

docker-compose -f docker-compose.yml  stop spot-base 停止spot-base服务

5、关于服务配置文件

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka-server:8761/eureka/
    healthcheck:
      enabled: true # 开启健康检查(需要spring-boot-starter-actuator依赖),配置项必须设置在application.yml中
  instance:
    lease-renewal-interval-in-seconds: 30    # 续约更新时间间隔(默认30秒)
    lease-expiration-duration-in-seconds: 90 # 续约到期时间(默认90秒)

由于使用的是docker容器,docker-compose可通过服务名来通讯,也可使用127.0.0.1:映射到宿主机的端口访问

6、添加restart.sh

#!/bin/bash

# 检查输入参数数量
if [ "$#" -ne 2 ]; then
  echo "请提供配置文件名,服务名作为参数"
  exit 1
fi

# 获取输入的服务名
composeFile=$1
serviceName=$2

# 停止指定的服务
docker-compose -f $composeFile down  $serviceName

# 检查服务是否成功停止
if [ $? -eq 0 ]; then
  echo "服务停止成功"
else
  echo "服务停止失败"
  exit 1
fi

# 启动指定的服务
docker-compose -f $composeFile  up -d $serviceName

# 检查服务是否成功启动
if [ $? -eq 0 ]; then
  echo "服务启动成功"
else
  echo "服务启动失败"
  exit 1
fi

 7.需要注意的点:

由于docker-compose 在设计之初 就是使用配置文件隔离服务的,也就是说,如果我们有多个配置文件,在启动后这些配置文件之间的服务默认是无法通信的,如果需要通讯,可以通过如下方式解决

方法1:

在a配置文件中指定一个全局network,然后在别的配置中使用:

a.yml

networks:
  net1:
    external: true

b.yml

services:
  your-service:
    networks:
      - net1

方法2:

docker network create --driver bridge my-network

docker network connect my-network container-name

8、docker镜像备份与还原脚本

#/bin/bash
image_arr=`docker images|grep -v 'grep\|none'|grep '9000'|awk '{print $1"#"$2}'`
#echo $image_arr
datestr=`date +"%Y-%m-%d"`
dirpre="/data/$datestr"
if [ -d $dirpre ]; then
  rm -rf $dirpre
fi
mkdir -p  $dirpre
echo "--------remkdir basedir $dirpre-------"
for var in  $image_arr;do
  old=`echo $var|cut -d '/' -f 3|awk -F# '{print $1"-"$2}'`.tar
  image=`echo $var|awk -F# '{print $1":"$2}'`
  echo $old
  echo "backing up image $image to $dirpre/$old....."
  docker save -o $dirpre/$old $image
done

  

  

 

posted @ 2023-06-29 10:36  漂渡  阅读(1436)  评论(0编辑  收藏  举报