25
votes

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:

  1. If I want to set it as a Windows or Linux service, is the jar file the best option?
  2. If I use the jar file I'm not going to have access to restart the server.
  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.

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.

I hope you can help me.

---EDIT---

Many times the answer is depends, this is for a normal web application or REST web service.

5
i've had the same question, and i think it's better to have it running on a tomcat server. but depends on what type of operations do you use your project. Also i think tomcat provide some security to run it.Paulo Galdo Sandoval
What kind of security do you mean with "Also i think tomcat provide some security to run it."?Andy
I think that He is talking about jdbc realm for exampleAlejandro Agapito Bautista
Good point. We solved it by adding the param --spring.config.location to the script that starts the application. The param specifies alternative config-files. This way we externalized the DB-Config including access data, but it is also a way to preserve configuration when upgrading the application.Andy

5 Answers

21
votes

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

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.

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.

7
votes

About a month ago I had the question like yours. Let me share my conclusion:

1) JAR:

  • You can run independently every appliction with different ports (in linux, java -jar ... > app_logs.log &) and you can route it (e.g. nginx). Note that, restarting is not problem. You can write custom bash script (like this: ps aux | grep appname and kill by PID)
  • But there are some problems with configuring production app. Property files will archived into jar.

2) WAR

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

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

  2. restart the machine or the 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?

  3. Spring-Boot enables microservices - so the idea is to have one embedded webapp-container for each webapp/microservice. 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. Or you add an appropriate startup script (see 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! The rest (the web-app-container, db-drivers, other libraries) is all part of the package you deliver. And if you decide to change the container for the next release, so will it be.

0
votes

One more reason to use "war" file in production. Springboot masked an error Jetty threw whereas WAR deployed in Jetty correctly caught it ( though issue below is still under review )

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

0
votes

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.

  • If you are using for Micro Service applications, use embedded tomcat with different port. And each micro service applications are independent from each other.