When starting Spring Boot applications, we typically use the default settings provided by starters, which is sufficient for most cases. However, if we are in need of performance, there are specific adjustments that can be made, as will be demonstrated in the first part of this article.
Applications web, RESTFul, which use Spring MVC, generally add the spring-boot-starter-web dependency, which by default uses Tomcat as server web. However, there are more interesting alternatives, such as Undertow, which is a high-performance web server, with an asynchronous and non-blocking architecture, which allows it to handle a large number of simultaneous connections. efficiently, making it suitable for high-performance applications. We're not saying Tomcat is bad, but we can give Undertow.
a chance.In order for us to use Undertow as a web server, we need to ignore the spring-boot-starter-tomcat dependency that spring-boot-starter-web already adds and then add the spring-boot-starter-undertow.
Using pom.xml:
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-undertow
Using build.gradle:
dependencies { implementation('org.springframework.boot:spring-boot-starter-web') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' } implementation 'org.springframework.boot:spring-boot-starter-undertow' }
Through application.properties or application.yml, we can configure how many IO threads and how many worker threads we want the server to use.
Using application.yml
server: undertow: threads: io: 4 worker: 64
Using application.properties
server.undertow.threads.io=4 server.undertow.threads.worker=64
I/O Threads perform non-blocking operations and should never perform blocking operations, as they are responsible for listening to connections arriving at the application, and then sending them to a processing queue. A common value is two I/O Threads per CPU core.
worker threads execute blocking operations, such as Servlet requests that were sent to the processing queue by I/O Threads. The ideal value depends on the workload, but it is generally recommended to configure around 10 threads per CPU core.
For more detailed information and more options that can be explored, just go to the Undertow documentation.
Data compression is a feature that aims to reduce the body size of HTTP responses, which in turn can improve the performance of our application by reducing the amount of data transmitted over the network.
Configuring data compression in Spring Boot is a trivial task, as it supports this functionality.
Using application.yml
server: compression: enabled: true mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json min-response-size: 1024
Using application.properties
server.compression.enabled=true server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json server.compression.min-response-size=1024
server.compression.enabled: Enables/disables compression.
server.compression.mime-types: List of MIME types that should be compressed.
server.compression.min-response-size: Minimum size of "Content-Length" that is necessary for compression to be performed.
With this, we close the first part of the article. In the next part, we will learn more about Hikari, JPA and Hibernate and learn how to configure them, in order to further improve the performance of Spring Boot applications.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3