Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

What is a difference between:

  • Building a Spring Boot .jar file and run it on my remote public host via java -jar myservice.jar?

or

  • Package it to .war file and deploy it on 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 .war file. Is there any other difference? Any pros and cons of both method?

like image 713
countryroadscat Avatar asked Nov 16 '16 20:11

countryroadscat


People also ask

Does spring boot use Jetty or Tomcat?

The Spring Boot starters ( spring-boot-starter-web in particular) use Tomcat as an embedded container by default. You need to exclude those dependencies and include the Jetty one instead.

What happens when we deploy a WAR file in Tomcat?

Java web applications are usually packaged as WAR files for deployment. These files can be created on the command line or with an IDE, like Eclipse. After deploying the WAR file, Tomcat unpacks it and stores all the project files from the webapps directory in a new directory named after the project.

Can we deploy spring boot application in Tomcat?

React Full Stack Web Development With Spring BootBy using Spring Boot application, we can create a war file to deploy into the web server. In this chapter, you are going to learn how to create a WAR file and deploy the Spring Boot application in Tomcat web server.

When deploying your web application as an external WAR file what are the first two details you need to specify in your app?

At minimum, the <servlet> declaration requires two pieces of information: a <servlet-name> , which serves as a handle to reference the servlet elsewhere in the web. xml file, and the <servlet-class> tag, which specifies the Java class name of the servlet.


1 Answers

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

Don't do 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.

As Josh Long once said in one of his Spring IO talks “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.

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.

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

  1. Use embedded tomcat so that tomcat will be packed in your jar and use a JVM docker image

OR

  1. Exclude tomcat from your jar and just pack your application and use a tomcat Docker image.

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

like image 160
so-random-dude Avatar answered Nov 04 '22 23:11

so-random-dude