微服务框架(五)Docker镜像化Dubbo微服务

版权声明:本文为博主原创文章,若要转载请注明文章出处:http://blog.csdn.net/why_still_confused https://blog.csdn.net/why_still_confused/article/details/81391827

  此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。
  本文为将Dubbo微服务实现Docker镜像化

本系列文章中所使用的框架版本为Spring Boot 2.0.3-RELEASE,Spring 5.0.7-RELEASE,Dubbo 2.6.2。

Docker镜像化

需要打包为docker镜像的项目,artifactId请设置为无特殊字符的小写字母字串,否则docker-maven-plugin会出错

配置docker-maven-plugin插件

maven pom.xml加入com.spotify:docker-maven-plugin插件配置(配置对应properties)

通用Dubbo POM中已集成,作为parent引入即可,详见文档

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.0.0</version>
    <configuration>
        <imageName>${registry.url}dubbo/${project.artifactId}</imageName>
        <dockerDirectory>src/main/resources/docker</dockerDirectory>
        <imageTags>
            <imageTag>${image.tag}</imageTag>
            <imageTag>latest</imageTag>
        </imageTags>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <includes>
                    <include>${project.build.finalName}.jar</include>
                    <include>lib/*.jar</include>
                </includes>
            </resource>
        </resources>
    </configuration>
</plugin>

properties配置示例

参数 描述 可否为空
registry.url Docker私有仓库url 可为空,此时镜像名为dubbo/${project.artifactId}
image.tag 镜像标签 可为空,此时只生成latest镜像标签
    <properties>
        <registry.url>0.0.0.1/</registry.url>
        <image.tag>0.0.1</image.tag>
    </properties>

编写Dockerfile

Dockerfile文件路径需在docker-maven-plugin插件配置

注:文件存放路径需对应上述插件配置中<dockerDirectory>

 # Dockerfile
 # 基础镜像
FROM openjdk:8-jdk-alpine

 # 设置工作目录
WORKDIR /

 # 将jar文件拷贝到镜像中。注:docker-maven-plugin 会将jar文件拷贝到镜像构建目录中
ADD *.jar app.jar
ADD lib lib/

 # 运行时设置JAVA环境参数
ENV JAVA_OPTS="-Duser.timezone=Asia/Shanghai"

 # 端口暴露(推荐不暴露端口,在运行容器时才对端口进行绑定)
EXPOSE 9702

 #ENTRYPOINT不支持环境变量展开
ENTRYPOINT ["java", "-Duser.timezone=Asia/Shanghai", "-jar", "/app.jar"]

编写CI文件

编写CI文件.gitlab-ci.yaml,将docker镜像打包设置为手动触发

注:gitlab项目需配置环境变量DOCKER_HOST=tcp://<ip>:<port>

deploy_docker:
  stage: deploy
  script:
    - echo "Deploy as a docker container"
    - 'mvn clean package docker:build -DskipTests'
  when: manual
  only:
  - master

查看并运行镜像

查看镜像

docker images

运行镜像

docker run -d \
    --name <containerName> \
    --net dubbo \
    -e DUBBO_IP_TO_REGISTRY=<ip> \
    -e DUBBO_PORT_TO_REGISTRY=<port> \
    -p <ip>:<port>:<port> \
    -v dubbo-log:/log \
    <imageName> \
    --spring.profiles.active=<env>

参考资料:

  1. Maven Docker插件

猜你喜欢

转载自blog.csdn.net/why_still_confused/article/details/81391827
今日推荐