如何在springboot中设定keepalive timeout

查看springboot的官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.server.server.tomcat.keep-alive-timeout

可以看到对server.tomcat.keep-alive-timeout这个参数的定义如下:
Time to wait for another HTTP request before the connection is closed. When not set the connectionTimeout is used. When set to -1 there will be no timeout.

可是在配置文件中按照上面所说,进行了定义:(注意,单位是毫秒,spring的文档历来是垃圾中的战斗机)

server:
  tomcat:
    keep-alive-timeout: 30000

运行后,发现不起任何作用。

于是只能按照上面所说的,对connection-timeout进行设置:

server:
  tomcat:
    connection-timeout: 30000

这样就起效了,说明keep-alive-timeout没有用,文档有误。

上述是通过配置来更改,还可以通过代码来设定keepalive_timeout,如下:

@Configuration
public class Config {

    @Bean   
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat
        .addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                ((AbstractProtocol) connector.getProtocolHandler())
                        .setKeepAliveTimeout(30000);
            }
        });
        return tomcat;
    }

}

发表评论

邮箱地址不会被公开。 必填项已用*标注