Tomcat调优之记

使用jmeter进行测试,
发现进程数到达一定的数量之后就不会在变化了。

[root@localhost ~]# pstree -p 3793 |wc -l
219
[root@localhost ~]# pstree -p 3793 |wc -l
219
[root@localhost ~]# pstree -p 3793 |wc -l
219
[root@localhost ~]# pstree -p 3793 |wc -l

查看spring-configuration-metadata.json文件,查看Tomcat的相关默认配置

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

从默认配置中找到我们本次特别关注的几个配置

  • server.tomcat.accept-count :等待队列长度,默认100
  • server.tomcat.max-connections :最大可被链接数,默认10000
  • server.tomcat.max-threads :最大工作线程数,默认200
  • server.tomcat.min-spare-threads :最小工作线程数,默认10

在默认配置的情况下,连接超过10000后出现拒绝连接情况,在触发请求超过200+100后拒绝处理。

优化

进入我们配置的application.properties文件,进行编辑

server.tomcat.accept-count=1000
//对于一个4核8G的服务器,一般设置为800是合适的,一旦超过1000,线程之间的切换消耗的时间会大大增加,效率反而会降低
server.tomcat.max-threads=800
server.tomcat.min-spare-threads=100

将文件保存,重启服务,服务重启后再次查看线程数,此时为默认时的线程数。

[root@localhost miaosha]# pstree -p 4596 | wc -l
116

重新进行压测:
Tomcat调优之记_第1张图片
结果为:

[root@localhost miaosha]# pstree -p 4596 | wc -l
819

结果

优化的结果已经有了,之前只能支持200+个并发,现在则可以支持800+的并发,虽然我的机器有些压力但是优化的效果还是非常明显的!

扩展

在使用spring boot内嵌化开发的时候还需要注意keepAlive的问题,有两个参数需要特别注意。

  • keepAliveTimeOut : 多少毫秒后不响应的断开keepAlive
  • maxKeepAliveRequests: 多少次请求后keepAlive断开失效

你可能感兴趣的:(项目,架构之路,-----【Tomca】,【并发】,服务器)