Spring Boot 应用程序即服务 [英] Spring Boot application as a Service
问题描述
如何在 Linux 系统中很好地配置打包为可执行 jar as a Service 的 Spring Boot 应用程序?这是推荐的方法,还是我应该将此应用程序转换为 war 并将其安装到 Tomcat 中?
How to configure nicely Spring Boot application packaged as executable jar as a Service in the Linux system? Is this recommended approach, or should I convert this app to war and install it into Tomcat?
目前,我可以从 screen
会话运行 Spring 启动应用程序,这很好,但需要在服务器重启后手动启动.
Currently, I can run Spring boot application from the screen
session, which is nice but requires manual start after a server reboot.
我正在寻找的是一般建议/方向或示例 init.d
脚本,如果我对可执行文件 jar 的方法是正确的.
What I'm looking for is general advice/direction or sample init.d
the script, if my approach with executable jar is proper.
推荐答案
以下适用于 springboot 1.3 及以上版本:
The following works for springboot 1.3 and above:
作为 init.d 服务
可执行 jar 具有通常的启动、停止、重新启动和状态命令.它还会在通常的/var/run 目录中设置一个 PID 文件,并默认登录到通常的/var/log 目录中.
The executable jar has the usual start, stop, restart, and status commands. It will also set up a PID file in the usual /var/run directory and logging in the usual /var/log directory by default.
你只需要像这样将你的 jar 符号链接到/etc/init.d
You just need to symlink your jar into /etc/init.d like so
sudo link -s /var/myapp/myapp.jar /etc/init.d/myapp
或
sudo ln -s ~/myproject/build/libs/myapp-1.0.jar /etc/init.d/myapp_servicename
之后就可以正常操作了
/etc/init.d/myapp start
然后在您希望应用程序在启动时启动/停止的任何运行级别(如果需要)设置链接.
Then setup a link in whichever runlevel you want the app to start/stop in on boot if so desired.
作为系统服务
要运行安装在 var/myapp 中的 Spring Boot 应用程序,您可以在/etc/systemd/system/myapp.service 中添加以下脚本:
To run a Spring Boot application installed in var/myapp you can add the following script in /etc/systemd/system/myapp.service:
[Unit]
Description=myapp
After=syslog.target
[Service]
ExecStart=/var/myapp/myapp.jar
[Install]
WantedBy=multi-user.target
<小时>
注意:如果您使用此方法,请不要忘记使 jar 文件本身可执行(使用 chmod +x),否则它将失败并显示权限被拒绝"错误.
NB: in case you are using this method, do not forget to make the jar file itself executable (with chmod +x) otherwise it will fail with error "Permission denied".
参考
这篇关于Spring Boot 应用程序即服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!