前言

本文根据前面所学习的知识,使用Maven插件docker-maven-plugin配合Docker+ Docker Compose完成部署微服务项目。

前期准备工作

  1. Linux虚拟机或云主机(云主机需要关注安全组)
  2. 安装好Docker
  3. 安装好Docker Compose

    部署流程

    Docker开启远程API

    用vim编辑修改docker.service文件

    1. vim /usr/lib/systemd/system/docker.service

    需要修改的部分

    1. ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

    修改后的内容

    1. ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

    重启Docker

    1. systemctl restart docker

    引入插件

    1. <plugin>
    2. <groupId>io.fabric8</groupId>
    3. <artifactId>docker-maven-plugin</artifactId>
    4. <version>0.33.0</version>
    5. <executions>
    6. <!--如果想在项目打包时构建镜像添加-->
    7. <execution>
    8. <id>build-image</id>
    9. <phase>package</phase>
    10. <goals>
    11. <goal>build</goal>
    12. <!--<goal>push</goal>-->
    13. </goals>
    14. </execution>
    15. </executions>
    16. <configuration>
    17. <!-- Docker 远程管理地址-->
    18. <dockerHost>http://192.168.1.10:2375</dockerHost>
    19. <!-- Docker 推送镜像仓库地址-->
    20. <!--<pushRegistry>http://192.168.1.10:5000</pushRegistry>-->
    21. <images>
    22. <image>
    23. <!--由于推送到私有镜像仓库,镜像名需要添加仓库地址-->
    24. <!--<name>192.168.1.10:5000/ronan/${project.name}:${project.version}</name>-->
    25. <!--<name>192.168.1.10:5000/ronan/${project.name}:v1.0</name>-->
    26. <name>ronan/${project.name}:${project.version}</name>
    27. <!--定义镜像构建行为-->
    28. <build>
    29. <!--定义基础镜像-->
    30. <from>java:8</from>
    31. <args>
    32. <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
    33. </args>
    34. <!--定义哪些文件拷贝到容器中-->
    35. <assembly>
    36. <!--定义拷贝到容器的目录-->
    37. <targetDir>/</targetDir>
    38. <!--只拷贝生成的jar包-->
    39. <descriptorRef>artifact</descriptorRef>
    40. </assembly>
    41. <!--定义容器启动命令-->
    42. <entryPoint>["java","-jar","-Xms256m","-Xmx256m","-XX:+UseG1GC","/${project.build.finalName}.jar"]</entryPoint>
    43. <!--定义维护者-->
    44. <maintainer>ronan</maintainer>
    45. </build>
    46. <!--定义容器启动行为-->
    47. <!--<run>
    48. &lt;!&ndash;设置容器名,可采用通配符&ndash;&gt;
    49. <containerNamePattern>${project.artifactId}</containerNamePattern>
    50. &lt;!&ndash;设置端口映射&ndash;&gt;
    51. <ports>
    52. <port>8022:8022</port>
    53. </ports>
    54. &lt;!&ndash;设置容器间连接&ndash;&gt;
    55. &lt;!&ndash;<links>
    56. <link>mysql:db</link>
    57. </links>&ndash;&gt;
    58. &lt;!&ndash;设置容器和宿主机目录挂载&ndash;&gt;
    59. <volumes>
    60. <bind>
    61. <volume>/etc/localtime:/etc/localtime</volume>
    62. <volume>/ronan/app/${project.artifactId}/logs:/var/logs</volume>
    63. </bind>
    64. </volumes>
    65. </run>-->
    66. </image>
    67. </images>
    68. </configuration>
    69. </plugin>

    使用脚本

    1. # 在微服务的根目录下运行以下命令,maven插件则会开始构建镜像
    2. mvn clean package

    编写Docker Compose脚本

  4. docker-compose-env.yml脚本,负责基础环境

    1. version: '3'
    2. services:
    3. ronan-nacos:
    4. image: nacos/nacos-server:1.4.2
    5. container_name: ronan-nacos
    6. ports:
    7. - "8848:8848"
    8. - "9848:9848"
    9. - "9555:9555"
    10. restart: always
    11. networks:
    12. - ronan-net
    13. volumes:
    14. - "/ronan/nacos/logs:/home/nacos/logs"
    15. - "/ronan/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties"
    16. environment:
    17. - PREFER_HOST_MODE=hostname
    18. - MODE=standalone
    19. - SPRING_DATASOURCE_PLATFORM=mysql
    20. - MYSQL_SERVICE_HOST=hs-jiangke.com
    21. - MYSQL_SERVICE_DB_NAME=ronan-nacos
    22. - MYSQL_SERVICE_PORT=3306
    23. - MYSQL_SERVICE_USER=root
    24. - MYSQL_SERVICE_PASSWORD=SophSell_99
    25. - MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
    26. - JVM_XMS=512m
    27. - JVM_XMX=512m
    28. - JVM_XMN=64m
    29. ronan-sentinel:
    30. image: bladex/sentinel-dashboard:latest
    31. container_name: ronan-sentinel
    32. ports:
    33. - "8858:8858"
    34. restart: always
    35. networks:
    36. - ronan-net
    37. volumes:
    38. - "/ronan/logs:/logs/csp"
    39. environment:
    40. - 'TZ="Asia/Shanghai"'
    41. - JVM_XMS=256m
    42. - JVM_XMX=256m
    43. - JVM_XMN=64m
    44. - JAVA_OPTS=-XX:+UseG1GC -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -Djava.security.egd=file:/dev/./urandom -Dcsp.sentinel.api.port=8719
    45. networks:
    46. ronan-net:
    47. driver: bridge
  5. docker-compose-app.yml脚本,负责业务服务

    1. version: '3'
    2. services:
    3. ronan-gateway:
    4. image: ronan/ronan-gateway:20220530
    5. container_name: ronan-gateway
    6. ports:
    7. - "9090:9090"
    8. volumes:
    9. - "/ronan/logs:/logs"
    10. - "/etc/localtime:/etc/localtime"
    11. restart: always
    12. external_links:
    13. - ronan-nacos:ronan-nacos
    14. networks:
    15. - ronan-net
    16. environment:
    17. - 'TZ="Asia/Shanghai"'
    18. ronan-ums-web:
    19. image: ronan/ronan-ums-web:20220530
    20. container_name: ronan-ums-web
    21. ports:
    22. - "9096:9096"
    23. volumes:
    24. - "/ronan/logs:/logs"
    25. - "/etc/localtime:/etc/localtime"
    26. restart: always
    27. networks:
    28. - ronan-net
    29. external_links:
    30. - ronan-nacos:ronan-nacos
    31. - ronan-sentinel:ronan-sentinel
    32. environment:
    33. - 'TZ="Asia/Shanghai"'
    34. ronan-third-web:
    35. image: ronan/ronan-third-web:20220530
    36. container_name: ronan-third-web
    37. ports:
    38. - "9095:9095"
    39. volumes:
    40. - "/ronan/logs:/logs"
    41. - "/etc/localtime:/etc/localtime"
    42. restart: always
    43. networks:
    44. - ronan-net
    45. external_links:
    46. - ronan-nacos:ronan-nacos
    47. - ronan-sentinel:ronan-sentinel
    48. environment:
    49. - 'TZ="Asia/Shanghai"'
    50. ronan-message-web:
    51. image: ronan/ronan-message-web:20220530
    52. container_name: ronan-message-web
    53. ports:
    54. - "9092:9092"
    55. volumes:
    56. - "/ronan/logs:/logs"
    57. - "/etc/localtime:/etc/localtime"
    58. restart: always
    59. networks:
    60. - ronan-net
    61. external_links:
    62. - ronan-nacos:ronan-nacos
    63. - ronan-sentinel:ronan-sentinel
    64. environment:
    65. - 'TZ="Asia/Shanghai"'
    66. networks:
    67. ronan-net:
    68. external:
    69. name: "ronan_ronan-net"

    上传脚本文件到服务器

    ```bash

    执行env脚本

    docker-compose -f docker-compose-env.yml up -d

查看env运行容器日志

docker-compose -f docker-compose-env.yml logs —tail 10 -f 容器名

执行app脚本

docker-compose -f docker-compose-app.yml up -d

停止并且删除容器

docker-compose -f docker-compose-app.yml down ```

总结

至此,部署完成