Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot app with Embedded tomcat

I am working on a project, we are using Spring-Boot, STS. I just start the project with right click on main and run as spring boot app and it simply runs. Now I am asked to run it with Embedded tomcat on some other machine like Amazon EC2 instance. In normal spring project what I used to do is, create a war file, place in tomcat->webapps folder and start it from bin. In case of embedded tomcat how would I run my app without tomcat. where will my war(which includes embedded tomcat dependencies) run? Please suggest some solution, this is first time working with spring boot and embedded tomcat. Appreciate your suggestion.

like image 955
Atul Avatar asked May 21 '17 19:05

Atul


People also ask

Does Spring boot have embedded tomcat?

By default, Spring Boot provides an embedded Apache Tomcat build. By default, Spring Boot configures everything for you in a way that's most natural from development to production in today's platforms, as well as in the leading platforms-as-a-service.

Can we configure embedded tomcat server in Spring boot?

We can start Spring boot applications in an embedded tomcat container that comes with some pre-configured default behavior via a properties file. In this post, we will learn to modify the default tomcat configurations via overriding respective properties in application.

Can tomcat run Spring boot application?

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.


3 Answers

Just pull in Spring boot tomcat starter, inside your pom or gradle build. This will bring all the tomcat dependencies to your app jar. Embedded tomcat means in runtime inside your JVM Spring boot starts a server with the dependencies in your jar. So all the problems of pushing the war to tomcat folder and restarting are eliminated. Build your jar using Spring boot plugin. mvn spring-boot:run or gradle bootRun and then just run this jar as any executable jar.

java -jar your-app.jar

This calls the Spring Boot Main class. Which internally starts your server and creates your application context. This works for all platform, your local or EC2.

like image 148
Praneeth Ramesh Avatar answered Oct 21 '22 23:10

Praneeth Ramesh


It is suggested to built jar file for spring boot application. This jar contains embedded tomcat inside it. But ofcourse you can build it as war file too. For both of them to run you can simply run java -jar your-jar-file.jar or java -jar your-war-file.war

like image 43
barbakini Avatar answered Oct 21 '22 22:10

barbakini


First, let me clarify about war and jar in the context of modern java web application development.

In the distant past, applications were written, packaged into .war files and deployed onto tomcat servers which ran on some OS like Linux. Usually these applications were big and reflected the whole business logic of some service, like a library site, which wholly depended on this one application that treated requests by generating a suiting response for each one of them.

With time, the web got bigger: More users + more data + stronger computers = more traffic. It got hard to keep this model of application development because It failed to serve reliably under high throughput traffic.

That's how Microservices were born. To keep a long story short: In Java, the best way to build microservices is by using spring-boot and the best way to deploy microservices is by using an embedded server for each microservice, like tomcat. Then these microservices are ideally deployed to the cloud (like AWS). Cloud services know how to deal with jars because jars are native to them.

Now, to your question, where will your war run? Your war won't run. Your jar will be executed by a cloud service and because It has an embedded tomcat dependency, It will run tomcat which will listen on default port 8080. This will be the gateway to your application that you built. The cloud service will probably allocate some DNS to your application and you will be able to communicate with It's embedded tomcat that way.

like image 31
MaxG Avatar answered Oct 21 '22 23:10

MaxG