SpringBoot内置Tomcat参数调优 川长思鸟来 2021-09-29 20:22 511阅读 0赞 今天想设置Tomcat连接超时时间,发现配置文件选项里没有这个参数,决定自己写一个,直接上代码吧。Spring Boot版本2.0.5 package cn.ployer.watch.common.tomcat; import java.nio.charset.Charset; import org.apache.catalina.connector.Connector; import org.apache.coyote.http11.Http11NioProtocol; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Bean; @SpringBootConfiguration public class WebServerConfiguration \{ @Bean public ConfigurableServletWebServerFactory webServerFactory() \{ TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.setPort(11359);//端口号 factory.setUriEncoding(Charset.forName("utf-8"));//编码 factory.addConnectorCustomizers(new MyTomcatConnectorCustomizer()); return factory; \} class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer \{ @Override public void customize(Connector connector) \{ // TODO Auto-generated method stub Http11NioProtocol handler = (Http11NioProtocol)connector.getProtocolHandler(); handler.setAcceptCount(2000);//排队数 handler.setMaxConnections(5000);//最大连接数 handler.setMaxThreads(2000);//线程池的最大线程数 handler.setMinSpareThreads(100);//最小线程数 handler.setConnectionTimeout(30000);//超时时间 \} \} \}
还没有评论,来说两句吧...