在开发运维中,比如现在比较流行的Spring-Boot
这种微服务,在Windows上面实现打包运行啥的有个控制台窗口,如果不注意关了服务就GG了,这里整理一些相关的Windows上注册Java的Jar为服务的方法等。
1、WinSW实现注册Jar包服务
下载地址:https://github.com/winsw/winsw/releases
我这里下载的是WinSW.NET4.exe
,看个人的需求吧!
-
准备相关的文件信息
创建一个空文件夹,放入下载的WinSW.NET4.exe
、打包的Jar包,创建一个WinSW.NET4.xml
文件,文件内容如下(注意,文件名称必须和WinSW.NET4.exe
名称一致,可自定义):<service> <id>Demo-admin</id> <name>Demo-admin</name> <description>一个测试服务 Demo-admin</description> <!-- java环境变量 --> <!-- 配置了环境变量 写java 否则写jdk绝对路径--> <executable>java</executable> <!-- 可以绝对路径 可以相对路径--> <arguments>-jar Demo-admin-1.0.0.jar</arguments> <!-- 开机启动 --> <startmode>Automatic</startmode> <!-- 日志配置 --> <logpath>%BASE%\log</logpath> <logmode>rotate</logmode> </service>
-
相关文件信息如下:
-
创建服务信息
打开CMD
命令窗口,输入如下命令即可:WinSW.NET4.exe install # 得到如下信息: 2020-10-19 17:59:55,784 INFO - Installing the service with id 'Demo-admin'
貌似win10系统有个确认框什么的,点击确认就行了,
打开Windows服务窗口,发现服务注册成功了,而且是能自启的,如下:
好像有些博客说的是还要创建 创建服务install.bat
和 卸载服务uninstall.bat
这种批处理文件啥的,好像最新的WinSW
不需要创建了,应该能减少一些错误情况吧
这种方法还是蛮简单的,亲测可用
WinSW
相关的命令语句:
地址: https://github.com/winsw/winsw/blob/v3/docs/cli-commands.md#install-command
install install the service to Windows Service Controller
uninstall uninstall the service
start start the service (must be installed before)
stop stop the service
stopwait stop the service and wait until it's actually stopped
restart restart the service
restart! self-restart (can be called from child processes)
status check the current status of the service
test check if the service can be started and then stopped
testwait starts the service and waits until a key is pressed then stops the service
version print the version info
2、wrapper-windows
这种方式我试了一下,不知道哪儿配置错了,好像有点问题,这里提供一个下载地址吧!
下载地址:https://wrapper.tanukisoftware.com/doc/english/download.jsp
注意的是这个软件有好几个版本,下载框了红框的社区版是不需要License
啥的
相对WinSW
有点繁琐,但是它支持好几种平台,功能相对强悍些