SpringBoot测试版本java
<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默认使用的是Tomcat做为web serverweb
springboot中默承认以经过配置以下参数设置内嵌容器Tomcat的端口,最大线程数等等spring
server.port=8081 server.tomcat.max-threads=1000
可是,没法设置最大链接数,而最大链接数直接影响到Tomcat的性能,如何调整这个最大链接数呢?apache
如下有三种方法tomcat
一:springboot
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); } protected void customizeConnector(Connector connector) { super.customizeConnector(connector); Http11NioProtocol protocol = (Http11NioProtocol)connector.getProtocolHandler(); //设置最大链接数 protocol.setMaxConnections(2000); //设置最大线程数 protocol.setMaxThreads(2000); protocol.setConnectionTimeout(30000); } }
二:app
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); } }
三:spring-boot
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(); } } class MyEmbeddedServletContainerCustomizer implements EmbeddedServletContainerCustomizer { public void customize(ConfigurableEmbeddedServletContainer container) { TomcatEmbeddedServletContainerFactory tomcatFactory = (TomcatEmbeddedServletContainerFactory)container; tomcatFactory.setPort(8081); //下面的操做能够参照上面的方法 } }
最后,若是发现没有生效,就把application.properties配置里面关于Tomcat的一些配置(server.*)所有删掉,就OK了。性能