『高级篇』docker之微服务服务docker化(18)
2019-02-03 10:11:04
李明
  • 访问次数: 375
  • 注册日期: 2018-07-09
  • 最后登录: 2019-08-08

原创文章,欢迎转载。转载请注明:转载自 IT人故事会 ,谢谢!原文链接地址: 『高级篇』docker之微服务服务docker化(18)

这次进入微服务的部署,代码也基本都通过了。如果比做一首歌曲的话,前奏已经结束,现在开始我们的高潮部分,如果吧我们的服务使用docker,使用服务编排工具,把项目给部署运行起来。源码: https://github.com/limingios/msA-docker

注意

因docker话都是在linux环境,为了方便编写dockerfile文件,我切换到mac本上进行演示,目的只有一个方便开发sh。方便使用。CICD学习实践首选电脑还是mac。

微服务部署

  1. 服务docker化,可以在docker下运行。

  2. Docker仓库创建,docker创建的镜像push到仓库里面。

  3. 构建高可用的集群环境,Mesos,Swarm,kubernetes。运行这3个服务编排工具把我们的服务运行和编排起来。可以优雅的启动,停止,扩充容,故障的恢复。

docker化

服务有个适合的环境,服务可以运行起来,给他准备一个环境,比如服务是个种子,我们需要准备一片土地,服务是一条鱼,就需要准备一片大海。源码中的服务有一个是python写的,有一个是java写的,也就是需要2个运行环境,一个基于java,一个基于python。


v2-0b044d6d354e541306ef5d3f63884cac_hd.p

  • java镜像

去hub.docker.com 搜索java,找到tag


v2-93f9c609dd2454cdd41e5e1fc4b8c938_hd.p

  • 下载java镜像

先配置加速, https://www.daocloud.io/mirror

docker pull java:openjdk-8
 docker images|grep jdk


v2-55151d4892f0cac764939a618f2ea184_hd.p


v2-5ff207e76d1887b385c4ff234d2c8e5a_hd.p

  • 安装jdk容器

docker run -it --entrypoint bash java:openjdk-8
java -version


v2-3f81acfd0b07043af32f83e4f53c2442_hd.p


v2-d70fdf7bdb8532615631983d90312a97_hd.p

开始dockerfile的开发

熟悉docker的老铁应该都知道,如果要编译docker的话需要docker化,首选需要开发dockerfile文件。

  • 准备工作

文件中不能有写死的情况,如果写死了,每次服务的变更都需要变更镜像。为了减少构建镜像的过程,尽量吧数据库的访问地址,经常会发生变化的东西,需要踢出去,不要在配置文件中配置死,针对数据库的访问,不能地址直接写死,mysql的地址,当服务运行在docker之后,他的ip是实时都在变化的,不能写死在镜像里,直接就找不到了就报错了。还有个问题,我们的服务以什么样的形式放在我们docker里面,springboot之所以说适用于微服务,他有个很大的好处,它可以将咱们的服务构建成一个fat jar,只有一个jar包,然后通过java的一个命令:java -jar 文件.jar 运行起来,这种方式对于微服务来说也是很友好的,也非常的简单,就使用这种方式来做。变量的方式就可以通过springboot --mysql.address 就可以传递进来了。

  • 构建user-thrift-service

修改配置文件 和 pom.xml文件

application.properties

service.name=user-thrift-service
service.port=7911
#数据源的配置
spring.datasource.url=jdbc:mysql://{mysql.address}:3306/db_userspring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.idig8</groupId>
    <artifactId>user-thrift-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.idig8</groupId>
            <artifactId>user-thrift-service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <e x e cutions>
                    <e x e cution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </e x e cution>
                </e x e cutions>
            </plugin>
        </plugins>
    </build>
</project>

Dockfile编写

FROM java:openjdk-8
MAINTAINER liming www.idig8.com
COPY target/user-thrift-service-1.0-SNAPSHOT.jar /user-thrift-service.jar
ENTRYPOINT ["java","-jar","/user-thrift-service.jar"]


v2-ac26b9436c6135c2816106e648b7f99b_hd.p

执行build生成镜像

docker build -t user-thrift-service:latest .


v2-6f056b19fe062798d91ff349f15adb8d_hd.p

查看ip地址

ifconfig


v2-0cdf5fd13531ba0e1f0badff87b967fd_hd.p

生成容器

docker run -it user-thrift-service:latest --mysql.address=192.168.1.140


v2-48591f4d2d0405396c019a8324034fc1_hd.p

新建立build.sh

#!/usr/bin/env bash
mvn package
docker build -t user-thrift-service:latest .


v2-fba0eb103a6589895eb1b5e4043a3b98_hd.p

  • 构建message-thrift-python-service

后面很多的服务,都依赖它,必须把它做好。它是一个python的服务,我们需要找一个python的镜像。去官方找吧

docker pull python:3.6
 docker images|grep python


v2-599abb1900ef51e14c8b8b2204bc8182_hd.p

编写Dockerfile

FROM python:3.6
MAINTAINER liming www.idig8.com
RUN pip install thrift
ENV PYTHONPATH /
COPY message /message
ENTRYPOINT ["python","/message/message_service.py"]


v2-e08bd3d729c196a8925c6a303b6a3a88_hd.p

