在windows服务器上使用winsw部署spring boot项目

简介

springboot项目须要在windows上部署,spring官方推荐使用winsw来将springboot项目做为服务运行,参考html

https://docs.spring.io/spring-boot/docs/2.0.8.RELEASE/reference/htmlsingle/#deployment-windowsjava

winsw下载及文档:https://github.com/kohsuke/winswgit

安装使用

winsw的使用比较简单。从github上下载:winsw下载,要下载的文件有两个:1.winsw.exe程序;2.xml配置文件。能够下载最新版本的WinSW.NET4.exe和sample-minimal.xml。下载完成后,将下载的两个文件及springboot项目的jar包放在同一个文件夹中。
github

下载winsw后,检查window上是否安装 .net framework4 ,若是没装后面会出问题。关于.net framework4的安装请自行搜索。web

SpringBoot项目经过执行mvn clean package命令后获得可执行jar包:Milan_ZKT-0.0.1-SNAPSHOT.jarspring

须要将winsw执行程序跟xml改为一样的名字,推荐使用项目名+Service的命名方式,好比:WinSW.NET4.exe改为Milan_ZKT_Service.exe,sample-minmal.xml改为Milan_ZKT_Service.xml。windows

更名完成后,编辑Milan_ZKT_Service.xml文件,配置以下springboot

<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.mdbash

配置完成后,命令行进入winsw所在的文件夹,执行“Milan_ZKT_Service.exe install”,其中Milan_ZKT_Service是你修改后的名称。注意:命令提示符界面要用管理员权限进入,不然安装服务会失败,提示“WMI Operation failure: AccessDenied”curl

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
相关文章
相关标签/搜索