SpringBoot2 and Tomcat Connection Timeout

pringBoot2 and Tomcat Connection Timeout

First of all, in my friends’ project, they did not use last spring boot web-flux.
So we have application.properties as follow
server.port=50310
server.address=0.0.0.0
server.connection-timeout=-1
server.tomcat.connection-timeout=-1
-1 means never timeout I guess.
I have not tested for confirmation if the properties works or not, because when it has issue, it configure as 1, maybe means 1 minute or 1 second.
I put this EmbeddedTomcatConfig.java in my class path.
package com.sillycat.connectors.quickbooks.config;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
@Component
public class EmbeddedTomcatConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        ((TomcatServletWebServerFactory) factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                protocol.setMaxConnections(200);
                protocol.setMaxThreads(200);
                protocol.setSelectorTimeout(600000);
                protocol.setSessionTimeout(600000);
                protocol.setConnectionTimeout(600000);
            }
        });
    }
}
After that I use my apache ab tool to test the performance.
Install Apache ab Tool
> sudo apt-get install apache2-utils
Try my curl command
> curl -X GET "http://localhost:50300/api/account/v1" -H "company: cloudsnap"
Try with ab tool, 10 requests, 2 concurrent threads
> ab -t 10 -c 2 -l -H "company: cloudsnap" http://localhost:50300/api/account/v1
60 seconds, 2 users keep sending requests
> ab -t 60 -c 20 -l -H "company: cloudsnap" http://localhost:50300/api/account/min/v1

References:
https://stackoverflow.com/questions/31461444/how-do-i-configure-this-property-with-spring-boot-and-an-embedded-tomcat
https://stackoverflow.com/questions/839314/clientabortexception-java-net-socketexception-connection-reset-by-peer-socket
SpringBoot Configuration
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
https://juejin.im/post/5ae17c95518825670d72ce23
https://www.jianshu.com/p/5c66de185ac3
https://my.oschina.net/go4it/blog/1801604

原文地址:https://www.cnblogs.com/telwanggs/p/15099148.html