springcloud部署——docker和docker-compose

最近一直在研究微服务的东西,发现微服务项目的部署运维比较麻烦,经过几天终于完整的把一套微服务部署了起来,这期间真是遇到太多问题,虽然最终觉得都不是什么大问题,但是这个过程总是艰难的,还好硬着头皮坚持了下来。本篇文章的项目是用了一个开源项目,对其进行了改造,是一个很不错的微服务学习开源项目,推荐一下:地址为https://gitee.com/wells2333/spring-microservice-exam/tree/master

当然本篇文章主要是总结下如何部署微服务的,传统的方式就是打jar或者war的方式运行项目,但是微服务的分支比较多,如何还按照原来的方式进行部署,那就太麻烦了,所以用docker部署的方式。

项目主要是用consul作为的注册中心,docker部署用的是集群版的,当然可以使用远程地址,但是在部署的时候一定要记得修改地址,地址的所有配置都是宿主机的地址,如下:

springcloud部署——docker和docker-compose_第1张图片

compose进行项目部署的编排

详细的说明见博主的地址:http://ehedgehog.net/2019/04/22/%E5%9C%A8%E7%BA%BF%E8%80%83%E8%AF%95%E7%B3%BB%E7%BB%9FV2.0%E9%95%9C%E5%83%8F%E6%9E%84%E5%BB%BA%E3%80%81%E6%8E%A8%E9%80%81%E3%80%81%E9%83%A8%E7%BD%B2/

编排文件如下:

命令执行文件:

docker-compose.env(一些基础的配置信息)

# JVM参数
# 可通过docker-compose -f docker-compose-services.yml config命令查看是否生效
JAVA_OPTS=-Xmx128m -Xms128m

# 租户标识,默认gitee
TENANT_CODE=gitee

# 网关token转换
GATEWAY_TOKEN_TRANSFER=false

# 环境配置
SPRING_PROFILES_ACTIVE=native

# consul配置
CONSUL_HOST=172.28.162.113
CONSUL_PORT=8500

# rabbitMq配置
RABBIT_HOST=172.28.162.113
RABBIT_PORT=5672
RABBITMQ_DEFAULT_USER=guest
RABBITMQ_DEFAULT_PASS=guest

# Redis配置
REDIS_HOST=172.28.162.113
REDIS_PORT=6379

# 数据库配置
MYSQL_HOST=172.28.158.98
MYSQL_PORT=3306
MYSQL_USERNAME=root
MYSQL_PASSWORD=123

# FDFS配置
#FDFS_HOST=fdfs
#FDFS_PORT=22122

# 配置中心的账号密码
CONFIG_SERVER_USERNAME=admin
CONFIG_SERVER_PASSWORD=11

# 附件服务器配置
#ATTACHMENT_HOST=http://attachment-service:8080
#ATTACHMENT_PORT=80

# 各服务host配置
CONFIG_SERVICE_HOST=172.28.162.113
AUTH_SERVICE_HOST=172.28.162.113
EXAM_SERVICE_HOST=172.28.162.113
GATEWAY_SERVICE_HOST=172.28.162.113
MONITOR_SERVICE_HOST=172.28.162.113
USER_SERVICE_HOST=172.28.162.113

#CONFIG_SERVICE_HOST=config-service
#AUTH_SERVICE_HOST=auth-service
#EXAM_SERVICE_HOST=exam-service
#GATEWAY_SERVICE_HOST=gateway-service
#MONITOR_SERVICE_HOST=monitor-service
#USER_SERVICE_HOST=user-service

# 服务监控配置172.28.162.113
#ADMIN_HOST=monitor-service
ADMIN_HOST=172.28.162.113
ADMIN_PORT=8085
ADMIN_USERNAME=admin
ADMIN_PASSWORD=11

# zipkin配置
ZIPKIN_HOST=172.28.162.113
ZIPKIN_PORT=9411

# 演示环境
PREVIEW_ENABLED=false

