Connection prematurely closed BEFORE response reactor.netty.http.client.PrematureCloseException: Co

旧城等待, 2022-11-05 03:04 120阅读 0赞
一、最近在开发网关系统,就在感觉万事大吉可以上线的时候发现了如下的错误(这个是我在配置rabbitmq访问多个服务时发现的)
  1. Connection prematurely closed BEFORE response
  2. reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response
  3. reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
  4. Error has been observed at the following site(s):
  5. |_ checkpoint org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
  6. |_ checkpoint org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
  7. |_ checkpoint HTTP GET "/rabbitmq/api/vhosts" [ExceptionHandlingWebHandler]

什么意思呢?就是在请求还未响应的时候连接直接断开了,什么情况,崩溃,但是问题还是要解决于是开始了度娘之路,来看下如下这张图:

在这里插入图片描述

由上图可知发生异常的原因是从连接池中拿到连接之后发送请求,请求还未到达目标服务器就已经被目标服务器关闭了;而SCG中的连接还未被回收掉;

二、解决方案

第一步添加JVM参数,更改从连接池中取连接的策略,由FIFO变更为LIFO(reactor.netty.resources.ConnectionProvider),确保拿到的连接永远是最新的连接;

  1. -Dreactor.netty.pool.leasingStrategy=lifo

第二步设置连接空闲多久后会被回收掉,这个时间要比对应服务的回收时间小(tomcat对应的是server.tomcat.connection-timeout属性配置,在浏览器中看到的就是keep-Alive),这样就可以确保SCG回收连接在后端服务之前进行,完美避开这个问题;

  1. spring:
  2. cloud:
  3. gateway:
  4. httpclient:
  5. pool:
  6. maxIdleTime: PT1S

GitHub地址:https://github.com/mingyang66/EmilyGateway/tree/main/doc

发表评论

表情:
评论列表 (有 0 条评论,120人围观)

还没有评论,来说两句吧...

相关阅读