springCloud Finchley 微服务架构从入门到精通【十】Docker Maven插件部署项目
程序员文章站
2024-03-17 22:35:10
...
一、Centos7 上安装docker
docker服务器的安装部署
1、Docker 要求 CentOS 系统的内核版本高于 3.10 ,验证CentOS 版本是否支持 Docker 。
root@bogon ~]# uname -r
2、更新yum包到最新
root@bogon ~]# sudo yum update
3、卸载旧版本
[root@bogon ~]# sudo yum remove docker docker-common docker-selinux docker-engine
4、设置yum源
[root@bogon ~]# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
5、安装docker
[root@bogon ~]# sudo yum install docker-ce
6、启动并加入开机启动
[root@bogon ~]# sudo systemctl start docker
[root@bogon ~]# sudo systemctl enable docker
7、验证是否安装成功
[root@bogon ~]# docker version
二、centos7安装 jdk 1.8 和 maven 3.5
由于安装过于简单,自行百度
二、修改springcloud项目配置
1、pom.xml 中添加 docker-maven-plugin 插件,完整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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mayi.springcloud</groupId>
<artifactId>commonservice-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>commonservice-config</name>
<description>配置中心</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<!-- 注意imageName要符合正则[a-z0-9-_.]的,否则构建不会成功 -->
<imageName>commonservice-eureka</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2、构建镜像文件
进入项目目录,运行 mvn clean package docker:build -DskipTests
注: -DskipTests 为跳过测试,springboot测试类会再启动一个主类,有时会报单例错误
[root@localhost Downloads]# cd commonservice-eureka
[root@localhost commonservice-eureka]# mvn clean package docker:build -DskipTests
//以下为输出日志:
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:latest
---> d23bdf5b1b1b
Step 2/3 : ADD /commonservice-eureka-0.0.1-SNAPSHOT.jar //
---> b796a24d8225
Step 3/3 : ENTRYPOINT ["java", "-jar", "/commonservice-eureka-0.0.1-SNAPSHOT.jar"]
---> Running in b488f35f00dd
Removing intermediate container b488f35f00dd
---> e3279c0c77c7
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built e3279c0c77c7
Successfully tagged commonservice-eureka:latest
[INFO] Built commonservice-eureka
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15:49 min
[INFO] Finished at: 2018-05-25T02:34:34-07:00
[INFO] -----------------------------------------------------------------------
使用docker images查看镜像
[root@localhost commonservice-eureka]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
commonservice-eureka latest e3279c0c77c7 2 minutes ago 693MB
java latest d23bdf5b1b1b 16 months ago 643MB
[root@localhost commonservice-eureka]#
启动镜像
[root@localhost commonservice-eureka]# docker run -p 8761:8761 commonservice-eureka
访问虚拟机地址+8761
部署成功。
JAVA架构师QQ群(初建):618578034
spring cloud社区群(需加微信拉)
微信公众号:java架构师修行
本公众号将要使用一年的时间,按照JAVA高级软件架构师实战培训的路线发布一期完整的架构文章,难度由浅入深,适合有一定开发基础想转架构和正在做初级架构开发的人员学习