springboot打包成zip部署,並實現優雅停機

springboot打包成zip部署,並實現優雅停機

衆所周知springboot項目,使用springboot插件打包的話,會打包成一個包含依賴的可執行jar,很是方便。只要有java運行環境的電腦上,運行java -jar xxx.jar就能夠直接運行項目。java

可是這樣的缺點也很明顯,若是我要改個配置,要將jar包中的配置文件取出來,修改完再放回去。這樣作在windows下還比較容易。若是在linux上面就很費勁了。linux

另外若是代碼中須要讀取一些文件(好比說一張圖片),也被打進jar中,就沒辦法像在磁盤中時一句File file = new File(path)代碼就能夠讀取了。(固然這個可使用spring的ClassPathResource來解決)。git

還有不少公司項目上線後,都是增量發佈,這樣若是隻有一個jar 的話,增量發佈也是很麻煩的事情。雖然我是很討厭這種增量發佈的方式,由於會形成線上生產環境和開發環境有不少不一致的地方,這樣在找問題的時候會走不少彎路。很不幸我如今在的項目也是這樣的狀況,並且最近接的任務就是用springboot搭建一個定時任務服務,爲了維護方便,最後決定將項目打包成zip進行部署。github

網上找到了不少springboot打包成zip的文章,不過基本都是將依賴從springboot的jar中拿出來放到lib目錄中,再將項目的jar包中META-INF中指定lib到classpath中。這樣作仍是會有上面的問題。web

最後我決定本身經過maven-assembly-plugin來實現這個功能。spring

打包

首先maven-assembly-plugin是將項目打包的一個插件。能夠經過指定配置文件來決定打包的具體要求。apache

個人想法是將class打包到classes中,配置文件打包到conf中,項目依賴打包到lib中,固然還有本身編寫的啓動腳本在bin目錄中。windows

如圖tomcat

2019-08-12-153019.png

maven的target/classes下就是項目編譯好的代碼和配置文件。原來的作法是在assembly.xml中配置篩選,將該目錄下class文件打包進classes中,除class文件打包到conf中(bin目錄文件打包進bin目錄,項目依賴打包進lib目錄)。結果發現conf目錄下會有空文件夾(java包路徑)。安全

pom.xml

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptors>
            <descriptor>assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

