0
点赞
收藏
分享

微信扫一扫

SpringBoot 优化内置 Tomcat

河南妞 2022-12-03 阅读 119


定制 Tomcat 配置

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;

@Slf4j
public class TomcatConnectionCustomizer implements TomcatConnectorCustomizer {

/**
* 配置类: {@link org.springframework.boot.autoconfigure.web.ServerProperties}
*
* 查看线程数命令
* <code>pstree -p pid | wc -l</code>
*/
@Override
public void customize(Connector connector) {
connector.setPort(8080);
// 连接协议处理器. 默认: org.apache.coyote.http11.Http11NioProtocol
connector.setAttribute("protocol", "org.apache.coyote.http11.Http11Nio2Protocol");

/* 参数设置-方式1 */
// connector.setAttribute("minSpareThreads", 100);
// connector.setAttribute("maxThreads", 800);

/* 参数设置-方式2 */
Http11NioProtocol protocolHandler = (Http11NioProtocol) connector.getProtocolHandler();
// 最小工作线程数, 默认: 10(适当增大一些, 以便应对突然增长的访问量)
protocolHandler.setMinSpareThreads(100);
// 最大线程数, 默认: 200(4核8g内存, 线程数经验值800, 操作系统做线程之间的切换调度是有系统开销的, 所以不是越多越好)
protocolHandler.setMaxThreads(800);
// 等待队列长度, 默认100
protocolHandler.setAcceptCount(1000);
// 最大连接数, 默认: 10000
protocolHandler.setMaxConnections(10000);
// 连接超时时间, 默认: 60000
protocolHandler.setConnectionTimeout(100);
// 默认: 1
protocolHandler.setAcceptorThreadCount(10);
// 30秒内没有请求则服务端自动断开keepalive链接
protocolHandler.setKeepAliveTimeout(300000);
// 当客户端发送超过10000个请求则自动断开keepalive链接
protocolHandler.setMaxKeepAliveRequests(10000);
}
}

  • ​​参数提取配置类参考​​
  • ​​SpringBoot Tomcat 参数说明​​

 

服务配置

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.valves.Constants;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import javax.servlet.MultipartConfigElement;

@Configuration
@Slf4j
public class ServerConfiguration {

/**
* 文件上传大小配置
*
* 或者直接使用配置文件配置
* <code>
* spring.servlet.multipart.max-file-size=1024MB
* spring.servlet.multipart.max-request-size=1024MB
* </code>
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
// 单个数据大小
multipartConfigFactory.setMaxFileSize(DataSize.ofMegabytes(1024));
// 总上传数据大小
multipartConfigFactory.setMaxRequestSize(DataSize.ofMegabytes(1024));
return multipartConfigFactory.createMultipartConfig();
}

@Bean
public AccessLogValve getLogAccessLogValue() {
AccessLogValve accessLogValve = new AccessLogValve();
accessLogValve.setDirectory("C:\\logs");
accessLogValve.setEnabled(true);
accessLogValve.setPattern(Constants.AccessLog.COMMON_PATTERN);
accessLogValve.setPrefix("springboot-access-log");
accessLogValve.setSuffix(".txt");
return accessLogValve;
}

@Bean
public TomcatConnectorCustomizer tomcatConnectorCustomizer() {
return new TomcatConnectionCustomizer();
}
}

 

定制 Tomcat

方式1

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.valves.AccessLogValve;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class TomcatCutomization {
@Bean
public ServletWebServerFactory servletContainer(TomcatConnectorCustomizer customizer, AccessLogValve accessLogValve) {
TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
// 定制内置 tomcat 配置
tomcatServletWebServerFactory.addConnectorCustomizers(customizer);

// 设置访问日志存放目录
tomcatServletWebServerFactory.addContextValves(accessLogValve);

// 初始化servletContext对象
tomcatServletWebServerFactory.addInitializers((servletContext) -> {
log.info("servletInfo===========>[{}]", servletContext.getServerInfo());
});
return tomcatServletWebServerFactory;
}
}

方式2

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.valves.AccessLogValve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class TomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
private final TomcatConnectorCustomizer customizer;
private final AccessLogValve accessLogValve;

@Autowired
public TomcatCustomizer(TomcatConnectorCustomizer customizer, AccessLogValve accessLogValve) {
this.customizer = customizer;
this.accessLogValve = accessLogValve;
}

@Override
public void customize(TomcatServletWebServerFactory factory) {
// factory.setPort(8888);
factory.setContextPath("/answer");
factory.setContextValves(Lists.newArrayList(accessLogValve));
factory.addInitializers((servletContext) -> {
log.info("servletInfo===========>[{}]", servletContext.getServerInfo());
});
factory.addConnectorCustomizers(customizer);
}
}

 

​​手把手教你优化springboot!!!​​


举报

相关推荐

0 条评论