排查SpringCloudGateway的readAddress(..) failed: Connection reset by peer问题(利用arthas)

问题

[id:48b8a8f5-1, L:/网关:37187 - R:应用/应用:应用端口] The connection observed an error, the request cannot be retried as the headers/body were sent
io.netty.channel.unix.Errors$NativeIoException: readAddress(..) failed: Connection reset by peer
[2021-12-28T11:31:06,329] DEBUG  
 Stopping retries since predicate returned false, retry context: iteration=1 exception=io.netty.channel.unix.Errors$NativeIoException: readAddress(..) failed: Connection reset by peer backoff={0ms}
[2021-12-28T11:31:06,330] ERROR  
 ==>返回错误信息
io.netty.channel.unix.Errors$NativeIoException: readAddress(..) failed: Connection reset by peer
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
	|_ checkpoint ⇢ org.springframework.web.cors.reactive.CorsWebFilter [DefaultWebFilterChain]
	...
	|_ checkpoint ⇢ HTTP POST "接口" [ExceptionHandlingWebHandler]
Stack trace:

分析

大体一看,是netty报的io异常,然后被reactor-netty抛出到网关返回了。
reactor-netty是SCG的底层依赖,去
https://github.com/reactor/reactor-netty
发现有同样的issue。
大体总结就是

  1. 后端应用tomcat有个keepalivetimeout,默认与connecttimeout相同,为20s。也就是说闲置20s的连接会被断开丢弃,主要跟keepalive长连接有关。
  2. SCG网关有个连接池,存放与后端应用tomcat的通道连接,默认不释放回收,取用连接方式为FIFO先入先出(取用最早建立的连接)
  3. 以上两者,其中至少一个没有配置正确,导致SCG连接池释放闲置时间大于tomcat与网关的断开时间,就会导致浏览器向网关请求,网关获取到的连接是已经断开的连接,继续向tomcat请求,tomcat发现使用的是废弃的连接则返回reset,网关抛出异常,但后续接口不受影响。
    可简单参考下图。.在这里插入图片描述

解决

①配置SCG网关
spring:
  cloud:
    gateway:
      httpclient:
        response-timeout: 10s
        pool:
          type: fixed
          max-idle-time: 5000
          max-connections: 200
          acquire-timeout: 45000
②配置网关启动参数:reactor-netty取用连接的规则LIFO
-Dreactor.netty.pool.leasingStrategy=lifo
③配置tomcat的闲置断开时间
server:
  tomcat:
    connection-timeout: 10000 #根据需要
⑤配置nacos 元数据
spring: 
	cloud:
		nacos:
			discovery:
				metadata:
					response-timeout: 10000
					connect-timeout: 3000
④配合nginx关于keepalive的配置
upstream gateway {
#	ip_hash;
		server x.x.x.x:xx weight=1;
        server x.x.x.x:xx weight=1;
        keepalive 100;
        keepalive_requests 30000;
        keepalive_timeout 300s;
}

location /xxx{
proxy_pass http://gateway;
proxy_http_version 1.1;
proxy_set_header Connection "";
}

用arthas测试(其他)

使用arthas
https://arthas.aliyun.com/doc/index.html

curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar

附加到网关

 vmtool --action getInstances --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader --className  reactor.netty.resources.PooledConnectionProvider --express 'instances[0].defaultPoolFactory.leasingStrategy'


返回

@String[lifo]

则说明修改成功


👉推荐!!!【腾讯云】爆款2核4G云服务器首年74元/年
👉推荐!!!【腾讯云】1核2G5M轻量应用服务器50元/年
【腾讯云】云数据库低至9.9/年!MySQL7.4元/月
【阿里云】ECS云服务器特惠
【阿里云】服务器首购优惠
如果文章对您有帮助,扫个红包码呗

红包码

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
当出现java.io.IOException: Connection reset by peer错误时,这意味着与服务器的连接被对方重置了。这个错误通常是由以下几个原因引起的: 1. 网络问题:可能是由于网络中断或连接超时导致的。这种情况下,可以尝试检查网络连接是否正常,并确保没有任何代理或防火墙阻止了连接。 2. 服务器配置问题:有时服务器的配置可能导致连接被重置。例如,在NGINX中,如果proxy_buffer_size设置得太小,会导致连接被重置。此时,可以尝试增加proxy_buffer_size的大小,以避免连接被重置。 3. 服务器负载过高:如果服务器过载或处理请求的能力不足,可能会导致连接被重置。此时,可以尝试优化服务器的性能,例如增加服务器的处理能力或调整负载均衡策略,以减轻服务器的负载。 需要根据具体情况来确定解决方案,可以考虑检查网络连接、调整服务器配置或优化服务器性能来解决java.io.IOException: Connection reset by peer错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java.io.IOException: Connection reset by peer问题解决](https://blog.csdn.net/zcl111/article/details/102582578)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [java.io.IOException: Connection reset by peer 下载大图片报错](https://blog.csdn.net/weixin_41864667/article/details/129586929)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [报错:java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries....](https://download.csdn.net/download/weixin_42196279/15528823)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值