所谓Docker化就是将服务放在容器里。我们目前的服务有基于Java和Python的两种基础镜像。
服务Docker化
- dockerhub搜索Java基础镜像;
- 拉取Java镜像:docker pull openjdk:7-jre;
- docker images|grep jdk:查看镜像;
- docker run -it --entrypoint bash openjdk:7-jre;
- exit:退出基础镜像
- 依次部署各服务到该基础镜像;
user-thrift-service容器化
-
修改配置
- 加入打包插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 编写Dockerfile文件
FROM openjdk:7-jre
MAINTAINER xxx xxx@imooc.com
COPY target/user-thrift-service-1.0-SNAPSHOT.jar /user-service.jar
ENTRYPOINT ["java", "-jar", "/user-service.jar"]
- 编写shell脚本
#!/usr/bin/env bash
mvn clean package
docker build -t user-service:latest .
-
运行容器
message-thrift-python-service
- 找Python的基础镜像
- 拉取python镜像:docker pull pyhton:3.6
- 运行Python基础镜像:docker run -it --entrypoint bash python:3.6
- 因为Python代码依赖thrift,在上面下载的基础镜像里没有thrift的东西。所以需要先做一层带有thrift模块的基础镜像。
- vi Dockerfile.base
FROM python:3.6
MAINTAINER xxx xxx@imooc.com
RUN pip install thrift
- 编写构建base镜像的脚本.vi build_base.sh
#!/usr/bin/env bash
docker build -t python-base:latest -f Dockerfile.base .
- 查看镜像
docker images|grep python-base -
编写Dockerfile
- 编写打镜像的脚本
#!/usr/bin/env bash
docker build -t message-service:latest .
- 执行上面的shell脚本,打包镜像。运行容器
docker run -it message-service:latest
user-edge-service
- 修改pom,添加打包插件
- 修改配置文件
主要是修改redis地址跟依赖服务的地址
server.name=user-edge-service
server.port=8082
thrift.user.ip=user-service
thrift.user.port=7911
thrift.message.ip=message-service
thrift.message.port=9090
#redis config
spring.redis.host=${redis.address}
spring.redis.port=6379
spring.redis.timeout=30000
- 编写Dockerfile
FROM openjdk:7-jre
MAINTAINER xxx xxx@imooc.com
COPY target/user-edge-service-1.0-SNAPSHOT.jar /user-edge-service.jar
ENTRYPOINT ["java", "-jar", "/user-edge-service.jar"]
- 打包镜像的shell脚本
#!/usr/bin/env bash
mvn clean package
docker build -t user-edge-service:latest .
- 运行脚本,打包镜像。docker images |grep user-edge-service
- 运行容器:docker run -it user-edge-service:latest --redis.address=127.0.0.1
course-dubbo-service
- 编写Dockerfile
FROM openjdk:7-jre
MAINTAINER xxx xxx@imooc.com
COPY target/course-dubbo-service-1.0-SNAPSHOT.jar /course-service.jar
ENTRYPOINT ["java", "-jar", "/course-service.jar"]
- 打包镜像的shell
#!/usr/bin/env bash
mvn clean package
docker build -t course-service:latest .
- 修改配置:
#dubbo 配置
spring.dubbo.application.name=course-service
spring.dubbo.registry.address=zookeeper://${zookeeper.address}:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
#spring.dubbo.protocol.host=127.0.0.1
spring.dubbo.scan=com.imooc.course
#数据源的配置
spring.datasource.url=jdbc:mysql://${mysql.address}/db_course
spring.datasource.username=root
spring.datasource.password=aA111111
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
thrift.user.ip=user-service
thrift.user.port=7911
course-edge-service
FROM openjdk:7-jre
MAINTAINER xxx xxx@imooc.com
COPY target/course-edge-service-1.0-SNAPSHOT.jar /course-edge-service.jar
ENTRYPOINT ["java", "-jar", "/course-edge-service.jar"]
api-gateway-zuul
FROM openjdk:7-jre
MAINTAINER xxx xxx@imooc.com
COPY target/api-gateway-zuul-1.0-SNAPSHOT.jar /api-gateway-zuul.jar
ENTRYPOINT ["java", "-jar", "/api-gateway-zuul.jar"]
- 编写shell
#!/usr/bin/env bash
mvn package
docker build -t api-gateway-zuul:latest .
Docker下的服务通讯
容器通讯的几种方式:
- 直接通讯,通过容器的Ip和端口去访问服务。这种方式应用较少。因为Docker的IP非常不稳定,每次重启都会发生变化;
- 服务映射端口,即把容器的端口转换为主机的端口。然后依赖的容器去访问主机的IP和端口即可;
- 使用Docker的Link机制。Link到一个容器之后就可以直接通过名字访问。
开发的微服务使用第三种方式,即Link机制,即根据名字去访问服务。而依赖的基础环境,如数据库、redis、和zookeeper使用第二种方式实现。
使用DockerCompose来描述服务间的依赖关系:
vi docker-compose.yml
version: '3' #版本
networks:
default:
external:
name: imooc-network
# 按照开发和依赖的顺序编写services
services:
message-service:
image: message-service:latest
user-service:
image: user-service:latest
command: #配置数据库地址
- "--mysql.address=172.19.0.2"
user-edge-service:
image: user-edge-service:latest
links:# 依赖关系。依赖于user-service和message-service
- user-service
- message-service
command:
- "--redis.address=172.19.0.3"
course-service:
image: course-service:latest
links:
- user-service
command:
- "--mysql.address=172.19.0.2"
- "--zookeeper.address=172.19.0.4"
course-edge-service:
image: course-edge-service:latest
links:
- user-edge-service
command:
- "--zookeeper.address=172.19.0.4"
api-gateway-zuul:
image: api-gateway-zuul:latest
links:
- course-edge-service
- user-edge-service
ports:#对外端口映射
- 8080:8080
- 启动基础服务:redis、mysql、zookeeper
- 执行docker-compose up -d