用户登录
用户注册

分享至

SpringBoot内置tomcat调优测试优化

  • 作者: 最凉丨不过人心
  • 来源: 51数据库
  • 2021-09-04

问题

怎么配置springboot 内置tomcat,才能使得自己的服务效率更高呢?

基础配置

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

如上图 同样是100并发 异常率为0 全部通过,响应时间也是减除sleep(2000) 大多数都是10毫秒内。优化效果可见显著。

到此这篇关于springboot内置tomcat调优测试优化的文章就介绍到这了,更多相关springboot内置tomcat调优测试内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

软件
前端设计
程序设计
Java相关