iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >使用springboot打包成zip部署,并实现优雅停机
  • 453
分享到

使用springboot打包成zip部署,并实现优雅停机

2024-04-02 19:04:59 453人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

众所周知SpringBoot项目,使用springboot插件打包的话,会打包成一个包含依赖的可执行jar,非常方便。只要有java运行环境的电脑上,运行java -jar xxx.

众所周知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下就是项目编译好的代码和配置文件。原来的做法是在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</include>
                <include>META-INF*.class</include>
                <include>META-INF
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的定时任务功能,所以就将地址绑定到本地才能访问。而且项目本身也是在内网运行,基本可以保证安全。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 使用springboot打包成zip部署,并实现优雅停机

本文链接: https://www.lsjlt.com/news/132908.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作