26

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. Commented Sep 22, 2016 at 2:21
  • What kind of security do you mean with "Also i think tomcat provide some security to run it."? Commented Sep 22, 2016 at 17:32
  • I think that He is talking about jdbc realm for example Commented Sep 22, 2016 at 17:35
  • 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. Commented Sep 23, 2016 at 7:59
  • Look for the following question: Spring boot project publish to production environment choose war(standalone tomcat) or jar(embedded tomcat)? Commented Sep 26, 2016 at 19:46

5 Answers 5

21
+50

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem with jar deployment is that if you have N applications, you will have N embedded servers. If it's good from an isolation perspective it will multiply the memory consumed by the application server by N. Which could be a problem if N is big. Isn't it?
8

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.

2 Comments

I disagree with the war vs. jar conclusion. Running Spring Boot with embedded Tomcat as an executable jar file is a much easier practice. The concern over multiple instances running and the need for different ports is easily solved with the use of profiles and specifying the profile on startup, like: --spring.profiles.active=prodconfig1, etc. Or even better, just passing in the desired tomcat port as a VM paramter: -Dserver.port=9090 which yields the output: (TomcatEmbeddedServletContainer) - Tomcat started on port(s): 9090 (http)
After so much time, I agree with your ideas.
0
  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.

2 Comments

No no no, I really like to use spring boot with the fat jar my unique concern is about how to deploy it on production, or in the case of your comment how can I create the script that set the jar as a service because there are not many documentation about how to deploy in production the jar, this is the reason because in production I use the external container.
I didn't know that either but it's much simpler than I thought: stackoverflow.com/questions/21503883/…
0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.