原文地址:https://github.com/eacdy/spring-cloud-book/blob/master/3%20%E4%BD%BF%E7%94%A8Docker%E6%9E%84%E5%BB%BA%E5%BE%AE%E6%9C%8D%E5%8A%A1/3.8.5%20%E4%BD%BF%E7%94%A8Docker%20Compose%E9%83%A8%E7%BD%B2%E9%A1%B9%E7%9B%AE.md

经过前文对Docker Compose的讲解,我们以Spring Cloud章节的几个示例项目为例,讲解如何使用Docker Compose部署项目:

准备工作

使用到的示例项目有:

项目名称 作用
microservice-api-gateway API Gateway
microservice-consumer-movie-ribbon-with-hystrix 服务消费者
microservice-discovery-eureka 服务发现
microservice-hystrix-dashboard 监控
microservice-hystrix-turbine Turbine
microservice-provider-user 服务提供者

要想使用Docker Compose部署项目,我们首先得将项目打包成Docker镜像。本文使用Docker的Maven插件将项目打包成Docker镜像,当然也可以使用Dockerfile或者其他方式打包。

  • 为了管理方便,我们首先在父pom中添加插件管理:
    <pluginManagement>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
</plugin>
</plugins>
</pluginManagement>
  • 然后依次在上文提到的6个项目添加以下内容:
 <build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
  • 在父项目所在路径下,执行命令:
mvn clean package

这样,项目就会为各个项目打包成jar包,并且自动制作成Docker镜像。

运行docker images 查看本地镜像:

reg.itmuch.com/microservice-api-gateway                          latest              98c2997cb8c6        3 days ago          678.4 MB
reg.itmuch.com/microservice-hystrix-turbine latest c482e8da54dc 3 days ago 677.6 MB
reg.itmuch.com/microservice-hystrix-dashboard latest 1daa15df3508 3 days ago 666.7 MB
reg.itmuch.com/microservice-consumer-movie-ribbon-with-hystrix latest 694634d340b1 3 days ago 677.2 MB
reg.itmuch.com/microservice-provider-user latest 2049cfe6794c 3 days ago 689.3 MB
reg.itmuch.com/microservice-discovery-eureka latest 73e2e80bf567 3 days ago 679.4 MB

我们发现Docker镜像已经制作成功了。

编写docker-compose.yml文件

在任意路径,新建一个docker-compose.yml文件,并添加如下内容:

microservice-discovery-eureka:
image: reg.itmuch.com/microservice-discovery-eureka
ports:
- :
hostname: discovery
microservice-provider-user:
image: reg.itmuch.com/microservice-provider-user
ports:
- :
links:
- microservice-discovery-eureka
microservice-consumer-movie-ribbon-with-hystrix:
image: reg.itmuch.com/microservice-consumer-movie-ribbon-with-hystrix
ports:
- :
links:
- microservice-discovery-eureka
microservice-hystrix-dashboard:
image: reg.itmuch.com/microservice-hystrix-dashboard
ports:
- :
links:
- microservice-discovery-eureka
- microservice-hystrix-turbine
microservice-hystrix-turbine:
image: reg.itmuch.com/microservice-hystrix-turbine
ports:
- :
links:
- microservice-discovery-eureka
microservice-api-gateway:
image: reg.itmuch.com/microservice-api-gateway
ports:
- :
links:
- microservice-discovery-eureka

相信经过前文多docker-compose.yml的讲解,大家已经能够看懂这个yaml文件了,简单讲解一下:

第一个段落:

microservice-discovery-eureka:                                    # 指定一个名词
image: reg.itmuch.com/microservice-discovery-eureka # 指定所使用的镜像
ports: # 指定端口映射
- :
hostname: discovery # 指定主机名

第二个段落:

microservice-hystrix-dashboard:
image: reg.itmuch.com/microservice-hystrix-dashboard
ports:
- :
links:
- microservice-discovery-eureka # 表示连接到某个服务
- microservice-hystrix-turbine