# ID生成配置
CLUSTER_WORK_ID=1
CLUSTER_DATA_CENTER_ID=1

# 时区设置,否则容器里的时间和主机时间差8小时
TZ=Asia/Shanghai

# elk配置
#ELK_DESTINATION=localhost:5044

docker-compose-base.yml(基础服务的编排,主要是config)

version: '3'
services:
  # ---------------------------
  # consul集群,3个节点
  # ---------------------------
  consul:
    image: consul:1.2.0
    container_name: consul
    command: consul agent -server -client 0.0.0.0 -ui -bootstrap-expect=3 -data-dir=/consul/data -retry-join=consul2 -retry-join=consul3 -datacenter=blr
    restart: always
    ports:
      - "8300:8300"
      - "8400:8400"
      - "8500:8500"
      - "8600:8600/udp"
    networks:
      - net
  consul2:
    image: consul:1.2.0
    container_name: consul-2
    expose:
      - "8500"
      - "8600"
    command: consul agent -server -data-dir=/consul/data -retry-join=consul -retry-join=consul3 -datacenter=blr
    links:
      - consul
    networks:
      - net
  consul3:
    image: consul:1.2.0
    container_name: consul-3
    expose:
      - "8500"
      - "8600"
    command: consul agent -server -data-dir=/consul/data -retry-join=consul -retry-join=consul2 -datacenter=blr
    links:
      - consul
      - consul2
    networks:
      - net

  # ---------------------------
  # rabbitMq
  # ---------------------------
  rabbit:
    image: rabbitmq:3.6.6-management-alpine
    container_name: rabbit-mq
    restart: always
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest
    networks:
      - net

  # ---------------------------
  # Redis
  # ---------------------------
  redis:
    image: redis:latest
    container_name: redis
    ports:
      - "6379:6379"
    networks:
      - net

  # ---------------------------
  # 配置中心
  # ---------------------------
  config-service:
    image: registry.cn-shenzhen.aliyuncs.com/lipf/config-service:3.2.0-SNAPSHOT
    container_name: config-service
    env_file: docker-compose.env  # 从文件中获取配置
    restart: always
    depends_on:
      - consul
      - rabbit
      - redis
    ports:
      - "8769:8769"
    networks:
      - net

networks:
  net:
    driver: bridge

docker-compose-services.yml(具体服务的编排)

version: '3'
services:

  # ---------------------------
  # 授权服务
  # ---------------------------
  auth-service:
    image: registry.cn-shenzhen.aliyuncs.com/lipf/auth-service:3.2.0-SNAPSHOT
    container_name: auth-service
    env_file: docker-compose.env  # 从文件中获取配置
    restart: always
    ports:
      - "8090:8090"
    networks:
      - net

  # ---------------------------
  # 用户服务
  # ---------------------------
  user-service:
    image: registry.cn-shenzhen.aliyuncs.com/lipf/user-service:3.2.0-SNAPSHOT
    container_name: user-service
    env_file: docker-compose.env  # 从文件中获取配置
    restart: always
    ports:
      - "8095:8095"
    networks:
      - net

  # ---------------------------
  # 考试服务
  # ---------------------------
  exam-service:
    image: registry.cn-shenzhen.aliyuncs.com/lipf/exam-service:3.2.0-SNAPSHOT
    container_name: exam-service
    env_file: docker-compose.env  # 从文件中获取配置
    restart: always
    ports:
      - "8098:8098"
    networks:
      - net

  # ---------------------------
  # 消息服务
  # ---------------------------
#  msc-service:
#    image: registry.cn-shenzhen.aliyuncs.com/lipf/msc-service:3.2.0-SNAPSHOT
#    container_name: msc-service
#    env_file: docker-compose.env  # 从文件中获取配置
#    restart: always
#    ports:
#      - "9000:9000"
#    networks:
#      - net

  # ---------------------------
  # 监控服务
  # ---------------------------
