1
0
mirror of https://gitee.com/coder-xiaomo/flashsale synced 2025-01-10 19:58:14 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

定制化内嵌Tomcat开发

This commit is contained in:
程序员小墨 2022-03-07 12:08:49 +08:00
parent 683aa24459
commit cfd3a6f029

View File

@ -0,0 +1,35 @@
package com.cxyxiaomo.flashsale.config;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.ProtocolHandler;
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.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;
/**
* 当Spring容器内没有TomcatEmbeddedServletContainerFactory这个bean时会把当前bean加载进Spring容器中
*/
@Component
public class WebServerConfiguration implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
// 使用对应工厂类提供给我们的接口定制化我们的tomcat connector
((TomcatServletWebServerFactory)factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
Http11NioProtocol protocolHandler = (Http11NioProtocol) connector.getProtocolHandler();
// 定制化 KeepAliveTimeout设置30秒内没有请求则服务端自动断开KeepAlive连接
protocolHandler.setKeepAliveTimeout(30000);
// 当客户端发送超过10000个请求则自动断开KeepAlive连接
protocolHandler.setMaxKeepAliveRequests(10000);
}
});
}
}