Is it a good practice to deploy multiple microservices in a single tomcat container

Carvia Tech | May 27, 2019 | 1 min read | 10 views


As stated in 12 factor app, we should deploy each microservice in a separate process to allow independent scaling. Therefore, it is best to run each microservice in its own servlet container (Tomcat, Jetty, Undertow, etc.). Spring boot provides support for easily embedding container inside the executable jar, so you just need to run the jar and service is up and running in desired container.

Creating a boot jar using build.gradle
springBoot {
    buildInfo()
}

bootJar {
    archiveBaseName = 'shunya-app'
    version = version
    launchScript()
    mainClassName = "com.shunya.App"
}

Define port as 0 in config file will make your application take a random port. The actual server port can be captured inside a variable using ${local.server.port}

In real cloud environment, Eureka client discovery can be used to locate the host and port number dynamically, thereby eliminating the need for knowing the host and port in advance.

Spring Boot provides us with options to use Tomcat, Jetty or Undertow servlet container in embedded mode with just few lines of code in build.gradle file.

build.gradle - switching from tomcat to undertow
dependencies {
	compile('org.springframework.boot:spring-boot-starter-web') {
		exclude module: "spring-boot-starter-tomcat"    (1)
	}
	compile("org.springframework.boot:spring-boot-starter-undertow")
	//...other dependencies
}
1 In above example we have excluded the default tomcat and opted for undertow as the embedded servlet container for Spring Boot Application.

Top articles in this category:
  1. Cracking Spring Microservices Interviews - question bank
  2. Top 50 microservices architecture interview questions for Java developers
  3. What is Spring Cloud, what are its advantages
  4. Why Microservices are better than Monoliths
  5. How will you Partition a typical e-shop into Microservices Architecture



Find more on this topic:
Microservices image
Microservices

Microservices Architecture and its implementation using Spring Framework

Last updated 1 month ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info