Home  >  Article  >  Java  >  Improving the performance of Spring Boot applications - Part I

Improving the performance of Spring Boot applications - Part I

王林
王林Original
2024-08-22 12:33:43613browse

Melhorando o desempenho de aplicações Spring Boot - Parte 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn