简体   繁体   English

Spring引导嵌入式容器或war文件在外部容器中进行生产

[英]Spring boot embedded container or war file in an external container for production

I'm complete able to configure spring boot in both cases, the question here is which of them are more robust and is the more recommended, because I didn't find in the spring boot documentation the recommended way to deploy it in a production environment, my concerns about use the embedded container are: 我完全能够在两种情况下配置spring boot,这里的问题是哪些更强大并且更推荐,因为我没有在spring boot文档中找到在生产环境中部署它的推荐方法,我对使用嵌入式容器的担忧是:

  1. If I want to set it as a Windows or Linux service, is the jar file the best option? 如果我想将其设置为Windows或Linux服务,那么jar文件是最佳选择吗?
  2. If I use the jar file I'm not going to have access to restart the server. 如果我使用jar文件,我将无法重新启动服务器。
  3. Maybe in the future I need more applications in the same container. 也许在将来我需要在同一容器中使用更多应用程序。
  4. If I restart the machine I have to execute again the java -jar. 如果我重新启动机器,我必须再次执行java -jar。

The question in general is which is better use the jar file and execute it as java -jar jarname.jar in production or change the packaging to war set the tomcat as provided and set the generated war in an empty tomcat. 一般来说,问题是哪个更好地使用jar文件并在生产中将其作为java -jar jarname.jar执行,或者将包装更改为war将tomcat设置为提供,并将生成的war设置为空的tomcat。

I hope you can help me. 我希望你能帮助我。

---EDIT--- - -编辑 - -

Many times the answer is depends, this is for a normal web application or REST web service. 很多时候,答案取决于,这适用于普通的Web应用程序或REST Web服务。

jar packaging is perfectly suitable for production and you should rather fallback to war only if you really have to - which is often the case when you cannot control your deployment environment (which is often the case in large enterprises). jar包装非常适合生产,你应该只在你真的需要的时候才能倒退到战争中 - 这通常是你无法控制部署环境的情况(在大型企业中通常就是这种情况)。

There is a chapter in Spring Boot Reference about setting up Spring Boot based application as a Unix/Linux/Windows service: Installing Spring Boot applications . Spring Boot Reference中有一章介绍如何将基于Spring Boot的应用程序设置为Unix / Linux / Windows服务: 安装Spring Boot应用程序

Regarding your concern: 关于你的关注:

Maybe in the future I need more applications in the same container. 也许在将来我需要在同一容器中使用更多应用程序。

With embedded containers if you need more applications running on the same machine, you should start two applications separately, each running on different port and effectively you will end up with two containers running - which is good, applications are better isolated from each other. 对于嵌入式容器,如果需要在同一台机器上运行更多应用程序,则应分别启动两个应用程序,每个应用程序在不同的端口上运行,实际上最终会运行两个容器 - 这很好,应用程序可以更好地相互隔离。

About a month ago I had the question like yours. 大约一个月前,我遇到了像你这样的问题。 Let me share my conclusion: 让我分享我的结论:

1) JAR: 1)JAR:

  • You can run independently every appliction with different ports (in linux, java -jar ... > app_logs.log & ) and you can route it (eg nginx). 您可以使用不同的端口(在linux, java -jar ...> app_logs.log& )中独立运行每个应用程序,您可以对其进行路由(例如nginx)。 Note that, restarting is not problem. 请注意,重新启动不是问题。 You can write custom bash script (like this: ps aux | grep appname and kill by PID) 您可以编写自定义bash脚本(如下所示: ps aux | grep appname并通过PID 终止
  • But there are some problems with configuring production app. 但配置生产应用程序存在一些问题。 Property files will archived into jar. 属性文件将存档到jar中。

2) WAR 2)战争

  • You can deploy into container and just run it. 您可以部署到容器中并运行它。 Easy managing at the server. 轻松管理服务器。 If you want to re-configure app, open properties file from unarchived folder inside container, change it as need and restart container. 如果要重新配置应用程序,请从容器内的未归档文件夹中打开属性文件,根据需要进行更改并重新启动容器。 So, managing and configuring will be easy. 因此,管理和配置将很容易。
  • But, if you want to run another app in this server with another port, then you must install another copy of container and config it. 但是,如果您想在此服务器中使用另一个端口运行另一个应用程序,则必须安装另一个容器副本并对其进行配置。

So, in my practice, using war app easier than jar to manage and re-configure. 所以,在我的实践中,使用war app比jar更容易管理和重新配置。

  1. I don't know that much about Windows services but on Linux you can add the execution of a jar to a RC-Scripts (and thus make the application start at a certain run-level). 我对Windows服务知之甚少,但在Linux上,您可以将jar的执行添加到RC-Scripts(从而使应用程序在某个运行级别启动)。 For a spring boot app you just have to symlink to the jar and you can start/stop/etc like any other service, see: Spring Boot application as a Service 对于Spring启动应用程序,您只需要符号链接到jar,您可以像任何其他服务一样启动/停止/等,请参阅: Spring Boot应用程序即服务

  2. restart the machine or the JVM? 重启机器还是JVM? A shutdown mechanism is built into spring boot, you just have to activate it (and you should enable security machanism so that not anybody can do that), see: How to shutdown a Spring Boot Application in a correct way? Spring引导中内置了一个关闭机制,您只需要激活它(并且您应该启用安全机制,以便任何人都不能这样做),请参阅: 如何以正确的方式关闭Spring Boot应用程序?

  3. Spring-Boot enables microservices - so the idea is to have one embedded webapp-container for each webapp/microservice. Spring-Boot支持微服务 - 因此我们的想法是为每个webapp /微服务安装一个嵌入式webapp容器。 This reduces the risk of losing all services when only one is going down. 这样可以降低只有一个服务失败时丢失所有服务的风险。

  4. Yes. 是。 and you have to execute catalina.sh|bat start after every restart. 你必须在每次重启后执行catalina.sh|bat start Or you add an appropriate startup script (see 1.) 或者您添加适当的启动脚本(请参阅1.)

I sense that you'd rather do it the old-fashioned way. 我觉得你宁愿以老式的方式去做。 Despite the 'matter of taste' answer, there is one argument pro-jar: the only dependency is the JVM! 尽管有'品味'的答案,但有一个论点是pro-jar:唯一的依赖是JVM! The rest (the web-app-container, db-drivers, other libraries) is all part of the package you deliver. 其余的(web-app-container,db-drivers,其他库)都是你提供的软件包的一部分。 And if you decide to change the container for the next release, so will it be. 如果您决定更换下一个版本的容器,它也将如此。

One more reason to use "war" file in production. 在生产中使用“war”文件的另一个原因。 Springboot masked an error Jetty threw whereas WAR deployed in Jetty correctly caught it ( though issue below is still under review ) Springboot掩盖了Jetty抛出的错误,而在Jetty中部署的WAR正确捕获了它(虽然下面的问题仍在审核中)

https://github.com/spring-projects/spring-boot/issues/8917#issuecomment-294673487 https://github.com/spring-projects/spring-boot/issues/8917#issuecomment-294673487

I don't know much about server kind of things, But my recommendation is 我不太了解服务器类的东西,但我的建议是

  • If you are using Monolithic application, better to use war with external tomcat. 如果您使用的是Monolithic应用程序,最好使用与外部tomcat的war。

  • If you are using for Micro Service applications, use embedded tomcat with different port. 如果您使用的是Micro Service应用程序,请使用具有不同端口的嵌入式tomcat。 And each micro service applications are independent from each other. 每个微服务应用程序都是相互独立的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM