Spring Boot应用程序即服务 [英] Spring Boot application as a Service

查看:70
本文介绍了Spring Boot应用程序即服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Linux系统中很好地配置打包为可执行jar作为服务的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.

如果我使用可执行文件 jar 的方法是正确的,我正在寻找的是一般建议/指导或示例 init.d 脚本.

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

OR

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".

参考

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/deployment-install.html#deployment-service

这篇关于Spring Boot应用程序即服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