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的定时任务功能,因此就将地址绑定到本地才能访问。并且项目自己也是在内网运行,基本能够保证安全。

相关文章
相关标签/搜索