我们看到microservice-discovery-eureka 这个服务配置了hostname。为什么呢?

假设不配置,下文links 配置了microservice-discovery-eureka ,默认将会使用该名称访问。而在我们各个配置文件中配置了eureka.client.serviceUrl.defaultZone ,是http://discovery:8761/eureka/ ,所以要保持一致,为microservice-discovery-eureka 这个服务配置一下hostname。

启动测试与故障排查

我们在docker-compose.yml所在路径执行:

docker-compose up

即可启动容器,容器启动时,应用的大量报错暂且不管,后文会有详细的讲解。

我们发现容器启动后,我们按照如下表格,依次访问进行测试:

应用 地址 测试结果  
microservice-discovery-eureka http://192.168.11.143:8761/ 正常  
microservice-provider-user http://192.168.11.143:8000/1 正常  
microservice-consumer-movie-ribbon-with-hystrix http://192.168.11.143:8011/ribbon/1 不正常 走了fallback
microservice-hystrix-turbine http://192.168.11.143:8031/turbine.stream 不正常 一直ping
microservice-api-gateway http://192.168.11.143:8050/movie/ribbon/1 不正常 500错误
microservice-hystrix-dashboard http://192.168.11.143:8030/hystrix.stream 正常  

发现测试到microservice-hystrix-turbine 开始发生异常,那么为什么会出现异常呢?

