如何在生产环境中运行 spring boot 可执行 jar? [英] How do I run a spring boot executable jar in a Production environment?

查看:29
本文介绍了如何在生产环境中运行 spring boot 可执行 jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring boot 的首选部署方式是通过一个包含 tomcat 的可执行 jar 文件.

Spring boot's preferred deployment method is via a executable jar file which contains tomcat inside.

从一个简单的java -jar myapp.jar开始.

现在,我想将该 jar 部署到我在 EC2 上的 linux 服务器,我是否遗漏了什么,或者我真的需要创建一个 init 脚本来正确启动应用程序作为守护程序?

Now, I want to deploy that jar to my linux server on EC2, am I missing something or do I really need to create a init script to properly start the application as a daemon?

如果我只是调用 java -jar 应用程序会在我注销时死亡.

If I simply call java -jar the application dies when I log out.

我可以在 screen 或 nohup 中启动它,但这不是很优雅,并且在我的服务器中重新启动会迫使我手动登录并启动该过程.

I could start it in screen or nohup but that is not very elegant and a restart in my server would force me to log in and start the process manually.

那么,Spring Boot 中的任务是否已经存在?

So, is there something already for the task in spring boot?

推荐答案

请注意,从 Spring Boot 1.3.0.M1 开始,您可以使用 Maven 和 Gradle 构建完全可执行的 jar.

Please note that since Spring Boot 1.3.0.M1, you are able to build fully executable jars using Maven and Gradle.

对于 Maven,只需在 pom.xml 中包含以下内容:

For Maven, just include the following in your pom.xml:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>

对于 Gradle,将以下代码段添加到您的 build.gradle:

For Gradle add the following snippet to your build.gradle:

springBoot {
    executable = true
}

完全可执行的 jar 在文件的前面包含一个额外的脚本,它允许您将 Spring Boot jar 符号链接到 init.d 或使用 systemd脚本.

The fully executable jar contains an extra script at the front of the file, which allows you to just symlink your Spring Boot jar to init.d or use a systemd script.

init.d 示例:

$ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp

这允许您启动、停止和重新启动您的应用程序,例如:

This allows you to start, stop and restart your application like:

$/etc/init.d/yourapp start|stop|restart

或者使用 systemd 脚本:

[Unit]
Description=yourapp
After=syslog.target

[Service]
ExecStart=/var/yourapp/yourapp.jar
User=yourapp
WorkingDirectory=/var/yourapp
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

更多信息,请访问以下链接:

More information at the following links:

这篇关于如何在生产环境中运行 spring boot 可执行 jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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