assembly.xml

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <excludes>
                <exclude>
                    ${groupId}:${artifactId}
                </exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>bin</directory>
            <outputDirectory>/bin</outputDirectory>
            <fileMode>777</fileMode>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/conf</directory>
            <outputDirectory>/conf</outputDirectory>
            <excludes>
                <exclude>**/*.class</exclude>
                <exclude>META-INF/*</exclude>
            </excludes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/classes</outputDirectory>
            <includes>
                <include>**/*.class</include>
                <include>META-INF/*</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

其實這樣是不影響項目運行的,可是我看着很難受,嘗試了不少方法去修改配置來達到不打包空文件夾的效果。可是都沒成功。

而後我換了個方式,經過maven-resources-plugin插件將配置文件在編譯的時候就複製一份到target/conf目錄下,打包的時候配置文件從conf目錄中取。這樣就能夠避免打包空白文件夾到conf目錄中的狀況。

pom.xml

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>compile-resources</id>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                    <configuration>
                        <encoding>utf-8</encoding>
                        <useDefaultDelimiters>true</useDefaultDelimiters>
                        <resources>
                            <resource>
                                <directory>src/main/resources/</directory>
                                <filtering>true</filtering>
                                <includes><!--只對yml文件進行替換-->
                                    <include>*.yml</include>
                                </includes>
                            </resource>
                            <resource>
                                <directory>src/main/resources/</directory>
                                <filtering>false</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
                <execution>
                    <id>copy-resources</id>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                    <configuration>
                        <encoding>utf-8</encoding>
                        <useDefaultDelimiters>true</useDefaultDelimiters>
                        <resources>
                            <resource>
                                <directory>src/main/resources/</directory>
                                <filtering>true</filtering>
                                <includes><!--只對yml文件進行替換-->
                                    <include>*.yml</include>
                                </includes>
                            </resource>
                            <resource>
                                <directory>src/main/resources/</directory>
                                <filtering>false</filtering>
                            </resource>
                        </resources>
                        <outputDirectory>${project.build.directory}/conf</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- springboot maven打包-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

assembly.xml

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>package</id>
    <formats>
        <format>zip</format>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <excludes>
                <exclude>
                    ${groupId}:${artifactId}
                </exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>bin</directory>
            <outputDirectory>/bin</outputDirectory>
            <fileMode>777</fileMode>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/conf</directory>
            <outputDirectory>/conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/classes</outputDirectory>
            <includes>
                <include>**/*.class</include>
                <include>META-INF/*</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

pom文件中resources插件配置了2個execution,一個是正常往classes中寫配置文件的execution,一個是往conf寫配置文件的execution。這樣作的好處是不影響maven自己的打包邏輯。若是再配置一個springboot的打包插件,也能夠正常打包,執行。

執行

原來打包成jar後,只要一句java -jar xxx.jar就能夠啓動項目。如今爲多個文件夾的狀況下,就要手動指定環境,經過java -classpath XXX xxx.xxx.MainClass來啓動項目,因此寫了啓動腳本。

run.sh

#!/bin/bash 

#Java程序所在的目錄(classes的上一級目錄) 
APP_HOME=..

#須要啓動的Java主程序(main方法類) 
APP_MAIN_CLASS="io.github.loanon.springboot.MainApplication"

#拼湊完整的classpath參數,包括指定lib目錄下全部的jar 
CLASSPATH="$APP_HOME/conf:$APP_HOME/lib/*:$APP_HOME/classes"

s_pid=0
checkPid() {
   java_ps=`jps -l | grep $APP_MAIN_CLASS`
   if [ -n "$java_ps" ]; then
      s_pid=`echo $java_ps | awk '{print $1}'`
   else 
      s_pid=0
   fi 
} 

start() { 
checkPid
if [ $s_pid -ne 0 ]; then
    echo "================================================================"
    echo "warn: $APP_MAIN_CLASS already started! (pid=$s_pid)"
    echo "================================================================"
else
    echo -n "Starting $APP_MAIN_CLASS ..."
    nohup java -classpath $CLASSPATH $APP_MAIN_CLASS >./st.out 2>&1 &
    checkPid
    if [ $s_pid -ne 0 ]; then
        echo "(pid=$s_pid) [OK]"
    else
        echo "[Failed]"
    fi
fi 
}

echo "start project......"
start

run.cmd

@echo off
set APP_HOME=..
set CLASS_PATH=%APP_HOME%/lib/*;%APP_HOME%/classes;%APP_HOME%/conf;
set APP_MAIN_CLASS=io.github.loanon.springboot.MainApplication
java -classpath %CLASS_PATH% %APP_MAIN_CLASS%

這樣就能夠啓動項目了。

中止

linux下中止tomcat通常怎麼作?固然是經過運行shutdown.sh。這樣作有什麼好處呢?能夠優雅停機。何爲優雅停機?簡單點說就是讓代碼把作了一半工做的作完,還沒作的(新的任務,請求)就不要作了,而後停機。

由於作的是定時任務處理數據的功能。試想下若是一個任務作了一半,我給停了,這個任務處理的數據被我標記了在處理中,下次重啓後,就再也不處理,那麼這些數據就一直不會再被處理。因此須要像tomcat同樣能優雅停機。

網上查詢springboot優雅停機相關資料。主要是使用spring-boot-starter-actuator,不過不少人說這個在1.X的springboot中能夠用,springboot 2.X不能用,須要本身寫相關代碼來支持,親測springboot 2.0.4.RELEASE能夠用。pom文件中引入相關依賴。

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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>io.github.loanon</groupId>
    <artifactId>spring-boot-zip</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <encoding>UTF-8</encoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <!-- springboot監控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--springboot自定義配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure-processor</artifactId>
        </dependency>

        <!--定時任務-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>
        <!--發送http請求 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>compile-resources</id>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                        <configuration>
                            <encoding>utf-8</encoding>
                            <useDefaultDelimiters>true</useDefaultDelimiters>
                            <resources>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <filtering>true</filtering>
                                    <includes><!--只對yml文件進行替換-->
                                        <include>*.yml</include>
                                    </includes>
                                </resource>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-resources</id>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                        <configuration>
                            <encoding>utf-8</encoding>
                            <useDefaultDelimiters>true</useDefaultDelimiters>
                            <resources>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <filtering>true</filtering>
                                    <includes><!--只對yml文件進行替換-->
                                        <include>*.yml</include>
                                    </includes>
                                </resource>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/conf</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- springboot maven打包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

在application.yml中配置一下

application.yml

management: #開啓監控管理,優雅停機
  server:
    ssl:
      enabled: false
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
    shutdown:
      enabled: true #啓用shutdown端點

啓動項目,能夠經過POST方式訪問/actuator/shutdown讓項目停機。

實際線上可能沒辦法方便的發送POST請求,因此寫個類處理下

Shutdown.java

package io.github.loanon.springboot;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

/**
 * 應用關閉入口
 * @author dingzg
 */
