Although Tomcat is the most common application server it also tends to add some overhead to a small microservice. Lets have a look at the alternatives.
Tomcat Alternatives
There are at least 2 alternatives to Tomcat for a Spring Boot Web application server: Jetty and Undertow. In this blog, I use Undertow because
- it is leightweight (< 1Mb)
- it has Servlet 3.1 support
- it offers Web sockets
Replace Tomcat
In the following example I will show you that only 2 changes are necessary to replace the default Tomcat:
- Add the Undertow dependency
- Exclude Tomcat from the spring-boot-we
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Undertow settings
An additional advantage of using Spring Boot is the configuration by using Spring configuration files. Here is an example application.yml with some server settings:
server:
port: 8080
undertow:
ioThreads: 16
workerThreads: 150
accesslog:
enabled: true
compression:
enabled: true
mimeTypes: text/xml, text/css, text/html, application/json
minResponseSize: 4096