3.8.5 使用Docker Compose部署项目

经过前文对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中添加插件管理:
  1. <pluginManagement>
  2. <plugins>
  3. <plugin>
  4. <groupId>com.spotify</groupId>
  5. <artifactId>docker-maven-plugin</artifactId>
  6. <version>0.4.12</version>
  7. </plugin>
  8. </plugins>
  9. </pluginManagement>
  • 然后依次在上文提到的6个项目添加以下内容:
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>com.spotify</groupId>
  5. <artifactId>docker-maven-plugin</artifactId>
  6. <executions>
  7. <execution>
  8. <id>build-image</id>
  9. <phase>package</phase>
  10. <goals>
  11. <goal>build</goal>
  12. </goals>
  13. </execution>
  14. </executions>
  15. <configuration>
  16. <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
  17. <baseImage>java</baseImage>
  18. <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
  19. <resources>
  20. <resource>
  21. <targetPath>/</targetPath>
  22. <directory>${project.build.directory}</directory>
  23. <include>${project.build.finalName}.jar</include>
  24. </resource>
  25. </resources>
  26. </configuration>
  27. </plugin>
  28. </plugins>
  29. </build>
  • 在父项目所在路径下,执行命令:
  1. mvn clean package

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

运行docker images 查看本地镜像:

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

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

编写docker-compose.yml文件

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

  1. microservice-discovery-eureka:
  2. image: reg.itmuch.com/microservice-discovery-eureka
  3. ports:
  4. - 8761:8761
  5. hostname: discovery
  6. microservice-provider-user:
  7. image: reg.itmuch.com/microservice-provider-user
  8. ports:
  9. - 8000:8000
  10. links:
  11. - microservice-discovery-eureka
  12. microservice-consumer-movie-ribbon-with-hystrix:
  13. image: reg.itmuch.com/microservice-consumer-movie-ribbon-with-hystrix
  14. ports:
  15. - 8011:8011
  16. links:
  17. - microservice-discovery-eureka
  18. microservice-hystrix-dashboard:
  19. image: reg.itmuch.com/microservice-hystrix-dashboard
  20. ports:
  21. - 8030:8030
  22. links:
  23. - microservice-discovery-eureka
  24. - microservice-hystrix-turbine
  25. microservice-hystrix-turbine:
  26. image: reg.itmuch.com/microservice-hystrix-turbine
  27. ports:
  28. - 8031:8031
  29. links:
  30. - microservice-discovery-eureka
  31. microservice-api-gateway:
  32. image: reg.itmuch.com/microservice-api-gateway
  33. ports:
  34. - 8050:8050
  35. links:
  36. - microservice-discovery-eureka

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

第一个段落:

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

第二个段落:

  1. microservice-hystrix-dashboard:
  2. image: reg.itmuch.com/microservice-hystrix-dashboard
  3. ports:
  4. - 8030:8030
  5. links:
  6. - microservice-discovery-eureka # 表示连接到某个服务
  7. - 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所在路径执行:

  1. 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) ,会看到日志:

  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
  2. 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修改一下,变为:

  1. microservice-discovery-eureka:
  2. image: reg.itmuch.com/microservice-discovery-eureka
  3. ports:
  4. - 8761:8761
  5. hostname: discovery
  6. microservice-provider-user:
  7. image: reg.itmuch.com/microservice-provider-user
  8. ports:
  9. - 8000:8000
  10. links:
  11. - microservice-discovery-eureka
  12. microservice-consumer-movie-ribbon-with-hystrix:
  13. image: reg.itmuch.com/microservice-consumer-movie-ribbon-with-hystrix
  14. ports:
  15. - 8011:8011
  16. links:
  17. - microservice-discovery-eureka
  18. hostname: ribbon
  19. microservice-hystrix-dashboard:
  20. image: reg.itmuch.com/microservice-hystrix-dashboard
  21. ports:
  22. - 8030:8030
  23. links:
  24. - microservice-discovery-eureka
  25. - microservice-hystrix-turbine
  26. microservice-hystrix-turbine:
  27. image: reg.itmuch.com/microservice-hystrix-turbine
  28. ports:
  29. - 8031:8031
  30. links:
  31. - microservice-discovery-eureka
  32. - microservice-consumer-movie-ribbon-with-hystrix
  33. microservice-api-gateway:
  34. image: reg.itmuch.com/microservice-api-gateway
  35. ports:
  36. - 8050:8050
  37. links:
  38. - microservice-discovery-eureka
  39. - microservice-consumer-movie-ribbon-with-hystrix

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