docker build -t message-thrift-python-service:latest .


v2-299d7a6aabbc691d84da6d102d54ad26_hd.p

build开发

#!/usr/bin/env bash
docker build -t message-thrift-python-service:latest .


v2-66b961ed18648380357a3cd05a70bc23_hd.p

镜像生成容器

docker run -it message-thrift-python-service:latest


v2-c22b24085fcbeea1bbc16d5ac6a3647d_hd.p

  • 构建user-edge-service

微服务的依赖,了解docker的老铁应该知道可以通过link的方式,去根据名字搜索到这个服务,只要不在开发范围内的,认为是通过另外的介入方式ip,域名的方式。通过docker分为2种情况,微服务和微服务之间的通信,微服务和外围系统的通信。修改配置文件 和 pom.xml文件

application.properties

server.name=user-edge-service
server.port=8082
thrift.user.ip=user-thrift-service
thrift.user.port=7911
thrift.message.ip=message-thrift-python-service
thrift.message.port=9090#redis configspring.redis.host=${redis.address}
spring.redis.port=6379
spring.redis.password=liming
spring.redis.timeout=30000

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.idig8</groupId>
    <artifactId>user-edge-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.idig8</groupId>
            <artifactId>user-thrift-service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.idig8</groupId>
            <artifactId>message-thrift-service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <e x e cutions>
                    <e x e cution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </e x e cution>
                </e x e cutions>
            </plugin>
        </plugins>
    </build>
</project>

Dockerfile编写

FROM java:openjdk-8
MAINTAINER liming www.idig8.com
COPY target/user-edge-service-1.0-SNAPSHOT.jar /user-edge-service.jar
ENTRYPOINT ["java","-jar","/user-edge-service.jar"]


v2-a311266871e8fdec601d25067e07fdba_hd.p

build.sh 编写

#!/usr/bin/env bash
mvn package
docker build -t user-edge-service:latest .


v2-e2cc442a662e185b4d5345ab50cbda23_hd.p

sh build.sh


v2-f9d59b75a7c99fc39f1ca378e35d0c9c_hd.p


v2-f4e78348e40272dcbec0d11024f67c27_hd.p

创建容器

docker run -it user-edge-service:latest --redis.address=192.168.1.140


v2-d9c3c71a7ec02b7e8c3a428ecd4a7bc0_hd.p

  • 构建course-dubbo-service

微服务的依赖,了解docker的老铁应该知道可以通过link的方式,去根据名字搜索到这个服务,只要不在开发范围内的,认为是通过另外的介入方式ip,域名的方式。通过docker分为2种情况,微服务和微服务之间的通信,微服务和外围系统的通信。修改配置文件 和 pom.xml文件

application.properties

#dubbo 配置
spring.dubbo.application.name=course-dubbo-service
spring.dubbo.registry.address=zookeeper://${zookeeper.address}:2181spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
#spring.dubbo.protocol.host=127.0.0.1
spring.dubbo.scan=com.idig8.course
#数据源的配置
spring.datasource.url=jdbc:mysql://${mysql.address}:3306/db_coursespring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
thrift.user.ip=user-thrift-service
thrift.user.port=7911

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.idig8</groupId>
    <artifactId>course-dubbo-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
        <dependency>
            <groupId>com.idig8</groupId>
            <artifactId>course-dubbo-service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.idig8</groupId>
            <artifactId>user-thrift-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.idig8</groupId>
            <artifactId>user-thrift-service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <e x e cutions>
                    <e x e cution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </e x e cution>
                </e x e cutions>
            </plugin>
        </plugins>
    </build>
</project>

Dockerfile编写

FROM java:openjdk-8
MAINTAINER liming www.idig8.com
COPY target/course-dubbo-service-1.0-SNAPSHOT.jar /course-dubbo-service.jar
ENTRYPOINT ["java","-jar","/course-dubbo-service.jar"]


v2-fddb30f9d1c7e1ad7c953dd4dad3e60c_hd.p

build.sh 编写

#!/usr/bin/env bash
mvn package
docker build -t course-dubbo-service:latest .sh build.sh


v2-3d3f8cf8736fb04b4fb27a48aa210ea8_hd.p


v2-817577d2a9568b5bf697da20e0753597_hd.p

创建容器

docker run -it course-dubbo-service:latest --redis.address=192.168.1.140 --zookeeper.address=192.168.1.140


v2-45be078634cd0fbdd18ccd44d46492af_hd.p

  • 构建course-edge-service

跟之间的基本是一样的,都是重复的工作。直接看源码吧

  1. 修改pom文件增加maven构建

  2. 增加dockerfile文件

  3. 增加build.sh文件

  4. 资源文件的修改


v2-ebe9dcbcd1a5ec0eef25858c84cf4171_hd.p

docker run -it course-edge-service:latest --zookeeper.address=192.168.1.140


edit


v2-b7b8622a8262e4d756b0ed119ef41a52_hd.p

docker run -it gataway-zuul:latest


v2-3c0cb32bca33b0f3029504a10b67ce8b_hd.p

PS:把所有单独的服务,做成了镜像,下次想个办法服务和镜像统一的运行起来。


v2-7f75c93f587eca2c35da2664ce67e513_hd.p