轻量级Spring Boot & Spring Cloud项目自动部署Maven插件

    技术2023-12-14  106

    介绍

    deploy-helper是一個Spring Boot & Spring Cloud项目自动远程部署轻量级Maven插件.

    Gitee:https://gitee.com/dt_research_institute/deploy-helper

    通常情况下,我们将开发好的Spring Boot程序部署到服务器上时,会经历以下流程:

    1、maven项目打包

    2、通过sftp、scp软件将jar包拷贝到目标服务器上

    3、通过ps命令查找pid进程号,使用kill命令杀进程

    4、通过java命令启动jar包

    如果需求更改后,我们需要重复以上流程,对于开发者来说,是一件很无聊的事情,特别是需要发布到测试服务器等进行测试的情况下,非常耗时间。

    deploy-helper解决的就是步骤2、步骤3、步骤4,项目打包好后,执行插件完成自动化部署,整个过程耗时10s左右

    软件架构

    软件架构说明

    安装教程

    在maven項目的pom.xml中直接引入

    <plugin> <groupId>com.github.xiaoymin</groupId> <artifactId>deploy-helper-maven-plugin</artifactId> <!--请在maven中央仓库搜索最新版本--> <version>1.0</version> <configuration> <!--配置文件路径--> <configurationFile>src/main/resources/deployHelperConfig.xml</configurationFile> </configuration> </plugin>

    deployHelperConfig.xml配置文件在项目的src/main/resources目录下时,可以不用在pom.xml里面进行配置

    引入后,在项目中可以看到我们引入的插件

    使用说明

    ### 配置文件

    deploy-helper在执行时,需要传递一个xml文件作为初始化文件,xml代表了所需要进行自动化部署的配置信息,XML模板如下:

    <?xml version="1.0" encoding="UTF-8"?> <deployHelperConfiguration> <!--指定deployHelper自动上传时所连接的服务器以及要使用的deploy配置--> <profile server="dev" deploy="dev"></profile> <servers> <!--可以配置多个server节点,代表的是目标服务器--> <server profile="dev" host="192.168.0.223" port="22" username="root" password="123456"></server> </servers> <deploys> <!--目标服务器存在启动&停止脚本--> <deploy profile="dev" source="target/demo-http-1.0.jar" target="/home/xiaoymin/test/test1/demo-http-1.0.jar" processName="demo-http-1.0.jar" activeDefaultStart="true" startShell="/home/xiaoymin/test/test1/startup.sh" stopShell="/home/xiaoymin/test/test1/stop.sh"> </deploy> <!--不存在启动脚本,使用deploy-helper默认的脚本--> <deploy profile="test" source="target/demo-http-1.0.jar" target="/home/xiaoymin/test/test5/demo-http-1.0.jar" processName="demo-http-1.0.jar" activeDefaultStart="true"> </deploy> </deploys> </deployHelperConfiguration>

    在上面的XML模板中,主要包含三个节点

    profile:profile节点代表的是在插件执行时具体引用那个server和deployservers:Linux服务器节点配置,主要包含的属性:profile(名称)、host(主机号)、port(端口号)、username(登陆用户名)、password(登陆密码)deploy:该节点是最终做自动化部署时的配置,可以配置多个,在profile节点时可以自动指定,区分开发环境&生成环境,属性如下: profile:名称source:需要上传的jar文件,可以是相对目录下的文件,也可以使用绝对路径target:目标服务器路径,需要注意的时该属性必须配置完整的路径+文件名,不能只包含目录,上传是覆盖策略processName:该参数主要用于在服务上用于查询进程号的名称,越完整越好,通过该名称通过ps命令查找时可以精准定位到程序的pid,如果stopShell停止脚本存在的话则该参数可以不传activeDefaultStart:是否自动生成启动jar包的启动命令,该参数的优先级低于startShellstartShell:启动脚本,必须配置完整路径stopShell:停止脚本,必须配置完整路径

    脚本参考

    目标服务器上配置的启动脚本和停止脚本参考如下:

    启动脚本(startup.sh):

    nohup java -jar demo-http-1.0.jar &

    停止脚本(stop.sh):

    #!/bin/bash echo "Stop Procedure : demo-http-1.0.jar" pid=`ps -ef |grep java|grep demo-http-1.0.jar|awk '{print $2}'` echo 'old Procedure pid:'$pid if [ -n "$pid" ] then kill $pid fi

    使用场景

    目前使用deploy-helper插件主要区分2种场景

    目标服务器存在启动&停止脚本

    我们可以通过在部署服务器上线创建2个脚本,分别是启动脚本和停止脚本,此时,我们需要在deployHelperConfig.xml配置文件中配置deploy节点,示例如下:

    <deploy profile="dev" source="target/demo-http-1.0.jar" target="/home/xiaoymin/test/test1/demo-http-1.0.jar" startShell="/home/xiaoymin/test/test1/startup.sh" stopShell="/home/xiaoymin/test/test1/stop.sh"> </deploy>

    通过上面的配置文件,我们可以知道

    部署目录:/home/xiaoymin/test/test1,如果该目录不存在,则会自动创建该目录

    启动脚本:/home/xiaoymin/test/test1/startup.sh

    停止脚本:/home/xiaoymin/test/test1/stop.sh

    需要注意的是,如果使用这种场景,那么目标服务器的启动脚本和停止脚本文件必须存在,并且必须是可执行的,需要有Linux的x权限

    使用deploy-helper提供的默认脚本

    如果你嫌弃写shell脚本麻烦,那么deploy-helper默认提供了启动和停止的脚本,此时,配置deploy的节点时如下:

    <deploy profile="test" source="target/demo-http-1.0.jar" target="/home/xiaoymin/test/test5/demo-http-1.0.jar" processName="demo-http-1.0.jar" activeDefaultStart="true"> </deploy>

    通过上面的配置:

    部署目录:/home/xiaoymin/test/test5/,如果该目录不存在,则会自动创建该目录

    进程名称:processName在这种模式下必须设置该参数,并且最好是通过ps命令能查找到pid进程号的,所以需要提供完整的名称最好

    启动自动启动:activeDefaultStart命令优先级低于startShell,如果startShell启动命令不为空,程序会使用startShell中的命令进行启动程序,否则activeDefaultStart参数必须设置为true,此时,使用deploy-helper默认会生成jar项目的启动命令进行启动

    Processed: 0.023, SQL: 9