Tomcat调优之记

本文通过jmeter测试发现,当进程数达到一定数量后,Tomcat不再接受新连接。研究了Tomcat的默认配置,重点关注了accept-count、max-connections、max-threads和min-spare-threads等参数。在调整了application.properties文件的配置后,服务重启并重新压测,并发支持从200+提升到800+,优化效果显著。同时,还提及了在Spring Boot内嵌Tomcat开发中,keepAliveTimeOut和maxKeepAliveRequests两个参数对性能的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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 pa
### 单机Tomcat服务器性能最佳实践 #### 配置文件化 对于单机Tomcat服务器而言,整`server.xml`中的连接器设置能够显著影响其处理请求的能力。通过修改最大线程数(`maxThreads`)、最小空闲线程数(`minSpareThreads`)以及接受队列大小(`acceptCount`)等参数来适应不同的负载情况[^1]。 ```xml <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxThreads="500" minSpareThreads="75" acceptCount="100"/> ``` #### JVM 参数整 适当配置JVM启动参数有助于提高应用响应速度和稳定性。增加堆内存(-Xms, -Xmx),启用垃圾回收日志录(-XX:+PrintGCDetails),并选择合适的GC算法(如G1收集器)都是有效的手段之一[^2]。 ```bash JAVA_OPTS="-Djava.awt.headless=true \ -Xms512m -Xmx1g \ -XX:MaxPermSize=256m \ -XX:+UseG1GC \ -XX:+PrintGCDetails" ``` #### 应用层面化 减少会话超时时间(session-timeout),合理利用缓存机制,并确保数据库查询语句高效执行也是不可忽视的部分。此外,在开发阶段应尽可能早地发现潜在瓶颈并通过工具进行分析解决。 #### 日志管理 合理的日志级别设定不仅有利于排查问题还不会给系统带来过多负担。通常情况下生产环境中建议将日志等级设为WARN及以上,同时定期清理过期的日志文件以释放磁盘空间。 #### 实时监控与自动扩展 部署专业的APM(Application Performance Management)软件可以帮助运维人员及时掌握服务状态变化趋势;当遇到突发流量增长时,则可通过容器编排平台实现快速扩容,从而保障用户体验不受影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值