访问:[http://192.168.11.143:8011/ribbon/1](http://192.168.11.143:8011/ribbon/1) ,会看到日志:

microservice-hystrix-turbine_1                     | 2016-09-26 11:17:44.849  INFO 1 --- [        Timer-0] c.n.t.monitor.instance.InstanceMonitor   : Url for host: http://ribbon:8011/hystrix.stream default
microservice-hystrix-turbine_1 | 2016-09-26 11:17:44.995 WARN 1 --- [InstanceMonitor] c.n.t.monitor.instance.InstanceMonitor : Stopping InstanceMonitor for: ribbon default

好像知道了点什么……我们查看Eureka( http://192.168.11.143:8761/)界面,发现microservice-consumer-movie-ribbon-with-hystrix 这个服务在注册中心登记的地址是http://ribbon:8011/info 。而此时microservice-consumer-movie-ribbon-with-hystrix 而这个应用并没有绑定主机名,试问要如何访问到呢?再回溯到启动的日志,我们发现其实这个应用连启动都没启动成功。

我们将docker-compose.yml修改一下,变为:

microservice-discovery-eureka:
image: reg.itmuch.com/microservice-discovery-eureka
ports:
- :
hostname: discovery
microservice-provider-user:
image: reg.itmuch.com/microservice-provider-user
ports:
- :
links:
- microservice-discovery-eureka
microservice-consumer-movie-ribbon-with-hystrix:
image: reg.itmuch.com/microservice-consumer-movie-ribbon-with-hystrix
ports:
- :
links:
- microservice-discovery-eureka
hostname: ribbon
microservice-hystrix-dashboard:
image: reg.itmuch.com/microservice-hystrix-dashboard
ports:
- :
links:
- microservice-discovery-eureka
- microservice-hystrix-turbine
microservice-hystrix-turbine:
image: reg.itmuch.com/microservice-hystrix-turbine
ports:
- :
links:
- microservice-discovery-eureka
- microservice-consumer-movie-ribbon-with-hystrix
microservice-api-gateway:
image: reg.itmuch.com/microservice-api-gateway
ports:
- :
links:
- microservice-discovery-eureka
- microservice-consumer-movie-ribbon-with-hystrix

重新启动测试,发现一切正常了。

Docker 使用docker-compose部署项目的更多相关文章

  1. Docker Compose部署项目到容器-基于Tomcat和mysql的项目yml配置文件代码

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  2. Docker Compose部署项目到容器-基于Tomcat和mysql的商城项目(附源码和sql下载)

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  3. docker安装Tomcat软件,部署项目

    1 搜索tomcat镜像 $ sudo docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tom ...

  4. docker | jenkins 实现自动化部署项目,后端躺着把运维的钱挣了!(上)

    前言 背景 最近在帮学校导师写项目,团队有4个人,项目前后端分离.如果是选择瀑布式开发:(在约定好接口的情况下)A.B同学写前端,C.D同学写后端,然后约定一个时间统一联调,最后将项目交付安装到客户机 ...

  5. Docker安装tomcat和部署项目

    随着微服务的流行,Docker越来越流行,正如它的理念"Build, Ship, and Run Any App, Anywhere"一样,Docker提供的容器隔离技术使得开发人 ...

  6. Ubuntu Server 上使用Docker Compose 部署Nexus(图文教程)

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  7. Docker Compose部署Nexus3时的docker-compose,yml代码

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  8. Docker Compose部署GitLab服务,搭建自己的代码托管平台(图文教程)

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  9. SpringBoot使用Docker快速部署项目

    1.简介 建议阅读本文最好对Dokcer有一些了解 首先我们先了解一下Docker是什么 Docker 属于 Linux 容器的一种封装,提供简单易用的容器使用接口.它是目前最流行的 Linux 容器 ...

随机推荐

  1. centos7 启用iptables

    在centos 7下启用iptables systemctl stop firewalld.service systemctl disable firewalld.service yum instal ...

  2. Xamarin XAML语言教程构建ControlTemplate控件模板 (二)

    Xamarin XAML语言教程构建ControlTemplate控件模板 (二) (2)打开MainPage.xaml文件,编写代码,将构建的控件模板应用于ContentView中.代码如下: &l ...

  3. 服务认证暴力破解工具Crowbar

    服务认证暴力破解工具Crowbar   Crowbar是Kali Linux新增的一款服务认证暴力破解工具.该工具支持OpenVPN.RDP.SSH和VNC服务.该工具具备常见的暴力破解功能,如主机字 ...

  4. [HNOI2018]道路(DP)

    题目描述 W 国的交通呈一棵树的形状.W 国一共有n−1n - 1n−1 个城市和nnn 个乡村,其中城市从111 到n−1n - 1n−1 编号,乡村从111 到nnn 编号,且111 号城市是首都 ...

  5. HTML5 Boilerplate笔记(2)(转)

    最近看到了HTML5 Boilerplate模版,系统的学习与了解了一下.在各种CSS库.JS框架层出不穷的今天,能看到这么好的HTML模版,感觉甚爽.写篇博客,推荐给大家使用.   一:HTML5 ...

  6. Delphi 7下最小化到系统托盘

    在Delphi 7下要制作系统托盘,只能制作一个比较简单的系统托盘,因为ShellAPI文件定义的TNotifyIconData结构体是比较早的版本.定义如下: 123456789   _NOTIFY ...

  7. tessellation 曲面细分 on Android

    Mac OS X 10.8 (OpenGL 3.2), MacOSX 10.9 (OpenGL 3.2 to 4.1) Windows with NVIDIA since 2006 (GeForce ...

  8. 面试题:判断两个字符串是否互为回环变位(Circular Rotaion)

    题干:   如果字符串 s 中的字符循环移动任意位置之后能够得到另一个字符串 t,那么 s 就被称为 t 的回环变位(circular rotation).   例如,ACTGACG 就是 TGACG ...

  9. http://blog.csdn.net/pet8766/article/details/8186955

    http://blog.csdn.net/pet8766/article/details/8186955

  10. Myeclipse中文件已经上传到server文件夹下,文件也没有被占用,可是页面中无法读取和使用问题的解决方法

    这个问题是因为Myeclipse中文件不同步引起的.在Myeclipse中,project文件是由Myeclipse自己主动扫描加入的,假设在外部改动了project文件夹中的文件但又关闭了自己主动刷 ...