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

前端 未结 4 438
清酒与你
清酒与你 2020-12-09 06:17

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.

Tomc

相关标签:
4条回答
  • 2020-12-09 06:24

    Spring Boot 1.4 and later

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

    Spring Boot 1.3 and earlier

    Provide a customized EmbeddedServletContainerFactory bean:

    @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 TomcatConnectorCustomizer like this:

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

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

    0 讨论(0)
  • 2020-12-09 06:26

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

    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(
                    "####################################################################################");
    
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-09 06:27

    It's actually supposed to be server.connection-timeout in your application.properties. Reference, I suggest you bookmark it.

    0 讨论(0)
  • 2020-12-09 06:35

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题