Spring Boot 应用程序的 tomcat 中的默认线程池

Posted

技术标签:

【中文标题】Spring Boot 应用程序的 tomcat 中的默认线程池【英文标题】:Default thread pool in tomcat of a spring boot application 【发布时间】:2019-06-07 20:06:06 【问题描述】:

在 Spring Boot 应用程序或一般情况下,tomcat 是否配置了默认线程池?

如果我们不配置任何东西,tomcat会为每个请求启动新线程,一旦请求完成,线程就会被销毁?

如果配置了一个线程池,那么当容器从池中选择该线程时,特定线程会为许多请求提供服务?

【问题讨论】:

【参考方案1】:

是的spring boot使用Embeded tomcat server,你可以在application.ymlapplication.properties修改它的一些配置默认它有200个线程spring-docs

# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=8080
server.address= # bind to a specific NIC
server.session-timeout= # session timeout in seconds
server.context-path= # the context path, defaults to '/'
server.servlet-path= # the servlet path, defaults to '/'
server.tomcat.access-log-pattern= # log pattern of the access log
server.tomcat.access-log-enabled=false # is access logging enabled
server.tomcat.protocol-header=x-forwarded-proto # ssl forward headers
server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.basedir=/tmp # base dir (usually not needed, defaults to tmp)
server.tomcat.background-processor-delay=30; # in seconds
server.tomcat.max-threads = 0 # number of threads in protocol handler
server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding

【讨论】:

但它真的是一个游泳池吗?它表明它可以处理的最大并发请求是 200 对吗?并且在线程处理请求后,该线程将不再可用知道吗? 它是线程池,你可以记录线程名称 'Thread.currentThread().getName()' 然后你会看到 'http-nio-1' , 'http-nio-2' 之类的这个【参考方案2】:

这是在springboot中嵌入Tomcat的配置

server.tomcat.accept-count=100 # Maximum queue length for incoming connection requests when all possible request processing threads are in use.
server.tomcat.accesslog.buffered=true # Whether to buffer output such that it is flushed only periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Whether to defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for the IP address, Hostname, protocol, and port used for the request.
server.tomcat.accesslog.rotate=true # Whether to enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.additional-tld-skip-patterns= # Comma-separated list of additional patterns that match jars to ignore for TLD scanning.
server.tomcat.background-processor-delay=10s # Delay between the invocation of backgroundProcess methods. If a duration suffix is not specified, seconds will be used.
server.tomcat.basedir= # Tomcat base directory. If not specified, a temporary directory is used.
server.tomcat.internal-proxies=10\\.\\d1,3\\.\\d1,3\\.\\d1,3|\\
        192\\.168\\.\\d1,3\\.\\d1,3|\\
        169\\.254\\.\\d1,3\\.\\d1,3|\\
        127\\.\\d1,3\\.\\d1,3\\.\\d1,3|\\
        172\\.1[6-9]1\\.\\d1,3\\.\\d1,3|\\
        172\\.2[0-9]1\\.\\d1,3\\.\\d1,3|\\
        172\\.3[0-1]1\\.\\d1,3\\.\\d1,3\\
        0:0:0:0:0:0:0:1\\
        ::1 # Regular expression that matches proxies that are to be trusted.
server.tomcat.max-connections=10000 # Maximum number of connections that the server accepts and processes at any given time.
server.tomcat.max-http-header-size=0 # Maximum size in bytes of the HTTP message header.
server.tomcat.max-http-post-size=2097152 # Maximum size in bytes of the HTTP post content.
server.tomcat.max-threads=200 # Maximum amount of worker threads.
server.tomcat.min-spare-threads=10 # Minimum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL.
server.tomcat.redirect-context-root=true # Whether requests to the context root should be redirected by appending a / to the path.
server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`.
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
server.tomcat.use-relative-redirects= # Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.

对于默认值可以看到,工作线程的最小数量为10,工作线程的最大数量为200,所有可能的请求处理线程时传入连接请求的最大队列长度正在使用的是100

【讨论】:

以上是关于Spring Boot 应用程序的 tomcat 中的默认线程池的主要内容,如果未能解决你的问题,请参考以下文章

是否每个 Spring Boot 应用程序都创建一个能够运行的 tomcat 容器?

在tomcat中部署spring boot应用程序

Spring Boot 如何控制 Tomcat 缓存?

使用 IntelliJ 部署启用嵌入式 tomcat 的 spring-boot 应用程序

Tomcat 不读取 Spring-Boot 应用程序属性

在一个 Tomcat 上运行多个 Spring-boot 应用程序