在Spring Boot中,可以通过配置文件或者编程方式设置Keep-Alive。
通过配置文件设置Keep-Alive:在application.properties或application.yml文件中添加以下配置:server.tomcat.keepAliveTimeout=60000server.tomcat.maxKeepAliveRequests=100server.tomcat.keepAliveTimeout属性设置了Keep-Alive超时时间,单位为毫秒。上述示例中的超时时间为60秒。server.tomcat.maxKeepAliveRequests属性设置了最大Keep-Alive请求数。上述示例中的最大请求数为100。通过编程方式设置Keep-Alive:可以通过自定义Tomcat的配置类来实现编程方式设置Keep-Alive,例如:import org.apache.catalina.connector.Connector;import org.apache.coyote.http11.AbstractHttp11Protocol;import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class TomcatConfig { @Bean public TomcatServletWebServerFactory tomcatServletWebServerFactory() { return new TomcatServletWebServerFactory() { @Override protected void customizeConnector(Connector connector) { super.customizeConnector(connector); if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) { ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setKeepAliveTimeout(60000); ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxKeepAliveRequests(100); } } }; }}上述示例中,通过自定义TomcatServletWebServerFactory类,重写customizeConnector方法来设置Keep-Alive超时时间和最大请求数。
需要注意的是,具体的配置方式可能会因为Spring Boot的版本和使用的容器而有所不同,以上示例适用于Spring Boot 2.x版本,并使用Tomcat作为容器。如果使用其他版本或者其他容器,可能需要做相应的调整。