詹学伟
詹学伟
Published on 2025-05-23 / 10 Visits
0
0

WinSW部署稳定Springboot程序

说明:公司给整了一台windows服务器,用于部署三个小项目,最开始我编写了一个简单的.bat启动脚本,但是后面发现这样启动的程序及其不稳定,每过段时间,程序就自动挂了(进程显示正常,但是不提供服务了)。于是就使用今天的WinSW。

其实我一直拒绝在windows系统上部署项目,因为我知道windows确实不稳定,但是领导之意要这样。很多东西经历过的人才知道,只是他没有经历过而已,内心很郁闷。

吐槽***********

一、下载WinSW

https://github.com/winsw/winsw/releases

下载对应的.exe文件(如WinSW-x64.exe)。

二、为每个服务创建独立的 WinSW 配置

目录结构

D:\app\gcmp\java\script\services
│
├── cost-gpms\
│   ├── cost-gpms.exe      # WinSW重命名后的可执行文件
│   ├── cost-gpms.xml      # 服务配置
│   └── logs\             # 日志目录(可选)
│
├── cost-ods-fm\
│   ├── cost-ods-fm.exe
│   ├── cost-ods-fm.xml
│   └── logs\
│
├── cost-ods-sync\
│   ├── cost-ods-sync.exe
│   ├── cost-ods-sync.xml
│   └── logs\
│
└── winsw-common.xml      # 公共配置模板(可选)

三、配置每个服务的 XML 文件

<service>
  <id>cost-gpms</id>
  <name>CostGpms</name>
  <description>cost gpms app</description>
  <executable>java</executable>
  <arguments>-jar "D:\app\gcmp\java\script\cost-gpms-1.0.0.jar" --server.port=8086</arguments>
  <logpath>D:\app\gcmp\java\script\services\cost-gpms\logs</logpath>
  <logmode>rotate</logmode>
  <env name="JAVA_OPTS" value="xxxx"/>
</service>

<service>
  <id>cost-ods-fm</id>
  <name>CostOdsFm</name>
  <description>cost ods fm app</description>
  <executable>java</executable>
  <arguments>-jar "D:\app\gcmp\java\script\cost-ods-fm-1.0.0.jar" --server.port=8087</arguments>
  <logpath>D:\app\gcmp\java\script\services\cost-ods-fm\logs</logpath>
  <logmode>rotate</logmode>
  <env name="JAVA_OPTS" value="xxxx"/>
</service>

<service>
  <id>cost-ods-sync</id>
  <name>CostOdsSync</name>
  <description>cost ods sync app</description>
  <executable>java</executable>
  <arguments>-jar "D:\app\gcmp\java\script\cost-ods-sync-1.0.0.jar" --server.port=8085</arguments>
  <logpath>D:\app\gcmp\java\script\services\cost-ods-sync\logs</logpath>
  <logmode>rotate</logmode>
  <env name="JAVA_OPTS" value="xxxx"/>
</service>

关键点‌:

  • 每个服务的 <id> 必须唯一。

  • 通过 --server.port 指定不同端口避免冲突。

  • 日志目录分离,便于排查问题。

四、安装并启动多个服务

# 进入服务目录
cd D:\app\gcmp\java\script\services\cost-gpms
cost-gpms.exe install
net start cost-gpms
​
​
# 进入服务目录
cd D:\app\gcmp\java\script\services\cost-ods-fm
cost-ods-fm.exe install
net start cost-ods-fm
​
# 进入服务目录
cd D:\app\gcmp\java\script\services\cost-ods-sync
cost-ods-sync.exe install
net start cost-ods-sync


Comment