Article directory
In the Spring Boot project, web application service containers such as Tomcat, Jetty, and Undertow can be supported. When we add the spring-boot-starter-web dependency, Tomcat will be used as the embedded web container by default, and we don't need to deploy it separately. The web application can be run as a jar package.
Two configuration methods for adjusting the parameters of the SpringBoot application container
- Modify the configuration file (simple)
- Custom configuration class (professional tuning)
Text video
Running parameter configuration of embedded tomcat container
1. Configuration file method
In application.properties/application.yml, you can configure the properties required for the operation of the web container. You can view all the configuration items about the server on the official website through this link: server-properties .
- The beginning of server.xx is the configuration common to all servlet containers,
- Server.tomcat.xx starts with configuration parameters specific to the tomcat container
- Server.jetty.xx starts with configuration parameters specific to the Jetty container
- Server.undertow.xx starts with the configuration parameters specific to the undertow container
1.1. Common configuration parameters
parameter | Defaults | illustrate |
---|---|---|
server.port | 8080 | Configure the port number of the web container |
server.servlet.session.timeout | 30m (30 minutes) | Session expiration time. If no unit is written, the default unit is seconds. (Note: Since the session expiration time configured in Tomcat is in minutes, if the setting here is seconds, it will be automatically converted to a maximum number of minutes that does not exceed the configured seconds. For example, 119 seconds (1 minute 59 seconds) is configured ), then the actual session expiration time is 1 minute) |
server.servlet.context-path | / | The base path of the URL access path |
server.tomcat.uri-encoding | UTF-8 | Configure Tomcat request encoding |
server.tomcat.basedir | Configure the directory for Tomcat run logs and temporary files. If not configured, the system's temporary directory will be used by default. |
1.2. Tomcat performance optimization core parameters
The working principle of the tomcat connector:
- A request receiving queue is maintained before the Acceptor. The maximum length of the queue is: the maximum number of request connections that tomcat can accept: server.tomcat.max-connections.
- Acceptor listens for connection requests and generates a SocketProcessor task and submits it to the thread pool for processing
- When all threads in the thread pool are occupied, the newly created SocketProcessor task is put into the waiting queue, and the maximum degree of waiting for the queue: server.tomcat.accept-count
- The server.tomcat.threads.max of the thread pool determines the limit SocketProcessor task processing capability of tomcat. It's not that bigger is better. The more threads, the more resources are consumed.
- The server.tomcat.threads.min-spare of the thread pool reserves a certain number of threads in the thread pool when the application is idle. Avoid time-wasting by temporarily creating threads after the request arrives.
parameter | Defaults | illustrate |
---|---|---|
server.tomcat.max-connections | 8192 | Maximum number of requested connections accepted |
server.tomcat.accept-count | 100 | When all threads are occupied, the maximum number of request connections that are put into the request queue and wait |
server.tomcat.threads.max | 200 | Maximum number of worker thread pools |
server.tomcat.threads.min-spare | 10 | Minimum number of worker thread pools |
Second, the custom configuration class
Steps:
1. Create a configuration class and add @Configuration annotation
2. Add the customizer ConfigurableServletWebServerFactory
3. Return the customizer
@Configuration
public class TomcatCustomizer {
@Bean
public ConfigurableServletWebServerFactory configurableServletWebServerFactory(){
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers(new MyTomcatConnectionCustomizer());
return factory;
}
static class MyTomcatConnectionCustomizer implements TomcatConnectorCustomizer {
public MyTomcatConnectionCustomizer() {
}
@Override
public void customize(Connector connector) {
connector.setPort(Integer.parseInt("8888"));
connector.setProperty("maxConnections", "8192");
connector.setProperty("acceptorThreadCount", "100");
connector.setProperty("minSpareThreads", "10");
connector.setProperty("maxThreads", "200");
}
}
}
This approach is more customizable and more flexible. However, you need to have a deep understanding of the underlying implementation principle and design mechanism of the server container, and you also need to have a certain API proficiency of TomcatServletWebServerFactory.
more flexible. However, you need to have a deep understanding of the underlying implementation principle and design mechanism of the server container, and you also need to have a certain API proficiency of TomcatServletWebServerFactory.