使用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包中的配置文件取出來(lái),修改完再放回去。這樣做在windows下還比較容易。如果在linux上面就很費(fèi)勁了。
另外如果代碼中需要讀取一些文件(比如說(shuō)一張圖片),也被打進(jìn)jar中,就沒(méi)辦法像在磁盤(pán)中時(shí)一句File file = new File(path)代碼就可以讀取了。(當(dāng)然這個(gè)可以使用spring的ClassPathResource來(lái)解決)。
還有很多公司項(xiàng)目上線后,都是增量發(fā)布,這樣如果只有一個(gè)jar 的話,增量發(fā)布也是很麻煩的事情。雖然我是很討厭這種增量發(fā)布的方式,因?yàn)闀?huì)造成線上生產(chǎn)環(huán)境和開(kāi)發(fā)環(huán)境有很多不一致的地方,這樣在找問(wèn)題的時(shí)候會(huì)走很多彎路。很不幸我現(xiàn)在在的項(xiàng)目也是這樣的情況,而且最近接的任務(wù)就是用springboot搭建一個(gè)定時(shí)任務(wù)服務(wù),為了維護(hù)方便,最后決定將項(xiàng)目打包成zip進(jìn)行部署。
網(wǎng)上找到了很多springboot打包成zip的文章,不過(guò)基本都是將依賴從springboot的jar中拿出來(lái)放到lib目錄中,再將項(xiàng)目的jar包中META-INF中指定lib到classpath中。這樣做還是會(huì)有上面的問(wèn)題。
最后我決定自己通過(guò)maven-assembly-plugin來(lái)實(shí)現(xiàn)這個(gè)功能。
打包
首先maven-assembly-plugin是將項(xiàng)目打包的一個(gè)插件。可以通過(guò)指定配置文件來(lái)決定打包的具體要求。
我的想法是將class打包到classes中,配置文件打包到conf中,項(xiàng)目依賴打包到lib中,當(dāng)然還有自己編寫(xiě)的啟動(dòng)腳本在bin目錄中。
如圖
maven的target/classes下就是項(xiàng)目編譯好的代碼和配置文件。原來(lái)的做法是在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)行的,但是我看著很難受,嘗試了很多方法去修改配置來(lái)達(dá)到不打包空文件夾的效果。但是都沒(méi)成功。
然后我換了個(gè)方式,通過(guò)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中寫(xiě)配置文件的execution,一個(gè)是往conf寫(xiě)配置文件的execution。這樣做的好處是不影響maven本身的打包邏輯。如果再配置一個(gè)springboot的打包插件,也可以正常打包,執(zhí)行。
執(zhí)行
原來(lái)打包成jar后,只要一句java -jar xxx.jar就可以啟動(dòng)項(xiàng)目?,F(xiàn)在為多個(gè)文件夾的情況下,就要手動(dòng)指定環(huán)境,通過(guò)java -classpath XXX xxx.xxx.MainClass來(lái)啟動(dòng)項(xiàng)目,所以寫(xiě)了啟動(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)然是通過(guò)運(yùn)行shutdown.sh。這樣做有什么好處呢?可以優(yōu)雅停機(jī)。何為優(yōu)雅停機(jī)?簡(jiǎn)單點(diǎn)說(shuō)就是讓代碼把做了一半工作的做完,還沒(méi)做的(新的任務(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,不過(guò)很多人說(shuō)這個(gè)在1.X的springboot中可以用,springboot 2.X不能用,需要自己寫(xiě)相關(guān)代碼來(lái)支持,親測(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: #開(kāi)啟監(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)目,可以通過(guò)POST方式訪問(wèn)/actuator/shutdown讓項(xiàng)目停機(jī)。
實(shí)際線上可能沒(méi)辦法方便的發(fā)送POST請(qǐng)求,所以寫(xiě)個(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%
這樣就可以通過(guò)腳本來(lái)啟停項(xiàng)目。
其他
關(guān)于停機(jī)這塊還是有缺點(diǎn),主要是安全性。如果不加校驗(yàn)都可以訪問(wèn)接口,別人也就可以隨便讓我們的項(xiàng)目停機(jī),實(shí)際操作過(guò)程中我是通過(guò)將web地址綁定到127.0.0.1這個(gè)地址上,不允許遠(yuǎn)程訪問(wèn)。當(dāng)然也可添加spring-security做嚴(yán)格的權(quán)限控制,主要項(xiàng)目中沒(méi)有用到web功能,只是spring-quartz的定時(shí)任務(wù)功能,所以就將地址綁定到本地才能訪問(wèn)。而且項(xiàng)目本身也是在內(nèi)網(wǎng)運(yùn)行,基本可以保證安全。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
REST架構(gòu)及RESTful應(yīng)用程序簡(jiǎn)介
這篇文章主要為大家介紹了REST架構(gòu)及RESTful的應(yīng)用程序簡(jiǎn)介,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03idea中mapper如何快速跳轉(zhuǎn)到xml插件
這篇文章主要介紹了idea中mapper如何快速跳轉(zhuǎn)到xml插件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05MyBatis整合Redis實(shí)現(xiàn)二級(jí)緩存的示例代碼
這篇文章主要介紹了MyBatis整合Redis實(shí)現(xiàn)二級(jí)緩存的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08詳解如何用spring Restdocs創(chuàng)建API文檔
這篇文章將帶你了解如何用spring官方推薦的restdoc去生成api文檔。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05java對(duì)象轉(zhuǎn)換String類型的三種方法
在很多情況下我們都需要將一個(gè)對(duì)象轉(zhuǎn)換為String類型。一般來(lái)說(shuō)有三種方法可以實(shí)現(xiàn):Object.toString()、(String)Object、String.valueOf(Object)。下面對(duì)這三種方法一一分析2013-11-11解決Feign切換client到okhttp無(wú)法生效的坑(出現(xiàn)原因說(shuō)明)
這篇文章主要介紹了解決Feign切換client到okhttp無(wú)法生效的坑(出現(xiàn)原因說(shuō)明),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02