怎么在SpringBoot中调优tomcat?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
Spring Boot 能支持的最大并发量主要看其对Tomcat的设置,可以在配置文件中对其进行更改。我们可以看到默认设置中,Tomcat的最大线程数是200,最大连接数是10000。 这个不同SpringBoot 版本可能有所细微差别。本文测试基于Springboot 2.0.7.RELEASE
/**
* Maximum amount of worker threads.
*/
private int maxThreads = 200;
/**
* Minimum amount of worker threads.
*/
private int minSpareThreads = 10;
/**
* Maximum size in bytes of the HTTP post content.
*/
private int maxHttpPostSize = 2097152;
/**
* Maximum size in bytes of the HTTP message header.
*/
private int maxHttpHeaderSize = 0;
/**
* Whether requests to the context root should be redirected by appending a / to
* the path.
*/
private Boolean redirectContextRoot = true;
/**
* Whether HTTP 1.1 and later location headers generated by a call to sendRedirect
* will use relative or absolute redirects.
*/
private Boolean useRelativeRedirects;
/**
* Character encoding to use to decode the URI.
*/
private Charset uriEncoding = StandardCharsets.UTF_8;
/**
* 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.
*/
private int maxConnections = 10000;
/**
* Maximum queue length for incoming connection requests when all possible request
* processing threads are in use.
*/
private int acceptCount = 100;
通过我们查看源码得知了(org.springframework.boot.autoconfigure.web.ServerProperties)springBoot 内置tomcat 默认配置,现在我们为了在本地体现出效果,我们将配置参数有意调小配置如下进行压测,同时将压测接口中设置sleep(2000) 模拟线程没有释放。
tomcat:
#最小线程数
min-spare-threads: 5
#最大线程数
max-threads: 5
#最大链接数
max-connections: 5
#最大等待队列长度
accept-count: 1
该配置对应压测
通过压测100并发 发现异常达到了85% 由于我们配置ReadTimeout 和ConnectTimeout 配置2秒 100个线程同时达到,处理最大线程才1,排队也是1 导致一个是没有线程处理请求导致超时一个是排不上队别拒绝。当我按照本机cup 合理配置后看看压测情况。优化配置如下:
tomcat:
#最小线程数
min-spare-threads: 100
#最大线程数
max-threads: 600
#最大链接数
max-connections: 10000
#最大等待队列长度
accept-count: 1000
看完上述内容,你们掌握怎么在SpringBoot中调优tomcat的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。