springCloud Finchley 微服務架構從入門到精通【十】Docker Maven插件部署項目

1、Centos7 上安裝docker

docker服務器的安裝部署java

一、Docker 要求 CentOS 系統的內核版本高於 3.10 ,驗證CentOS 版本是否支持 Docker 。linux

root@bogon ~]# uname -r

二、更新yum包到最新spring

root@bogon ~]# sudo yum update

三、卸載舊版本docker

[root@bogon ~]# sudo yum remove docker  docker-common docker-selinux docker-engine

四、設置yum源apache

[root@bogon ~]# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

五、安裝dockercentos

[root@bogon ~]# sudo yum install docker-ce

六、啓動並加入開機啓動springboot

[root@bogon ~]# sudo systemctl start docker
[root@bogon ~]# sudo systemctl enable docker

七、驗證是否安裝成功服務器

[root@bogon ~]# docker version

clipboard.png

2、centos7安裝 jdk 1.8 和 maven 3.5

因爲安裝過於簡單,自行百度微信

2、修改springcloud項目配置

一、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>

二、構建鏡像文件

進入項目目錄,運行 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

clipboard.png

部署成功。

架構師Q羣:618578034

spring cloud社區羣(需加微信拉入)

clipboard.png

微信公衆號:java架構師修行

本公衆號將要使用一年的時間,按照JAVA高級軟件架構師實戰培訓的路線發佈一期完整的架構文章,難度由淺入深,適合有必定開發基礎想轉架構和正在作初級架構開發的人員學習

clipboard.png

相關文章
相關標籤/搜索