Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep-alive configurations of spring-boot app (with embedded tomcat)

I am trying to fix/debug an issue of too many closing connection in a spring-boot web app that uses embedded tomcat. The problem arise because it closes connection that should be kept alive.

Now, I found that tomcat has configuration that limit the number of keep-alive connection (see maxKeepAliveRequests in https://tomcat.apache.org/tomcat-8.5-doc/config/http.html) and there are maybe other config that could be related to the issue. But my problem is that I don't see where are those parameters given, or how I could change them if default are used.

My question: where can I found a documentation that explain how to config spring-boot/embedded-tomcat keep-alive parameters, and which are those parameters?

like image 587
Juh_ Avatar asked May 10 '19 09:05

Juh_


People also ask

Can we configure embedded Tomcat server in Spring Boot?

The Embedded tomcat server has a set of default configurations, which makes them ready to use. However, we can use the properties or yaml files to configure embedded tomcat server and change the default settings. We will start with the most basic Tomcat configurations like server address and port.

Can we use Spring Boot embedded Tomcat in production?

Yes. It's the same tomcat, but in library form. So you are responsible for configuration and other things that the standalone tomcat provides. Also you don't have to use tomcat.

Can Tomcat run Spring Boot application?

In contrast to standalone applications, Tomcat is installed as a service that can manage multiple applications within the same application process, avoiding the need for a specific setup for each application. In this tutorial, we'll create a simple Spring Boot application and adapt it to work within Tomcat.


1 Answers

Not all tomcat properties can be configured via the properties file. The keep-alive related properties are one of those properties and that means they can only be configured programmatically. This is done by configuring a WebServerFactoryCustomizer bean. You can use the protocol handler to set the KeepAlive settings. An example below:

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer() {
    return (tomcat) -> tomcat.addConnectorCustomizers((connector) -> {
        if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {
            AbstractHttp11Protocol<?> protocolHandler = (AbstractHttp11Protocol<?>) connector
                    .getProtocolHandler();
            protocolHandler.setKeepAliveTimeout(80000);
            protocolHandler.setMaxKeepAliveRequests(500);
            protocolHandler.setUseKeepAliveResponseHeader(true);
        }
    });
}

To know more about these settings please read the tomcat 9 configuration reference

like image 121
Maurice Avatar answered Sep 23 '22 08:09

Maurice