使用springboot打包成zip部署,并實(shí)現(xiàn)優(yōu)雅停機(jī)
眾所周知springboot項(xiàng)目,使用springboot插件打包的話,會(huì)打包成一個(gè)包含依賴的可執(zhí)行jar,非常方便。只要有java運(yùn)行環(huán)境的電腦上,運(yùn)行java -jar xxx.jar就可以直接運(yùn)行項(xiàng)目。
但是這樣的缺點(diǎn)也很明顯,如果我要改個(gè)配置,要將jar包中的配置文件取出來,修改完再放回去。這樣做在windows下還比較容易。如果在linux上面就很費(fèi)勁了。
另外如果代碼中需要讀取一些文件(比如說一張圖片),也被打進(jìn)jar中,就沒辦法像在磁盤中時(shí)一句File file = new File(path)代碼就可以讀取了。(當(dāng)然這個(gè)可以使用spring的ClassPathResource來解決)。
還有很多公司項(xiàng)目上線后,都是增量發(fā)布,這樣如果只有一個(gè)jar 的話,增量發(fā)布也是很麻煩的事情。雖然我是很討厭這種增量發(fā)布的方式,因?yàn)闀?huì)造成線上生產(chǎn)環(huán)境和開發(fā)環(huán)境有很多不一致的地方,這樣在找問題的時(shí)候會(huì)走很多彎路。很不幸我現(xiàn)在在的項(xiàng)目也是這樣的情況,而且最近接的任務(wù)就是用springboot搭建一個(gè)定時(shí)任務(wù)服務(wù),為了維護(hù)方便,最后決定將項(xiàng)目打包成zip進(jìn)行部署。
網(wǎng)上找到了很多springboot打包成zip的文章,不過基本都是將依賴從springboot的jar中拿出來放到lib目錄中,再將項(xiàng)目的jar包中META-INF中指定lib到classpath中。這樣做還是會(huì)有上面的問題。
最后我決定自己通過maven-assembly-plugin來實(shí)現(xiàn)這個(gè)功能。
打包
首先maven-assembly-plugin是將項(xiàng)目打包的一個(gè)插件??梢酝ㄟ^指定配置文件來決定打包的具體要求。
我的想法是將class打包到classes中,配置文件打包到conf中,項(xiàng)目依賴打包到lib中,當(dāng)然還有自己編寫的啟動(dòng)腳本在bin目錄中。
如圖

