简介
springboot项目需要在windows上部署,spring官方推荐使用winsw来将springboot项目作为服务运行,参考
https://docs.spring.io/spring-boot/docs/2.0.8.RELEASE/reference/htmlsingle/#deployment-windows
winsw下载及文档:https://github.com/kohsuke/winsw
安装使用
winsw的使用比较简单。从github上下载:winsw下载,要下载的文件有两个:1.winsw.exe程序;2.xml配置文件。可以下载最新版本的WinSW.NET4.exe和sample-minimal.xml。下载完成后,将下载的两个文件及springboot项目的jar包放在同一个文件夹中。
下载winsw后,检查window上是否安装 .net framework4 ,如果没装后面会出问题。关于.net framework4的安装请自行搜索。
SpringBoot项目通过执行mvn clean package命令后得到可执行jar包:Milan_ZKT-0.0.1-SNAPSHOT.jar
需要将winsw执行程序跟xml改成同样的名字,推荐使用项目名+Service的命名方式,比如:WinSW.NET4.exe改成Milan_ZKT_Service.exe,sample-minmal.xml改成Milan_ZKT_Service.xml。
改名完成后,编辑Milan_ZKT_Service.xml文件,配置如下
<configuration>
<!-- ID of the service. It should be unique accross the Windows system-->
<id>Milan_ZKT</id>
<!-- Display name of the service -->
<name>Milan_ZKT Service (powered by WinSW)</name>
<!-- Service description -->
<description>This service is a service cratead from ...</description>
<!-- Path to the executable, which should be started -->
<executable>C:\Program Files\Java\jre1.8.0_191\bin\java.exe</executable>
<arguments>-Xmx256m -jar Milan_ZKT.jar --spring.profiles.active=test</arguments>
<!--日志模式 Throw away stdout and stderr, and do not produce any log files at all.-->
<log mode="none"/>
<!--延迟启动-->
<delayedAutoStart/>
<!--停止 -->
<stopexecutable>D:\Milan_ZKT\CURL.EXE</stopexecutable>
<stoparguments>-X POST -u username:password http://127.0.0.1:8088/actuator/shutdown</stoparguments>
<onfailure action="restart" delay="120 sec"/>
<onfailure action="restart" delay="240 sec"/>
</configuration>
xml配置参考:https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.md
配置完成后,命令行进入winsw所在的文件夹,执行“Milan_ZKT_Service.exe install”,其中Milan_ZKT_Service是你修改后的名称。注意:命令提示符界面要用管理员权限进入,否则安装服务会失败,提示“WMI Operation failure: AccessDenied”
Milan_ZKT_Service.exe install
进入服务界面,可以看到Milan_ZKT_Service服务已经生成了:
命令提示符界面输入命令“net start Milan_ZKT_Service”启动服务。
winsw优雅停止springboot进程
通过winsw配置文档,知道winsw是通过WindowsAPI中TerminateProcess函数可以用来终止或者说杀死进程,它不会留给进程及其所有线程清理的时间,系统会马上终止(杀死)这个进程的所有线程,致使进程终止。这就导致我们不能优雅终止springboot服务,这在生产环境很有可能产生不可预知问题。
springboot通过 Actuator 的 HTTP Endpoint可以方便地对应用的监控与管理,可以通过如下命令结束springboot服务
curl -X POST -u username:password http://127.0.0.1:8088/actuator/shutdown
此命令包含了一个curl工具,我们需要下载一个curl.exe
在Milan_ZKT_Service.xml配置文件中增加winsw停止参数,配置如下
<stopexecutable>D:\Milan_ZKT\CURL.EXE</stopexecutable>
<stoparguments>-X POST -u username:password http://127.0.0.1:8088/actuator/shutdown</stoparguments>
winsw会在服务停止的时候执行如上参数,执行的过程中会增加一个进程,这个进程通过web请求让springboot进程停止,然后进程结束,springboot进程也结束之后,winsw服务就完全停止了。
详细配置请参考:Spring Boot 2.X优雅停止
删除服务
删除服务分为两步:1停止服务;2删除服务,都是在命令行界面实现。
输入“net stop Milan_ZKT_Service”停止运行服务。
输入“Milan_ZKT_Service.exe uninstall”删除服务。
最后
上面所有的命令都可以写在批处理文件中,部署的时候就可以实现一键部署了。
将命令写在批处理文件中,希望将批处理文件默认为管理员权限打开,可以在批处理文件的开头写上:
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
net stop Milan_ZKT
net start Milan_ZKT