如何为WinSW提供spring.config.location以将SpringBoot安装为Windows服务?

问题描述

我正在尝试设置WinSW以在Windows10上将Spring Boot JAR安装为Windows服务。到目前为止,当我只在XML文件中提供JAR文件名和配置选项时,它就可以使用。

<service>
    <id>testSB/id>
    <name>testSB</name>
    <description>This service runs a spring boot JAR as service.</description>
    <executable>java</executable>
    <startmode>Automatic</startmode>
    <delayedAutoStart>true</delayedAutoStart>
    <onfailure action="restart" delay="10 sec"/>
    <onfailure action="restart" delay="20 sec"/>
    <onfailure action="none"/>
    <resetfailure>1 hour</resetfailure>
    <arguments>-jar "myjar-1.0.0.jar" </arguments>
    <log mode="roll-by-size-time">
      <sizeThreshold>20480</sizeThreshold>
      <pattern>yyyyMMdd</pattern>
      <autoRollAtTime>00:30:00</autoRollAtTime>
      <zipOlderThanNumDays>5</zipOlderThanNumDays>
    </log>
    <logpath>%BASE%/logs</logpath>
</service>

它可以正常工作,我可以看到主页。

但是,如果我添加--spring.config.location = application.yml,服务将无法运行。

<service>
    <id>testSB/id>
    <name>testSB</name>
    <description>This service runs a spring boot JAR as service.</description>
    <executable>java</executable>
    <startmode>Automatic</startmode>
    <delayedAutoStart>true</delayedAutoStart>
    <onfailure action="restart" delay="10 sec"/>
    <onfailure action="restart" delay="20 sec"/>
    <onfailure action="none"/>
    <resetfailure>1 hour</resetfailure>
    <arguments>-jar "myjar-1.0.0.jar" --spring.config.location=./application.yml</arguments>
    <log mode="roll-by-size-time">
      <sizeThreshold>20480</sizeThreshold>
      <pattern>yyyyMMdd</pattern>
      <autoRollAtTime>00:30:00</autoRollAtTime>
      <zipOlderThanNumDays>5</zipOlderThanNumDays>
    </log>
    <logpath>%BASE%/logs</logpath>
</service>

是否使用相对路径./application.yml,%BASE%:%BASE%/application.yml绝对路径C:\path\to\app.yml都没关系。它总是失败,并且日志只说:

2020-09-21 16:57:25,626 DEBUG - Completed. Exit code is 0
2020-09-21 16:57:30,560 DEBUG - Starting WinSW in console mode
2020-09-21 16:57:30,930 DEBUG - User requested the status of the process with id 'testSB'
2020-09-21 16:57:30,932 DEBUG - Completed. Exit code is 0
2020-09-21 16:57:35,363 DEBUG - Starting WinSW in service mode
2020-09-21 16:57:35,380 INFO  - Starting java -jar "myjar-1.0.0.jar" --spring.config.location=C:\path\to\application.yml
2020-09-21 16:57:35,395 DEBUG - Completed. Exit code is 0

Windows事件查看器显示以下内容错误

Service cannot be started. System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
   at System.Diagnostics.Process.StartWithCreateProcess(processstartinfo startInfo)
   at WinSW.Util.ProcessHelper.StartProcessAndCallbackForExit(Process processtoStart,String executable,String arguments,Dictionary`2 envVars,String workingDirectory,Nullable`1 priority,ProcessCompletionCallback callback,Boolean redirectStdin,LogHandler logHandler,Boolean hideWindow)
   at WinSW.WrapperService.StartProcess(Process processtoStart,Boolean redirectStdin)
   at WinSW.WrapperService.OnStart(String[] args)
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

是否有提供此春季启动选项的方式来启动JAR?

解决方法

我认为这应该可行:

<arguments>-Dspring.config.location=./application.yml -jar "myjar-1.0.0.jar"</arguments>