In the previous post, we have created a web-based Spring Boot application which uses Embedded Tomcat as the default server running on default port 8080
. Spring Boot supports Tomcat, Undetow and Jetty as embedded servers. Now, we will change and/ or configure the default embedded server and common properties to all the available servers.
Spring Boot provides convenient way of configuring dependencies with its starters. For changing the embedded server, we will user its spring-boot-starter-undertow
.
Adding dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
spring-boot-starter-web
comes with Embedded Tomcat. We need to exclude this 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>
This is all we need to do to change the embedded server. There are some generic properties which is applicable for every server and some server specific properties that we can tweak to improve the preformance. Let's change some of the server properties.
Changing the default server port
server.port
property is used for configuring the port on our Spring Boot application should run.
Enabling compression on responses
You can enable to compression on response sent by server and can tweak the mimeTypes, minResponseSize for compression. By default, the compression is disabled. Default property value for mimeTypes is text/html, text/xml,text/plain,text/css,text/javascript,application/javascript
. Default property value for minResponseSize is 2048
bytes.
Other server properties
You can also enable ssl, modify maxHttpPostSize, contextParameters, contextPath and other server related properties. To know more, see org.springframework.boot.autoconfigure.web.ServerProperties
class.
Configuring sever-specific properties
You can also change embedded server specific properties. In our example, we have changed embedded server to Undertow and have tweaked its ioThreads and workerThreads properties.
A sample properties file which have above mentioned properties changes.
server: port: 8082 undertow: ioThreads: 15 workerThreads: 150 accesslog: enabled: true compression: enabled: true mimeTypes: text/xml, text/css, text/html, application/json minResponseSize: 4096 spring: application: name: gaurav-bytes-embedded-server-example
I hope this post is informative and helpful. You can grab the full example code on Github.
Good article..
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteWhat is ioThread and workerThread .. If i want to scale my spring boot application what is the role of these two parameters
ReplyDeleteThanks