失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 基于winSW开源工具将springBoot 项目部署为一个window服务

基于winSW开源工具将springBoot 项目部署为一个window服务

时间:2023-03-10 12:53:23

相关推荐

基于winSW开源工具将springBoot 项目部署为一个window服务

背景:

springboot 项目可以直接打包成一个个的jar包,我们可以简单的通过-jar 来运行它。那么我们会或多或少的遇到如下问题

问题一:怎么批量部署jar服务?

问题二:在windows 环境下如何区分一个个的java 服务?

问题三:在windows中能不能做到开机启动了?

解决方案(Windows):

针对问题一,我们可以考虑通过bat来做,

针对问题二,只能通过对jar进行改造,指定其进程名,这不是一种优雅的方案(侵入式的)。

针对问题三,官方推荐一款小工具winsw。下面我主要记录它的用法。

1、通过/kohsuke/winsw/releases下载

我这里下载的是2.1.2。我们需要下载截图中的2个文件

2、新建一个磁盘目录winsw(任意的)

3、将下载的2个文件连同需要服务化的jar包都放到该目录(注意:要通过重命名保持.exe,.jar,.xml这三个文件名称相同,下面截图中的log文件是执行后的文件,忽略即可)

4、修改.xml文件

<configuration><!-- ID of the service. It should be unique accross the Windows system--><id>pbplatform-registry-1.0-SNAPSHOT</id><!-- Display name of the service --><name>pbplatform-registry-1.0-SNAPSHOT (powered by WinSW)</name><!-- Service description --><description>This service is a service cratead from a minimal configuration</description><!-- Path to the executable, which should be started --><executable>java</executable><arguments>-server -XX:+TieredCompilation -Xmn192m -XX:+AggressiveOpts -XX:+UseG1GC -XX:+UseStringDeduplication -Xss1m -XX:NewRatio=4 -XX:ParallelGCThreads=2 -XX:CMSFullGCsBeforeCompaction=5 -XX:+UseCMSCompactAtFullCollection -Denv=UAT -Duat_meta=:8183 -Duser.timezone=GMT+08 -Xms512m -Xmx512m -jar pbplatform-registry-1.0-SNAPSHOT.jar</arguments><logmode>rotate</logmode></configuration>

5,到此就完成了配置,可以安装并启动服务了

6,进入到三个文件的目标目录(比如target),执行命令:statement-0.0.1-SNAPSHOT.exe install,安装服务。

7,安装服务成功后,通过命令:net startstatement-0.0.1-SNAPSHOT(服务ID)启动服务。

当然,也可以通过在开始那里输入:services.msc命令直接进入服务界面,通过服务ID找到服务,然后手动启动。

8,通过命令:net stop statement-0.0.1-SNAPSHOT停止服务,或者是直接去服务界面手动停止服务。

9,停止服务后,可通过:sc delete ServiceName删除服务

10,查找windows service的PID:SC queryex service_name

11、遇到的问题:

Microsoft Windows [版本 10.0.17763.379](c) Microsoft Corporation。保留所有权利。E:\winsw>pbplatform-registry-1.0-SNAPSHOT.exe install-03-28 17:02:14,051 INFO - Installing the service with id 'pbplatform-registry-1.0-SNAPSHOT'Service with id 'pbplatform-registry-1.0-SNAPSHOT' already existsTo install the service, delete the existing one or change service Id in the configuration file-03-28 17:02:14,054 FATAL - Unhandled exceptionSystem.Exception: Installation failure: Service with id 'pbplatform-registry-1.0-SNAPSHOT' already exists在 winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)在 winsw.WrapperService.Main(String[] args)System.Exception: Installation failure: Service with id 'pbplatform-registry-1.0-SNAPSHOT' already exists在 winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)在 winsw.WrapperService.Main(String[] args)E:\winsw>

解决方案:

使用管理员权限启动cmd 后切换到该工作目录(winsw)

Linux :

通过supervisor 来进行多个jar服务的进程级别管理项目,借助它可以完全消灭上面遇到的所有问题。

如果觉得《基于winSW开源工具将springBoot 项目部署为一个window服务》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。