maven的target/classes下就是項(xiàng)目編譯好的代碼和配置文件。原來的做法是在assembly.xml中配置篩選,將該目錄下class文件打包進(jìn)classes中,除class文件打包到conf中(bin目錄文件打包進(jìn)bin目錄,項(xiàng)目依賴打包進(jìn)lib目錄)。結(jié)果發(fā)現(xiàn)conf目錄下會(huì)有空文件夾(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>
其實(shí)這樣是不影響項(xiàng)目運(yùn)行的,但是我看著很難受,嘗試了很多方法去修改配置來達(dá)到不打包空文件夾的效果。但是都沒成功。
然后我換了個(gè)方式,通過maven-resources-plugin插件將配置文件在編譯的時(shí)候就復(fù)制一份到target/conf目錄下,打包的時(shí)候配置文件從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><!--只對(duì)yml文件進(jìn)行替換-->
<include>*.yml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources/</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>-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><!--只對(duì)yml文件進(jìn)行替換-->
<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個(gè)execution,一個(gè)是正常往classes中寫配置文件的execution,一個(gè)是往conf寫配置文件的execution。這樣做的好處是不影響maven本身的打包邏輯。如果再配置一個(gè)springboot的打包插件,也可以正常打包,執(zhí)行。
執(zhí)行
原來打包成jar后,只要一句java -jar xxx.jar就可以啟動(dòng)項(xiàng)目?,F(xiàn)在為多個(gè)文件夾的情況下,就要手動(dòng)指定環(huán)境,通過java -classpath XXX xxx.xxx.MainClass來啟動(dòng)項(xiàng)目,所以寫了啟動(dòng)腳本。
run.sh
#!/bin/bash
#Java程序所在的目錄(classes的上一級(jí)目錄)
APP_HOME=..
#需要啟動(dòng)的Java主程序(main方法類)
APP_MAIN_CLASS="io.github.loanon.springboot.MainApplication"
#拼湊完整的classpath參數(shù),包括指定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%
這樣就可以啟動(dòng)項(xiàng)目了。
停止
linux下停止tomcat一般怎么做?當(dāng)然是通過運(yùn)行shutdown.sh。這樣做有什么好處呢?可以優(yōu)雅停機(jī)。何為優(yōu)雅停機(jī)?簡單點(diǎn)說就是讓代碼把做了一半工作的做完,還沒做的(新的任務(wù),請(qǐng)求)就不要做了,然后停機(jī)。
因?yàn)樽龅氖嵌〞r(shí)任務(wù)處理數(shù)據(jù)的功能。試想下如果一個(gè)任務(wù)做了一半,我給停了,這個(gè)任務(wù)處理的數(shù)據(jù)被我標(biāo)記了在處理中,下次重啟后,就不再處理,那么這些數(shù)據(jù)就一直不會(huì)再被處理。所以需要像tomcat一樣能優(yōu)雅停機(jī)。
網(wǎng)上查詢springboot優(yōu)雅停機(jī)相關(guān)資料。主要是使用spring-boot-starter-actuator,不過很多人說這個(gè)在1.X的springboot中可以用,springboot 2.X不能用,需要自己寫相關(guān)代碼來支持,親測(cè)springboot 2.0.4.RELEASE可以用。pom文件中引入相關(guān)依賴。
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監(jiān)控 -->
<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>
<!--定時(shí)任務(wù)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<!--發(fā)送http請(qǐng)求 -->
<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><!--只對(duì)yml文件進(jìn)行替換-->
<include>*.yml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources/</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>-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><!--只對(duì)yml文件進(jìn)行替換-->
<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: #開啟監(jiān)控管理,優(yōu)雅停機(jī)
server:
ssl:
enabled: false
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
shutdown:
enabled: true #啟用shutdown端點(diǎn)
啟動(dòng)項(xiàng)目,可以通過POST方式訪問/actuator/shutdown讓項(xiàng)目停機(jī)。
實(shí)際線上可能沒辦法方便的發(fā)送POST請(qǐng)求,所以寫個(gè)類處理下
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;
/**
* 應(yīng)用關(guān)閉入口
* @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();
}
}
}
只要將啟動(dòng)腳本中的啟動(dòng)類改成Shutdown類,并指定請(qǐng)求的地址即可。
stop.sh
#!/bin/bash
#Java程序所在的目錄(classes的上一級(jí)目錄)
APP_HOME=..
#需要啟動(dòng)的Java主程序(main方法類)
APP_MAIN_CLASS="io.github.loanon.springboot.MainApplication"
SHUTDOWN_CLASS="io.github.loanon.springboot.Shutdown"
#拼湊完整的classpath參數(shù),包括指定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%
這樣就可以通過腳本來啟停項(xiàng)目。
其他
關(guān)于停機(jī)這塊還是有缺點(diǎn),主要是安全性。如果不加校驗(yàn)都可以訪問接口,別人也就可以隨便讓我們的項(xiàng)目停機(jī),實(shí)際操作過程中我是通過將web地址綁定到127.0.0.1這個(gè)地址上,不允許遠(yuǎn)程訪問。當(dāng)然也可添加spring-security做嚴(yán)格的權(quán)限控制,主要項(xiàng)目中沒有用到web功能,只是spring-quartz的定時(shí)任務(wù)功能,所以就將地址綁定到本地才能訪問。而且項(xiàng)目本身也是在內(nèi)網(wǎng)運(yùn)行,基本可以保證安全。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot緩存實(shí)戰(zhàn) Caffeine示例
本篇文章主要介紹了Spring Boot緩存實(shí)戰(zhàn) Caffeine示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
詳解Java如何進(jìn)行Base64的編碼(Encode)與解碼(Decode)
這篇文章主要介紹了詳解Java如何進(jìn)行Base64的編碼(Encode)與解碼(Decode),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
初始JAVA模塊化開發(fā)的超詳細(xì)步驟(適合菜鳥)
這篇文章主要介紹了初始JAVA模塊化開發(fā)的超詳細(xì)步驟,詳細(xì)解釋了模塊描述符的職責(zé)、模塊路徑的概念以及如何使用命令行運(yùn)行模塊化Java程序,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
深入淺析Spring 的aop實(shí)現(xiàn)原理
AOP(Aspect-OrientedProgramming,面向方面編程),可以說是OOP(Object-Oriented Programing,面向?qū)ο缶幊蹋┑难a(bǔ)充和完善。本文給大家介紹Spring 的aop實(shí)現(xiàn)原理,感興趣的朋友一起學(xué)習(xí)吧2016-03-03
java.lang.ExceptionInInitializerError初始化程序中的異常錯(cuò)誤的解決
java.lang.ExceptionInInitializerError?異常在?Java?中表示一個(gè)錯(cuò)誤,該錯(cuò)誤發(fā)生在嘗試初始化一個(gè)類的靜態(tài)變量、靜態(tài)代碼塊或枚舉常量時(shí),本文就來介紹并解決一下,感興趣的可以了解一下2024-05-05

