编程语言
首页 > 编程语言> > java – 在spring boot中增加tomcat的连接超时

java – 在spring boot中增加tomcat的连接超时

作者:互联网

如何增加超时以便在处理响应之前,请求不会超时?

Spring Boot中的Tomcat设置 –

server.tomcat.max-connections=2000
server.tomcat.max-threads=200
server.connection-timeout=1200000

在15秒期间,每秒请求在(15)期间将constantUsersPerSec(20)提升到300并且所有请求都被提供,如下面的图表中可以看到的那样(蓝色).

scn.inject(
      constantUsersPerSec(20) during (15), 
    )

这是由于max-connections = 2000,它使用200个工作线程提供300个请求.

Controller是用Spring MVC编写的,它返回DeferredResult,它执行异步请求处理,因此一旦处理响应就会恢复响应.

Requests Per Sec

但即使server.connection-timeout设置为高数字1200000,也有很多503朝向终点(红色)

> status.find.in(200,304,201,202,203,204,205,206,207,208,209), b     78 (100.0%)
ut actually found 503

Response Per Sec

Gatling.conf也设置为增加超时 –

   timeOut {
      simulation = 8640000 # Absolute timeout, in seconds, of a simulation
    }
    ahc {
      #keepAlive = true                                # Allow pooling HTTP connections (keep-alive header automatically added)
      connectTimeout = 600000                          # Timeout when establishing a connection
      handshakeTimeout = 600000                        # Timeout when performing TLS hashshake
      pooledConnectionIdleTimeout = 600000             # Timeout when a connection stays unused in the pool
      readTimeout = 600000                             # Timeout when a used connection stays idle
      #maxRetry = 2                                    # Number of times that a request should be tried again
      requestTimeout = 600000           

解决方法:

根据Rcordoval的评论 –

Check this property: spring.mvc.async.request-timeout= # Amount of
time before asynchronous request handling times out

此设置有助于其余的gatling配置

spring.mvc.async.request-timeout=1200000

然而,根本原因是当请求大量发生时,所有工作线程(200)在上传打开的连接(2000)时被占用(Controller将MultipartFile作为参数并返回DeferredResult)

我认为当请求服务逻辑很快且业务逻辑很慢(在forkjoin.commonPool上运行)时,DeferredResult会发光.它不太适合MultiPartFile上传(阻塞和慢速),当文件大小很大时更是如此,因为那时响应不会很快恢复(如上面的响应每秒图表所示,仅在几秒钟后响应开始恢复,因为打开连接2000年,工人只有200).如果工作人员增加,那么无论如何它都会减轻异步处理的优势.

在这种情况下,请求处理(上传和阻止)很慢,业务逻辑很快.所以响应已经准备就绪但是所有工作线程(200)都忙着提供越来越多的请求,响应没有得到恢复并因此超时.

可能会在DeferredResult的异步处理中为请求服务和响应恢复提供单独的池吗?

标签:java,spring,spring-boot,gatling,tomcat
来源: https://codeday.me/bug/20190910/1801317.html