docker-compose 完整打包发布, 多服务,多节点SPRING CLOUD ,EUREKA 集群

Posted 高因咖啡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker-compose 完整打包发布, 多服务,多节点SPRING CLOUD ,EUREKA 集群相关的知识,希望对你有一定的参考价值。

这里不再使用 端口映射的方式,因为不同主机上,Feign 根据 docker hostname访问会有问题.

把打包的好jar copy到docker镜像里

有几个服务,就复制几个dockerfile

Dockerfile

FROM registry.cn-hangzhou.aliyuncs.com/laizhenwei/jre:8u144

#
MAINTAINER zhenweilai <181282945@qq.com>

COPY jar /usr/local

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \\ 
    echo \'Asia/Shanghai\' >/etc/timezone

开启两个eureka,互相注册

开启两个api服务,

开启两个app服务.

开启一个zuul路由服务

docker-compose

version: \'2\'
services:
  eureka_master:
    build: eurekaserver
    network_mode: "host"
    command: java -jar /usr/local/eurekaserver.jar --spring.profiles.active=test --eureka.instance.hostname=eureka1 --eureka.instance.other-hostname=eureka2 --server.port=8000 --eureka.instance.other-port=8001
    volumes:
     - /usr/local/microservice/log:/usr/local/microservice/log
    restart: always

  eureka_backup:
    build: eurekaserver
    network_mode: "host"
    command: java -jar /usr/local/eurekaserver.jar --spring.profiles.active=test --eureka.instance.hostname=eureka2 --eureka.instance.other-hostname=eureka1 --server.port=8001 --eureka.instance.other-port=8000
    volumes:
     - /usr/local/microservice/log:/usr/local/microservice/log
    restart: always

  api_8070:
    build: api
    network_mode: "host"
    command: java -jar /usr/local/api.jar --spring.profiles.active=test --server.port=8070
    depends_on:
     - eureka_master
     - eureka_backup
    volumes:
     - /usr/local/microservice/log:/usr/local/microservice/log
    restart: always

  api_8071:
    build: api
    network_mode: "host"
    command: java -jar /usr/local/api.jar --spring.profiles.active=test --server.port=8071
    depends_on:
     - eureka_master
     - eureka_backup
    volumes:
     - /usr/local/microservice/log:/usr/local/microservice/log
    restart: always

  router:
    build: router
    network_mode: "host"
    command: java -jar /usr/local/router.jar --spring.profiles.active=test --server.port=8088
    depends_on:
     - eureka_master
     - eureka_backup
    volumes:
     - /usr/local/microservice/log:/usr/local/microservice/log
    restart: always


  app_8060:
    build: app
    network_mode: "host"
    command: java -jar /usr/local/app.jar --spring.profiles.active=test --server.port=8060
    depends_on:
     - eureka_master
     - eureka_backup
    volumes:
     - /usr/local/microservice/log:/usr/local/microservice/log
    restart: always

  app_8080:
    build: app
    network_mode: "host"
    command: java -jar /usr/local/app.jar --spring.profiles.active=test --server.port=8080
    depends_on:
     - eureka_master
     - eureka_backup
    volumes:
     - /usr/local/microservice/log:/usr/local/microservice/log
    restart: always

应用application.yml,注册到两个eureka

eureka:
  instance:
    appname: app
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    #续约更新时间间隔
    lease-renewal-interval-in-seconds: 10
    #续约到期时间
    lease-expiration-duration-in-seconds: 30
  client:
    serviceUrl:
      defaultZone: http://username:password@eureka1:8000/eureka/,http://username:password@eureka2:8001/eureka/

eureka application.yml

eureka:
  instance:
     hostname: localhost
     other-port: 8001
     other-hostname: backup
     appname: eureka-server
     instance-id: ${spring.cloud.client.ipAddress}:${server.port}
     lease-expiration-duration-in-seconds: 5
  server:
    #5秒清理一次
    eviction-interval-timer-in-ms: 5
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://username:password@${eureka.instance.other-hostname}:${eureka.instance.other-port}/eureka/

输入命令:docker-compose up --build -d

Step 1/4 : FROM registry.cn-hangzhou.aliyuncs.com/laizhenwei/jre:8u144
 ---> 1b97d252ae81
Step 2/4 : MAINTAINER zhenweilai <181282945@qq.com>
 ---> Using cache
 ---> bf31044329be
Step 3/4 : COPY jar /usr/local
 ---> 7a1be0756dca
Removing intermediate container 5c45807bbc81
Step 4/4 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&     echo \'Asia/Shanghai\' >/etc/timezone
 ---> Running in e8988ea8a1f8
 ---> ad26f883175e
Removing intermediate container e8988ea8a1f8
Successfully built ad26f883175e
Successfully tagged microservice_eureka_backup:latest
Building eureka_master
Step 1/4 : FROM registry.cn-hangzhou.aliyuncs.com/laizhenwei/jre:8u144
 ---> 1b97d252ae81
Step 2/4 : MAINTAINER zhenweilai <181282945@qq.com>
 ---> Using cache
 ---> bf31044329be
Step 3/4 : COPY jar /usr/local
 ---> Using cache
 ---> 7a1be0756dca
