Improving the performance of Spring Boot applications - Part I
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.
Replacing Tomcat with another servlet container
Applications web, RESTFul, which use Spring MVC, generally add the spring-boot-starter-web dependency, which by default uses Tomcat as a web server. 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 you 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.
Adding Undertow in Spring
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:
<dependencies> <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> </exclusions> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> </dependencies>
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' }
Configuring 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 in the application, and then sending them to a processing queue. A common value is two I/O Threads per CPU core.
The worker threads execute blocking operations, such as Servlet requests that were sent to the processing queue by the 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.
Compressing HTTP responses
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.
The above is the detailed content of Improving the performance of Spring Boot applications - Part I. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
