Spring Boot程序做成系统服务(windows,详细的Linux运维学习指南
warning:
这篇文章距离上次修改已过432天,其中的内容可能已经有所变动。
将Spring Boot程序制作成系统服务,在Windows和Linux上有所不同。
Windows
在Windows上,你可以使用winsw来将Spring Boot应用程序制作成服务。
- 下载
winsw的可执行文件到你的Spring Boot应用程序的目录中。 - 为你的应用程序创建一个配置文件,如
your-app.xml,它应该与winsw.exe在同一个目录。 - 编辑配置文件,指定Java命令和你的Spring Boot应用程序的主类。
- 使用
winsw安装你的服务:运行your-app.exe install。 - 启动服务:
your-app.exe start。
Linux
在Linux上,你可以使用systemd来创建服务单元。
- 为你的应用程序创建一个
systemd服务文件,如your-app.service,它通常位于/etc/systemd/system/。 - 编辑服务文件,指定Java命令和你的Spring Boot应用程序的启动脚本。
- 重新加载
systemd配置:sudo systemctl daemon-reload。 - 启动服务:
sudo systemctl start your-app。 - 设置服务开机自启:
sudo systemctl enable your-app。
示例代码和配置
Windows
your-app.xml 配置示例:
<service>
<id>your-app</id>
<name>Your Application</name>
<description>This service runs Your Spring Boot Application</description>
<executable>java</executable>
<arguments>-jar "your-app.jar"</arguments>
<logpath>logs</logpath>
<logmode>rotate</logmode>
</service>Linux
your-app.service 配置示例:
[Unit]
Description=Your Spring Boot Application
After=network.target
[Service]
User=your-user
Type=simple
ExecStart=/usr/bin/java -jar /path/to/your-app.jar
Restart=on-failure
[Install]
WantedBy=multi-user.target确保替换<executable>、<arguments>、[Service]ExecStart中的路径和用户为你的实际情况。
这些是大致步骤,实际操作时可能需要根据Spring Boot应用程序的具体情况进行调整。
评论已关闭