java Spring 启动应用程序与部署在 Tomcat/Jetty 上的 .war 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40641589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:22:14  来源:igfitidea点击:

Spring boot app vs .war file deployed on Tomcat/Jetty

javatomcatspring-bootjetty

提问by ilovkatie

In my case let's consider a simple RESTful service created and configured with Spring Boot. This service communicates with a database (e.g. Postgres).

就我而言,让我们考虑使用 Spring Boot 创建和配置的简单 RESTful 服务。该服务与数据库(例如 Postgres)通信。

What is a difference between:

有什么区别:

  • Building a Spring Boot .jarfile and run it on my remote public host via java -jar myservice.jar?
  • 构建一个 Spring Boot.jar文件并通过java -jar myservice.jar?

or

或者

  • Package it to .warfile and deploy it on Tomcat/Jetty?
  • 打包成.war文件,部署在Tomcat/Jetty上?

First option seems to be a lot easier, u just need to run a .jar. In second option u need to create Tomcat instance, run it and then deploy a .warfile. Is there any other difference? Any pros and cons of both method?

第一个选项似乎要容易得多,你只需要运行一个.jar. 在第二个选项中,您需要创建 Tomcat 实例,运行它,然后部署一个.war文件。还有其他区别吗?这两种方法有什么优缺点吗?

回答by so-random-dude

Package it to .war file and deploy it on Tomcat/Jetty?

将其打包为 .war 文件并将其部署在 Tomcat/Jetty 上?

Don'tdo this if at all possible. Reason: with embedded tomcat (or any other server runtime like undertow, netty, jetty etc) it's much easier to build a micro-services architecture.

如果可能,请要这样做。原因:使用嵌入式 tomcat(或任何其他服务器运行时,如 undertow、netty、jetty 等),构建微服务架构要容易得多。

As Josh Long once said in one of his Spring IO talks “make Jar, not War”

正如 Josh Long 在他的一次 Spring IO 演讲中所说的那样 “make Jar, not War”

On the other hand, if you have an existing infrastructure out there with servlet containers and already there are multiple application.wars running in those servlet containers, and all you have to do is just package your application as a war file and hand it over to a release team (or rather you are forced to just reuse the existing infrastructure), then it's a different story...But, tech-world is already moving away from this practice.

另一方面,如果您有一个带有 servlet 容器的现有基础设施,并且已经有多个 application.wars 在这些 servlet 容器中运行,那么您所要做的就是将您的应用程序打包为一个 war 文件并将其交给一个发布团队(或者更确切地说,您被迫只重用现有的基础设施),那么情况就不同了……但是,技术世界已经远离这种做法。

Again, in the spirit of microservices, in the spirit of what spring boot intended for, I would encourage you to use embedded server and make runnable jar file rather than going with traditional deployment.

同样,本着微服务的精神,本着 spring boot 的精神,我鼓励您使用嵌入式服务器并制作可运行的 jar 文件,而不是使用传统部署。

Edit

编辑

If you are dockerizing your application, you have 2 options to package your tomcat in your "final artifact" (by "final artifact", I meant docker image, not the jar. Jar is just an intermediate artifact here).

如果您正在对您的应用程序进行 dockerizing,您有 2 个选项可以将您的 tomcat 打包到您的“最终工件”中(“最终工件”,我指的是 docker 镜像,而不是 jar。Jar 在这里只是一个中间工件)。

  1. Use embedded tomcat so that tomcat will be packed in your jar and use a JVM docker image
  1. 使用嵌入式 tomcat,以便 tomcat 将被打包到您的 jar 中并使用 JVM docker 映像

OR

或者

  1. Exclude tomcat from your jar and just pack your application and use a tomcat Docker image.
  1. 从您的 jar 中排除 tomcat,只需打包您的应用程序并使用 tomcat Docker 映像。

Still, any day, I would go with 1st option because that make development process much easier (and this goes with out saying, on-boarding a new developer will be easier).

尽管如此,任何一天,我都会选择第一个选项,因为这使开发过程更容易(不用说,加入新开发人员会更容易)。

回答by dahrens

As described on the product pagethose jars include the servlet container and will directly start it.

产品页面所述,这些 jar 包含 servlet 容器并将直接启动它。

Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

直接嵌入Tomcat、Jetty或Undertow(无需部署WAR文件)

If you plan to run your application on one host with nothing else next to it (docker?) this might be a nice possibility - if your host contains other apps the approach to deploy wars would be more interesting as you can have those servlet containers only once on your host.

如果你打算在一个主机上运行你的应用程序,它旁边没有其他东西(docker?),这可能是一个很好的可能性 - 如果你的主机包含其他应用程序,那么部署War的方法会更有趣,因为你只能拥有那些 servlet 容器一次在你的主机上。