#  monitor-service:
#    image: registry.cn-shenzhen.aliyuncs.com/lipf/monitor-service:3.2.0-SNAPSHOT
#    container_name: monitor-service
#    env_file: docker-compose.env  # 从文件中获取配置
#    restart: always
#    ports:
#      - "8085:8085"
#    networks:
#      - net
  # ---------------------------
  # api网关
  # ---------------------------
  gateway-service:
    image: registry.cn-shenzhen.aliyuncs.com/lipf/gateway-service:3.2.0-SNAPSHOT
    container_name: gateway-service
    env_file: docker-compose.env  # 从文件中获取配置
    restart: always
    ports:
      - "8000:8000"
    networks:
      - net

networks:
  net:
    driver: bridge

start.sh(启动的脚本)

#!/bin/sh

# 配置文件根目录,固定是spring-microservice-exam
DOCKERHOME=/root/spring-microservice-exam

# 镜像名称前缀、标签
BASE_IMAGE_NAME=registry.cn-shenzhen.aliyuncs.com/lipf
BSEE_IMAGE_TAG=3.2.0-SNAPSHOT

# 各服务的镜像名称
CONFIG_SERVICE=$BASE_IMAGE_NAME/config-service:$BSEE_IMAGE_TAG
AUTH_SERVICE=$BASE_IMAGE_NAME/auth-service:$BSEE_IMAGE_TAG
USER_SERVICE=$BASE_IMAGE_NAME/user-service:$BSEE_IMAGE_TAG
EXAM_SERVICE=$BASE_IMAGE_NAME/exam-service:$BSEE_IMAGE_TAG
GATEWAY_SERVICE=$BASE_IMAGE_NAME/gateway-service:$BSEE_IMAGE_TAG
MSC_SERVICE=$BASE_IMAGE_NAME/msc-service:$BSEE_IMAGE_TAG
MONITOR_SERVICE=$BASE_IMAGE_NAME/monitor-service:$BSEE_IMAGE_TAG
#UI_SERVICE=$BASE_IMAGE_NAME/spring-microservice-exam-ui:$BSEE_IMAGE_TAG
#WEB_SERVICE=$BASE_IMAGE_NAME/spring-microservice-exam-web:$BSEE_IMAGE_TAG

case "$1" in

    # 删除容器
    removeAll)
        echo "* 正在删除容器..."
        time docker rm $(docker ps -aq) -f
        echo "* 删除容器成功..."
        ;;
    # 拉取镜像
    pull)
        echo "* 正在拉取后端镜像..."
        time docker pull $CONFIG_SERVICE
        time docker pull $AUTH_SERVICE
        time docker pull $USER_SERVICE
        time docker pull $EXAM_SERVICE
        time docker pull $GATEWAY_SERVICE
        time docker pull $MSC_SERVICE
        time docker pull $MONITOR_SERVICE
    #    echo "* 开始拉取前端镜像..."
    #    time docker pull $UI_SERVICE
    #    time docker pull $WEB_SERVICE
        echo "* 拉取镜像成功..."
        ;;
    # 运行镜像
    run)
        echo "* 开始运行基础镜像..."
        time docker-compose -f $DOCKERHOME/docker-compose-base.yml up -d
        echo "* 等待100s..."
        sleep 100
        echo "* 开始运行后端服务镜像..."
        time docker-compose -f $DOCKERHOME/docker-compose-services.yml up -d
        echo "* 等待10s..."
        sleep 10
        #echo "* 开始运行前端服务镜像..."
        #time docker-compose -f $DOCKERHOME/docker-compose-nginx.yml up -d
        echo "* 运行成功..."
        ;;
    # 拉取镜像并运行
    pullrun)
        echo "* 正在拉取后端镜像..."
        time docker pull $CONFIG_SERVICE
        time docker pull $AUTH_SERVICE
        time docker pull $USER_SERVICE
        time docker pull $EXAM_SERVICE
        time docker pull $GATEWAY_SERVICE
        time docker pull $MSC_SERVICE
        time docker pull $MONITOR_SERVICE
        #echo "* 开始拉取前端镜像..."
        #time docker pull $UI_SERVICE
        #time docker pull $WEB_SERVICE
        echo "* 拉取镜像成功..."

         echo "* 开始运行基础镜像..."
        time docker-compose -f $DOCKERHOME/docker-compose-base.yml up -d
        echo "* 等待100s..."
        sleep 100
        echo "* 开始运行后端服务镜像..."
        time docker-compose -f $DOCKERHOME/docker-compose-services.yml up -d
        echo "* 等待10s..."
        sleep 10
   #     echo "* 开始运行前端服务镜像..."
   #     time docker-compose -f $DOCKERHOME/docker-compose-nginx.yml up -d
        echo "* 运行成功..."
        ;;
    # 停止容器
    stop)
        echo "* 正在停止容器..."
   #     time docker-compose -f $DOCKERHOME/docker-compose-nginx.yml stop
        time docker-compose -f $DOCKERHOME/docker-compose-services.yml stop
        time docker-compose -f $DOCKERHOME/docker-compose-base.yml stop
        echo "* 停止容器成功..."
        ;;
    # 重启容器
    restart)
        echo "* 正在停止镜像..."
   #     time docker-compose -f $DOCKERHOME/docker-compose-nginx.yml restart
        time docker-compose -f $DOCKERHOME/docker-compose-services.yml restart
        time docker-compose -f $DOCKERHOME/docker-compose-base.yml restart
        ;;
    # 其它
    *)
        echo "* ..."
        ;;
esac
exit 0

 

部署过程中用到的一些命令:

 

【1】推送镜像

通过maven上传镜像到阿里云
在项目的根目录下执行
mvn防止内存溢出
set MAVEN_OPTS= -Xms128m -Xmx512m
mvn clean package

【2】容器的一些简单命令

docker ps // 查看所有正在运行容器
docker stop containerId // containerId 是容器的ID
docker ps -a // 查看所有容器
docker ps -a -q // 查看所有容器ID
docker stop $(docker ps -a -q) //  stop停止所有容器
docker rm  $(docker ps -a -q) //   remove删除所有容器

springcloud部署——docker和docker-compose_第2张图片

【3】查看容器日志

docker logs -f -t --tail -f consul
docker logs -f -t --tail -f config-service
docker logs -f -t --tail -f auth-service
docker logs -f -t --tail -f user-service
docker logs -f -t --tail -f exam-service
docker logs -f -t --tail -f gateway-service

【4】脚本命令

停止容器:./start.sh stop
删除所有容器:./start.sh removeAll
重启所有容器:./start.sh restart
拉取并启动:./start.sh pullrun

命令启动结果(在拉取镜像运行容器之前要先上传镜像哦):

springcloud部署——docker和docker-compose_第3张图片

启动后的结果:

springcloud部署——docker和docker-compose_第4张图片

【5】登录阿里云命令

docker login --username=username registry.cn-shenzhen.aliyuncs.com

【6】安装compose

遇到的问题:

【1】该问题就是因为由于地址没有配置成功导致的网络不通,报了如下两个错误

springcloud部署——docker和docker-compose_第5张图片

如果consul是容器部署的,就会显示上述容器,如果是远程地址就会显示地址,建议生产环境用docker集成consul,开发的时候可以再本地使用远程地址,配置远程地址的时候子项目中指定(如果是docker下的consul就不需要指定也可以)如图下:

springcloud部署——docker和docker-compose_第6张图片项目中配置的时候只需要配置集群中的一个地址就行。

【2】在consul的节点监控中有的显示超时net/http:request cancel (Client.Timeout exceeded while awaiting headers)

springcloud部署——docker和docker-compose_第7张图片

这种错误的本质就是网络不通导致的,说白了还是因为服务不通导致的,要检查服务的consul地址以及服务容器是否正常启动

 

 

 

你可能感兴趣的:(springcloud)