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

背景:

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

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

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

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

解决方案(Windows):

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

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

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

      1、通过 https://github.com/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=http://config.powerbridge.com: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 start statement-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) 2018 Microsoft Corporation。保留所有权利。

E:\winsw>pbplatform-registry-1.0-SNAPSHOT.exe install
2019-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 exists
To install the service, delete the existing one or change service Id in the configuration file
2019-03-28 17:02:14,054 FATAL - Unhandled exception
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)
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服务的进程级别管理项目,借助它可以完全消灭上面遇到的所有问题。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值