Springboot内置Tomcat配置调优

我们在使用springboot(版本:2.0.3.RELEASE)开发web项目时,大多数时候采用的是内置的Tomcat

#云服务器配置12核心,24G内存,java启动jar命令:

nohup $JAVA_HOME/bin/java -server -Xms10240m -Xmx14336m -Xmn9216m -XX:MetaspaceSize=400m -XX:MaxMetaspaceSize=5120m -XX:-OmitStackTraceInFastThrow -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -Xloggc:./song_gc.log -XX:ErrorFile=./song_error.log -jar $APP_HOME/$APP_MAINCLASS >> $LOG_FILE 2>&1 &

针对目前的容器优化,可以从以下几点考虑:

1、线程数  2、超时时间  3、JVM优化

首先,线程数是一个重点,每一次HTTP请求到达Web服务器,Web服务器都会创建一个线程来处理该请求,该参数决定了应用服务同时可以处理多少个HTTP请求。

比较重要的有两个:初始线程数最大线程数

初始线程数:保障启动的时候,如果有大量用户访问,能够很稳定的接受请求。最大线程数:用来保证系统的稳定性。

超时时间:用来保障连接数不容易被压垮。如果大批量的请求过来,延迟比较高,很容易把线程数用光,这时就需要提高超时时间。这种情况在生产中是比较常见的 ,一旦网络不稳定,宁愿丢包也不能把服务器压垮

min-spare-threads:最小备用线程数,tomcat启动时的初始化的线程数。

max-threads:Tomcat可创建的最大的线程数,每一个线程处理一个请求,超过这个请求数后,客户端请求只能排队,等有线程释放才能处理。(建议这个配置数可以在服务器CUP核心数的200~250倍之间)

accept-count:当调用Web服务的HTTP请求数达到tomcat的最大线程数时,还有新的HTTP请求到来,这时tomcat会将该请求放在等待队列中,这个acceptCount就是指能够接受的最大等待数,默认100。如果等待队列也被放满了,这个时候再来新的请求就会被tomcat拒绝(connection refused)。

max-connections:这个参数是指在同一时间,tomcat能够接受的最大连接数。一般这个值要大于(max-threads)+(accept-count)。

connection-timeout:最长等待时间,如果没有数据进来,等待一段时间后断开连接,释放线程。

在spring boot配置文件中application.yml,添加以下配置:

这块对tomcat进行了一个优化配置,最大线程数是2500,初始化线程是500,超时时间是12000ms;

复制代码

# Tomcat
server:
  tomcat:
    uri-encoding: UTF-8
    #最小线程数
    min-spare-threads: 500
    #最大线程数
    max-threads: 2500
    #最大链接数
    max-connections: 6500
    #最大等待队列长度
    accept-count: 1000
    #请求头最大长度kb
    max-http-header-size: 1048576
    #请请求体最大长度kb
    #max-http-post-size: 2097152
  #服务http端口
  port: 8080
  #链接建立超时时间
  connection-timeout: 12000
  servlet:
    #访问根路径
    context-path: /song

复制代码

JVM优化一般来说没有太多场景,无非就是加大初始的堆,和最大限制堆,当然也不能无限增大,要根据实际情况优化。

初始内存和最大内存基本会设置成一样的,具体大小根据场景设置,-server是一个必须要用的参数,至于收集器这些使用默认的就可以了,除非有特定需求。

1.使用-server模式:设置JVM使用server模式。64位JDK默认启动该模式。

2.指定堆参数:这个根据服务器的内存大小,来设置堆参数。

-Xms :设置Java堆栈的初始化大小

-Xmx :设置最大的java堆大小

设置初始化堆内存为10240MB,最大为14336MB。

官方文档: https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties

server.tomcat.accept-count

Maximum queue length for incoming connection requests when all possible request processing threads are in use.

100

server.tomcat.keep-alive-timeout

Time to wait for another HTTP request before the connection is closed. When not set the connectionTimeout is used. When set to -1 there will be no timeout.

server.tomcat.max-connections

Maximum number of connections that the server accepts and processes at any given time. Once the limit has been reached, the operating system may still accept connections based on the "acceptCount" property.

8192

server.tomcat.max-http-form-post-size

Maximum size of the form content in any HTTP post request.

2MB

server.tomcat.max-keep-alive-requests

Maximum number of HTTP requests that can be pipelined before the connection is closed. When set to 0 or 1, keep-alive and pipelining are disabled. When set to -1, an unlimited number of pipelined or keep-alive requests are allowed.

100

server.tomcat.max-swallow-size

Maximum amount of request body to swallow.

2MB

server.tomcat.mbeanregistry.enabled

Whether Tomcat's MBean Registry should be enabled.

false

server.tomcat.processor-cache

Maximum number of idle processors that will be retained in the cache and reused with a subsequent request. When set to -1 the cache will be unlimited with a theoretical maximum size equal to the maximum number of connections.

200

server.tomcat.threads.max

Maximum amount of worker threads.

200

server.tomcat.threads.min-spare

Minimum amount of worker threads.

10

server.tomcat.uri-encoding

Character encoding to use to decode the URI.

UTF-8

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值