Step 4/4 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&     echo \'Asia/Shanghai\' >/etc/timezone
 ---> Using cache
 ---> ad26f883175e
Successfully built ad26f883175e
Successfully tagged microservice_eureka_master:latest
Building cloudinvoice_8080
Step 1/4 : FROM registry.cn-hangzhou.aliyuncs.com/laizhenwei/jre:8u144
 ---> 1b97d252ae81
Step 2/4 : MAINTAINER zhenweilai <181282945@qq.com>
 ---> Using cache
 ---> bf31044329be
Step 3/4 : COPY jar /usr/local
 ---> Using cache
 ---> b9fe90218b85
Step 4/4 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&     echo \'Asia/Shanghai\' >/etc/timezone
 ---> Using cache
 ---> 709a728e383a
Successfully built 709a728e383a
Successfully tagged microservice_cloudinvoice_8080:latest
Building cloudinvoice_8060
Step 1/4 : FROM registry.cn-hangzhou.aliyuncs.com/laizhenwei/jre:8u144
 ---> 1b97d252ae81
Step 2/4 : MAINTAINER zhenweilai <181282945@qq.com>
 ---> Using cache
 ---> bf31044329be
Step 3/4 : COPY jar /usr/local
 ---> Using cache
 ---> b9fe90218b85
Step 4/4 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&     echo \'Asia/Shanghai\' >/etc/timezone
 ---> Using cache
 ---> 709a728e383a
Successfully built 709a728e383a
Successfully tagged microservice_cloudinvoice_8060:latest
Building api_8070
Step 1/4 : FROM registry.cn-hangzhou.aliyuncs.com/laizhenwei/jre:8u144
 ---> 1b97d252ae81
Step 2/4 : MAINTAINER zhenweilai <181282945@qq.com>
 ---> Using cache
 ---> bf31044329be
Step 3/4 : COPY jar /usr/local
 ---> Using cache
 ---> 5b85563136e5
Step 4/4 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&     echo \'Asia/Shanghai\' >/etc/timezone
 ---> Using cache
 ---> 679564f7d414
Successfully built 679564f7d414
Successfully tagged microservice_api_8070:latest
Building api_8071
Step 1/4 : FROM registry.cn-hangzhou.aliyuncs.com/laizhenwei/jre:8u144
 ---> 1b97d252ae81
Step 2/4 : MAINTAINER zhenweilai <181282945@qq.com>
 ---> Using cache
 ---> bf31044329be
Step 3/4 : COPY jar /usr/local
 ---> Using cache
 ---> 5b85563136e5
Step 4/4 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&     echo \'Asia/Shanghai\' >/etc/timezone
 ---> Using cache
 ---> 679564f7d414
Successfully built 679564f7d414
Successfully tagged microservice_api_8071:latest
Building router
Step 1/4 : FROM registry.cn-hangzhou.aliyuncs.com/laizhenwei/jre:8u144
 ---> 1b97d252ae81
Step 2/4 : MAINTAINER zhenweilai <181282945@qq.com>
 ---> Using cache
 ---> bf31044329be
Step 3/4 : COPY jar /usr/local
 ---> Using cache
 ---> c88e35c7fc38
Step 4/4 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&     echo \'Asia/Shanghai\' >/etc/timezone
 ---> Using cache
 ---> 288ce24cfef0
Successfully built 288ce24cfef0
Successfully tagged microservice_router:latest
Recreating microservice_eureka_master_1 ... 
Recreating microservice_eureka_backup_1 ... 
Recreating microservice_eureka_master_1
Recreating microservice_eureka_backup_1 ... done
Recreating microservice_app_8060_1 ... 
Recreating microservice_api_8070_1 ... 
Recreating microservice_api_8070_1
Recreating microservice_router_1 ... 
Recreating microservice_api_8071_1 ... 
Recreating microservice_cloudinvoice_8060_1
Recreating microservice_api_8071_1
Recreating microservice_app_8080_1 ... 
Recreating microservice_router_1
Recreating microservice_app_8080_1 ... done

docker-compose ps

              Name                            Command               State   Ports 
---------------------------------------------------------------------------------
microservice_api_8070_1            java -jar /usr/local/api ...   Up            
microservice_api_8071_1            java -jar /usr/local/api ...   Up            
microservice_app_8060_1   java -jar /usr/local/app ...   Up            
microservice_app_8080_1   java -jar /usr/local/app...   Up            
microservice_eureka_backup_1       java -jar /usr/local/eurek ...   Up            
microservice_eureka_master_1       java -jar /usr/local/eurek ...   Up            
microservice_router_1              java -jar /usr/local/route ...   Up  

 

 

 

 192.168.51.60:8000

 

以上是关于docker-compose 完整打包发布, 多服务,多节点SPRING CLOUD ,EUREKA 集群的主要内容,如果未能解决你的问题,请参考以下文章

Docker部署LNMP完整教程

Docker部署LNMP完整教程

搭建Jenkins,Gitlab自动化打包部署项目

搭建Jenkins,Gitlab自动化打包部署项目

一docker-compose部署elasticsearch+hanlp(7.16.2版本)----附完整镜像

Docker + Maven + Docker-compose