public class Shutdown {
    public static void main(String[] args) {
        String url = null;
        if (args.length > 0) {
            url = args[0];
        } else {
            return;
        }
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        try {
            httpClient.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

只要將啓動腳本中的啓動類改爲Shutdown類,並指定請求的地址便可。

stop.sh

#!/bin/bash

#Java程序所在的目錄(classes的上一級目錄)
APP_HOME=..

#須要啓動的Java主程序(main方法類)
APP_MAIN_CLASS="io.github.loanon.springboot.MainApplication"
SHUTDOWN_CLASS="io.github.loanon.springboot.Shutdown"

#拼湊完整的classpath參數,包括指定lib目錄下全部的jar
CLASSPATH="$APP_HOME/conf:$APP_HOME/lib/*:$APP_HOME/classes"

ARGS="http://127.0.0.1:8080/actuator/shutdown"

s_pid=0
checkPid() {
   java_ps=`jps -l | grep $APP_MAIN_CLASS`
   if [ -n "$java_ps" ]; then
      s_pid=`echo $java_ps | awk '{print $1}'`
   else
      s_pid=0
   fi
}

stop() {
checkPid
if [ $s_pid -ne 0 ]; then
    echo -n "Stopping $APP_MAIN_CLASS ...(pid=$s_pid) "
    nohup java -classpath $CLASSPATH $SHUTDOWN_CLASS $ARGS >./shutdown.out 2>&1 &
    if [ $? -eq 0 ]; then
       echo "[OK]"
    else
       echo "[Failed]"
    fi
    sleep 3
    checkPid
    if [ $s_pid -ne 0 ]; then
       stop
    else
       echo "$APP_MAIN_CLASS Stopped"
    fi
else
    echo "================================================================"
    echo "warn: $APP_MAIN_CLASS is not running"
    echo "================================================================"
fi
}

echo "stop project......"
stop

stop.cmd

@echo off
set APP_HOME=..
set CLASS_PATH=%APP_HOME%/lib/*;%APP_HOME%/classes;%APP_HOME%/conf;
set SHUTDOWN_CLASS=io.github.loanon.springboot.Shutdown
set ARGS=http://127.0.0.1:8080/actuator/shutdown
java -classpath %CLASS_PATH% %SHUTDOWN_CLASS% %ARGS%

這樣就能夠經過腳原本啓停項目。

其餘

關於停機這塊仍是有缺點,主要是安全性。若是不加校驗均可以訪問接口,別人也就能夠隨便讓咱們的項目停機,實際操做過程當中我是經過將web地址綁定到127.0.0.1這個地址上,不容許遠程訪問。固然也可添加spring-security作嚴格的權限控制,主要項目中沒有用到web功能,只是spring-quartz的定時任務功能,因此就將地址綁定到本地才能訪問。並且項目自己也是在內網運行,基本能夠保證安全。

相關文章
相關標籤/搜索