springboot打包成zip部署,并实现优雅停机
springboot打包成zip部署,并实现优雅停机
更新:本⽂重点是springboot打包成zip(),关于启停应⽤可以看《》和《》
众所周知springboot项⽬,使⽤springboot插件打包的话,会打包成⼀个包含依赖的可执⾏jar,⾮常⽅便。只要有java运⾏环境的电脑上,运⾏java -jar xxx.jar就可以直接运⾏项⽬。
但是这样的缺点也很明显,如果我要改个配置,要将jar包中的配置⽂件取出来,修改完再放回去。这样做在windows下还⽐较容易。如果在linux上⾯就很费劲了。
另外如果代码中需要读取⼀些⽂件(⽐如说⼀张图⽚),也被打进jar中,就没办法像在磁盘中时⼀句File file = new File(path)代码就可以读取了。(当然这个可以使⽤spring的ClassPathResource来解决)。
还有很多公司项⽬上线后,都是增量发布,这样如果只有⼀个jar 的话,增量发布也是很⿇烦的事情。虽然我是很讨厌这种增量发布的⽅式,因为会造成线上⽣产环境和开发环境有很多不⼀致的地⽅,这样在问题的时候会⾛很多弯路。很不幸我现在在的项⽬也是这样的情况,⽽且最近接的任务就是⽤springboot搭建⼀个定时任务服务,为了维护⽅便,最后决定将项⽬打包成zip进⾏部署。
⽹上到了很多springboot打包成zip的⽂章,不过基本都是将依赖从springboot的jar中拿出来放到lib⽬录中,再将项⽬的jar包中META-INF 中指定lib到classpath中。这样做还是会有上⾯的问题。
最后我决定⾃⼰通过maven-assembly-plugin来实现这个功能。
打包
⾸先maven-assembly-plugin是将项⽬打包的⼀个插件。可以通过指定配置⽂件来决定打包的具体要求。
我的想法是将class打包到classes中,配置⽂件打包到conf中,项⽬依赖打包到lib中,当然还有⾃⼰编写的启动脚本在bin⽬录中。
如图
maven的target/classes下就是项⽬编译好的代码和配置⽂件。原来的做法是在l中配置筛选,将该⽬录下class⽂件打包进classes中,除class⽂件打包到conf中(bin⽬录⽂件打包进bin⽬录,项⽬依赖打包进lib⽬录)。结果发现conf⽬录下会有空⽂件夹(java包路径)。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>l</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<assembly
xmlns="/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/plugins/maven-assembly-plugin/assembly/1.1.2 /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⽬
录中的情况。
<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>l</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<assembly
xmlns="/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/plugins/maven-assembly-plugin/assembly/1.1.2 /xsd/assembly-1.1.2.xsd">
<id>package</id>
<formats>
<format>zip</format>
<format&</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>maven打包本地jar包
<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
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
@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⽂件中引⼊相关依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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>
&ding>UTF-8</ding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>        <mavenpiler.source>${java.version}</mavenpiler.source>
<mavenpiler.target>${java.version}</mavenpiler.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>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。