SpringBoot test version
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
SpringBoot default Tomcat is used as a web server
springboot default Tomcat container may be provided by embedded port, set the parameters, the maximum number of threads, etc.
server.port=8081 server.tomcat.max-threads=1000
However, you can not set the maximum number of connections, and the maximum number of connections directly affects the performance of Tomcat, how to adjust the maximum number of connections it?
Here are three ways
One:
import org.apache.coyote.http11.Http11NioProtocol; import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.ServletContextInitializer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.stereotype.Component; @Component() public class MyEmbeddedServletContainerFactory extends TomcatEmbeddedServletContainerFactory { public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) { //设置端口 this.setPort(8081); return super.getEmbeddedServletContainer(initializers); } void customizeConnector protected (Connector Connector) { super.customizeConnector (Connector); Http11NioProtocol = Protocol (Http11NioProtocol) connector.getProtocolHandler (); // set the maximum number of connections protocol.setMaxConnections (2000); // set the maximum number of threads protocol.setMaxThreads ( 2000); protocol.setConnectionTimeout (30000); } }
two:
import org.apache.coyote.http11.Http11NioProtocol; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebServerConfiguration { @Bean public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() { TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); tomcatFactory.setPort(8081); tomcatFactory.addConnectorCustomizers(new MyTomcatConnectorCustomizer()); return tomcatFactory; } } class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer { public void customize(Connector connector) { Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler(); //设置最大连接数 protocol.setMaxConnections(2000); //设置最大线程数 protocol.setMaxThreads(2000); protocol.setConnectionTimeout(30000); } }
three:
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebServerConfiguration { @Bean public EmbeddedServletContainerCustomizer createEmbeddedServletContainerCustomizer() { return new MyEmbeddedServletContainerCustomizer(); } } MyEmbeddedServletContainerCustomizer the implements EmbeddedServletContainerCustomizer class { public void Customize (ConfigurableEmbeddedServletContainer Container) { TomcatEmbeddedServletContainerFactory tomcatFactory = (TomcatEmbeddedServletContainerFactory) Container; tomcatFactory.setPort (8081); // The following operation may refer to the above method } }
Finally, if found not in force, application.properties put inside some configuration on the configuration of Tomcat (server. *) All deleted, OK.