Java 如何使用 Spring Boot 和嵌入式 Tomcat 配置此属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31461444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:08:45  来源:igfitidea点击:

How do I configure this property with Spring Boot and an embedded Tomcat?

javaspringtomcatspring-boottomcat8

提问by smuggledPancakes

Do I configure properties like the connectionTimeout in the application.properties file or is the somewhere else to do it? I can't figure this out from Google.

我是在 application.properties 文件中配置诸如 connectionTimeout 之类的属性还是在其他地方进行配置?我无法从谷歌那里弄清楚这一点。

Tomcat properties list

Tomcat 属性列表

I found this Spring-Boot example, but it does not include a connectionTimeout property and when I set server.tomcat.connectionTimeout=60000in my application.properties file I get an error.

我找到了这个Spring-Boot 示例,但它不包含 connectionTimeout 属性,当我server.tomcat.connectionTimeout=60000在 application.properties 文件中设置时,出现错误。

采纳答案by hzpz

Spring Boot 1.4 and later

Spring Boot 1.4 及更高版本

As of Spring Boot 1.4 you can use the property server.connection-timeout. See Spring Boot's common application properties.

从 Spring Boot 1.4 开始,您可以使用该属性server.connection-timeout。请参阅 Spring Boot 的常用应用程序属性

Spring Boot 1.3 and earlier

Spring Boot 1.3 及更早版本

Provide a customized EmbeddedServletContainerFactorybean:

提供一个定制的EmbeddedServletContainerFactorybean:

@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();

    factory.addConnectorCustomizers(connector -> 
            ((AbstractProtocol) connector.getProtocolHandler()).setConnectionTimeout(10000));

    // configure some more properties

    return factory;
}

If you are not using Java 8 or don't want to use Lambda Expressions, add the TomcatConnectorCustomizerlike this:

如果您不使用 Java 8 或不想使用Lambda 表达式,请添加TomcatConnectorCustomizer如下内容:

    factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
            ((AbstractProtocol) connector.getProtocolHandler()).setConnectionTimeout(10000);
        }
    });

The setConnectionTimeout()method expects the timeout in milliseconds (see connectionTimeoutin Apache Tomcat 8 Configuration Reference).

setConnectionTimeout()方法需要以毫秒的超时时间(见connectionTimeout的Apache Tomcat 8配置参考)。

回答by Juraj

I prefer set of system properties before the server start:

我更喜欢在服务器启动之前设置系统属性:

/**
 * Start SpringBoot server
 */
@SpringBootApplication(scanBasePackages= {"com.your.conf.package"})
//@ComponentScan(basePackages = "com.your.conf.package")
public class Application {
    public static void main(String[] args) throws Exception {
        System.setProperty("server.port","8132"));
        System.setProperty("server.tomcat.max-threads","200");
        System.setProperty("server.connection-timeout","60000");
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

回答by rorschach

It's actuallysupposed to be server.connection-timeoutin your application.properties. Reference, I suggest you bookmark it.

实际上应该server.connection-timeout在你的application.properties. 参考,我建议你收藏它。

回答by ???

After spring boot 2.x and later, the implement method of the embeding tomcat has been changed.

spring boot 2.x及以后,内嵌tomcat的实现方式发生了变化。

refer to the code below.

请参阅下面的代码。

import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class TomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Autowired
    private ContainerProperties containerProperties;

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.addConnectorCustomizers(connector -> {
            AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) connector.getProtocolHandler();

            protocol.setMaxKeepAliveRequests(10);


            log.info("####################################################################################");
            log.info("#");
            log.info("# TomcatCustomizer");
            log.info("#");
            log.info("# custom maxKeepAliveRequests {}", protocol.getMaxKeepAliveRequests());
            log.info("# origin keepalive timeout: {} ms", protocol.getKeepAliveTimeout());
            log.info("# keepalive timeout: {} ms", protocol.getKeepAliveTimeout());
            log.info("# connection timeout: {} ms", protocol.getConnectionTimeout());
            log.info("# max connections: {}", protocol.getMaxConnections());
            log.info("#");
            log.info(
                "####################################################################################");

        });
    }
}