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<java.lang.String>"
    },
    {
      "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

重新進行壓測:
在這裏插入圖片描述
結果爲:

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

結果

優化的結果已經有了,之前只能支持200+個併發,現在則可以支持800+的併發,雖然我的機器有些壓力但是優化的效果還是非常明顯的!

擴展

在使用spring boot內嵌化開發的時候還需要注意keepAlive的問題,有兩個參數需要特別注意。

  • keepAliveTimeOut : 多少毫秒後不響應的斷開keepAlive
  • maxKeepAliveRequests: 多少次請求後keepAlive斷開失效
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章