经过前文对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: - 8761:8761 hostname: discovery microservice-provider-user: image: reg.itmuch.com/microservice-provider-user ports: - 8000:8000 links: - microservice-discovery-eureka microservice-consumer-movie-ribbon-with-hystrix: image: reg.itmuch.com/microservice-consumer-movie-ribbon-with-hystrix ports: - 8011:8011 links: - microservice-discovery-eureka microservice-hystrix-dashboard: image: reg.itmuch.com/microservice-hystrix-dashboard ports: - 8030:8030 links: - microservice-discovery-eureka - microservice-hystrix-turbine microservice-hystrix-turbine: image: reg.itmuch.com/microservice-hystrix-turbine ports: - 8031:8031 links: - microservice-discovery-eureka microservice-api-gateway: image: reg.itmuch.com/microservice-api-gateway ports: - 8050:8050 links: - microservice-discovery-eureka
相信经过前文多docker-compose.yml的讲解,大家已经能够看懂这个yaml文件了,简单讲解一下:
第一个段落:
microservice-discovery-eureka: # 指定一个名词 image: reg.itmuch.com/microservice-discovery-eureka # 指定所使用的镜像 ports: # 指定端口映射 - 8761:8761 hostname: discovery # 指定主机名
第二个段落:
microservice-hystrix-dashboard: image: reg.itmuch.com/microservice-hystrix-dashboard ports: - 8030:8030 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: - 8761:8761 hostname: discovery microservice-provider-user: image: reg.itmuch.com/microservice-provider-user ports: - 8000:8000 links: - microservice-discovery-eureka microservice-consumer-movie-ribbon-with-hystrix: image: reg.itmuch.com/microservice-consumer-movie-ribbon-with-hystrix ports: - 8011:8011 links: - microservice-discovery-eureka hostname: ribbon microservice-hystrix-dashboard: image: reg.itmuch.com/microservice-hystrix-dashboard ports: - 8030:8030 links: - microservice-discovery-eureka - microservice-hystrix-turbine microservice-hystrix-turbine: image: reg.itmuch.com/microservice-hystrix-turbine ports: - 8031:8031 links: - microservice-discovery-eureka - microservice-consumer-movie-ribbon-with-hystrix microservice-api-gateway: image: reg.itmuch.com/microservice-api-gateway ports: - 8050:8050 links: - microservice-discovery-eureka - microservice-consumer-movie-ribbon-with-hystrix
重新启动测试,发现一切正常了。
所有评论(0)