欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot内嵌tomcat调优

程序员文章站 2022-07-26 21:54:59
Springboot默认配置server: tomcat: accept-count: 100 # 最大连接等待数,默认100 max-connections: 10000 #最大连接数,默认为10000 max-threads: 200 #最大工作线程数,默认200 min-spare-threads: 10 #最小工作线程数,默认10最大连接数=max-connections + accept-count最大并发数=max-threads关于max-c...

Springboot Tomcat默认配置

可通过org.springframework.boot.autoconfigure.web.ServerProperties查看,其中包括属性tomcat、jetty、undertow三种服务器的设置,默认启用tomcat。

server:
  tomcat:
    accept-count: 100 # 最大连接等待数,默认100 
    max-connections: 10000 #最大连接数,默认为10000 
    max-threads: 200  #最大工作线程数,默认200
    min-spare-threads: 10 #最小工作线程数,默认10

最大连接数=max-connections + accept-count
最大并发数=max-threads
关于max-connections, accept-count, max-threads三者关系可参考:
秒懂:tomcat的maxConnections、maxThreads、acceptCount 图解

Tomcat经验值

max-threads线程数的经验值为:
1核2g内存,线程数经验值200;
4核8g内存,线程数经验值800。

4核8g内存,建议值:

server:
  tomcat:
    accept-count: 1000
    max-connections: 10000 
    max-threads: 800 
    min-spare-threads: 100

自定义Springboot Tomcat配置

示例代码中均为get方法,可调整为set方法进行配置设置

import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

  private static final Logger logger = LogManager.getLogger(TomcatCustomizer.class);

  @Override
  public void customize(TomcatServletWebServerFactory factory) {
    factory.addConnectorCustomizers(connector -> {
      //AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) connector.getProtocolHandler();
      Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();

      logger.info("####################################################################################");
      logger.info("#");
      logger.info("# TomcatCustomizer");
      logger.info("#");

      logger.info("# MaxConnections {}", protocol.getMaxConnections());
      logger.info("# AcceptorThreadCount {}", protocol.getAcceptorThreadCount());
      logger.info("# AcceptCount {}", protocol.getAcceptCount());

      logger.info("#");
      logger.info("# MaxThread {}", protocol.getMaxThreads());
      logger.info("# MinSpareThreads {}", protocol.getMinSpareThreads());
      logger.info("#");

      logger.info("# ConnectionTimeout {}", protocol.getConnectionTimeout());
      logger.info("# KeepAliveTimeout {}", protocol.getKeepAliveTimeout());
      logger.info("# MaxKeepAliveRequests {}", protocol.getMaxKeepAliveRequests());
      logger.info("#");

      logger.info("####################################################################################");

    });
  }
}

运行结果:
SpringBoot内嵌tomcat调优

本文地址:https://blog.csdn.net/luo15242208310/article/details/107353987

相关标签: java springboot