Spring Cloud Gateway转发下游API请求存在延迟问题求助
Spring Cloud Gateway 两大问题排查与解决
问题背景
已配置Spring Cloud Gateway转发请求至下游API,但存在两个核心问题:
- 部分请求转发耗时超5秒,导致依赖服务因超时返回错误,最终出现
java.io.IOException: An existing connection was forcibly closed by the remote host错误 - 仅配置了POST请求路由,但仍有GET请求到达网关,需拦截此类请求
已排除服务器资源负载问题,调大JVM堆内存参数无效。
原始错误日志
2024-03-12 01:21:12.174 DEBUG 7104 --- [tor-http-nio-13] reactor.netty.http.server.HttpServer : [5b39460c-1, L:/Local IP:8058 - R:/10.216.130.23:14454] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@2bd948e4 2024-03-12 01:21:12.174 DEBUG 7104 --- [tor-http-nio-13] o.s.w.s.adapter.HttpWebHandlerAdapter : [5b39460c-2530] HTTP POST "/wallet/v1/wallet/qr/payment/auth" 2024-03-12 01:21:12.174 INFO 7104 --- [tor-http-nio-13] c.d.epiCloudproxy.LoggingFilter : Routing Incoming request orginal url Local API Endpoint route to url: api endpoint, 2024-03-12 01:21:12.174 DEBUG 7104 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider : [c5448fc1, L:/Local IP:54766 - R:Remote IP:9480] Channel acquired, now: 1 active connections, 0 inactive connections and 0 pending acquire requests. 2024-03-12 01:21:12.174 DEBUG 7104 --- [ctor-http-nio-1] r.netty.http.client.HttpClientConnect : [c5448fc1-2, L:/Local IP:54766 - R:Remote IP:9480] Handler is being applied: {uri=api endpoint, method=POST} 2024-03-12 01:21:12.174 DEBUG 7104 --- [ctor-http-nio-1] r.n.r.DefaultPooledConnectionProvider : [c5448fc1-2, L:/Local IP:54766 - R:Remote IP:9480] onStateChange(POST{uri=/v1/wallet/qr/payment/auth, connection=PooledConnection{channel=[id: 0xc5448fc1, L:/Local IP:54766 - R:Remote IP:9480]}}, [request_prepared]) 2024-03-12 01:21:12.174 DEBUG 7104 --- [tor-http-nio-13] reactor.netty.channel.FluxReceive : [5b39460c-1, L:/Local IP:8058 - R:/10.216.130.23:14454] [terminated=true, cancelled=false, pending=1, error=null]: subscribing inbound receiver 2024-03-12 01:21:12.174 DEBUG 7104 --- [ctor-http-nio-1] r.n.r.DefaultPooledConnectionProvider : [c5448fc1-2, L:/Local IP:54766 - R:Remote IP:9480] onStateChange(POST{uri=/v1/wallet/qr/payment/auth, connection=PooledConnection{channel=[id: 0xc5448fc1, L:/Local IP:54766 - R:Remote IP:9480]}}, [request_sent]) 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] reactor.netty.tcp.SslProvider : [0510b3b7, L:/Local IP:8058 - R:/10.216.130.22:55058] SSL enabled using engine 410137f5[SSLEngine[hostname=null port=-1] SSL_NULL_WITH_NULL_NULL] 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] r.n.http.server.HttpServerOperations : [0510b3b7, L:/Local IP:8058 - R:/10.216.130.22:55058] New http connection, requesting read 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] reactor.netty.transport.TransportConfig : [0510b3b7, L:/Local IP:8058 - R:/10.216.130.22:55058] Initialized pipeline DefaultChannelPipeline{(reactor.left.sslHandler = io.netty.handler.ssl.SslHandler), (reactor.left.sslReader = reactor.netty.tcp.SslProvider$SslReadHandler), (reactor.left.httpCodec = io.netty.handler.codec.http.HttpServerCodec), (reactor.left.httpTrafficHandler = reactor.netty.http.server.HttpTrafficHandler), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)} 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] r.n.http.server.HttpServerOperations : [0510b3b7, L:/Local IP:8058 - R:/10.216.130.22:55058] Increasing pending responses, now 1 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] reactor.netty.http.server.HttpServer : [0510b3b7-1, L:/Local IP:8058 - R:/10.216.130.22:55058] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@2bd948e4 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] o.s.w.s.adapter.HttpWebHandlerAdapter : [0510b3b7-2531] HTTP GET "/" 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] o.s.w.r.handler.SimpleUrlHandlerMapping : [0510b3b7-2531] Mapped to ResourceWebHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/]] 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] o.s.w.r.resource.ResourceWebHandler : [0510b3b7-2531] Resource not found 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] org.springframework.web.HttpLogging : [0510b3b7-2531] Resolved [ResponseStatusException: 404 NOT_FOUND] for HTTP GET / 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] org.springframework.web.HttpLogging : [0510b3b7-2531] Encoding [{timestamp=Tue Mar 12 01:21:21 GST 2024, path=/, status=404, error=Not Found, message=null, requestI (truncated)...] 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] r.n.http.server.HttpServerOperations : [0510b3b7-1, L:/Local IP:8058 - R:/10.216.130.22:55058] Detected non persistent http connection, preparing to close 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] r.n.http.server.HttpServerOperations : [0510b3b7-1, L:/Local IP:8058 - R:/10.216.130.22:55058] Last HTTP packet was sent, terminating the channel 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] reactor.netty.channel.ChannelOperations : [0510b3b7-1, L:/Local IP:8058 - R:/10.216.130.22:55058] [HttpServer] Channel inbound receiver cancelled (operation cancelled). 2024-03-12 01:21:21.940 DEBUG 7104 --- [tor-http-nio-14] o.s.w.s.adapter.HttpWebHandlerAdapter : [0510b3b7-2531] Completed 404 NOT_FOUND 2024-03-12 01:21:31.128 WARN 7104 --- [ctor-http-nio-1] r.netty.http.client.HttpClientConnect : [c5448fc1-2, L:/Local IP:54766 - R:Remote IP:9480] The connection observed an error
当前配置文件
server: ssl: key-store: cert.p12 key-store-password: pw key-alias: alias key-store-type: PKCS12 port: 8058 logging: level: reactor: netty: DEBUG org: springframework: web: DEBUG spring: cloud: gateway: httpclient: proxy: port: 9480 host: cloudproxy routes: - id: test-route uri: Remote_uri predicates: - Path=/reqbin/** filters: - RewritePath=/reqbin/(?<path>.*), /$\{path} - id: fill_go_routes uri: Remote_uri predicates: - Path=/fill-go/** filters: - RewritePath=/fill-go/(?<path>.*), /$\{path} - id: qr_wallet_routes uri: Remote_uri predicates: - Path=/wallet/** filters: - RewritePath=/wallet/(?<path>.*), /$\{path}
解决方案
一、解决请求延迟与连接被强制关闭问题
从日志可见,POST请求发送后约19秒才触发连接错误,核心原因是网关未配置合理的超时参数,导致请求等待至被代理或下游服务强制断开。
- 添加HttpClient超时配置
在spring.cloud.gateway.httpclient下补充超时参数,与依赖服务的5秒超时保持一致:
spring: cloud: gateway: httpclient: connect-timeout: 5000 # 连接超时5秒 response-timeout: 5000 # 响应超时5秒 pool: max-idle-time: 10000 # 连接池最大空闲时间10秒 max-life-time: 30000 # 连接最大存活时间30秒 max-connections: 200 # 根据业务并发量调整最大连接数
此配置能避免请求因等待过久被强制关闭,同时优化连接池复用效率。
- 排查代理与下游服务网络
- 检查代理服务器到下游API的网络链路是否存在丢包、延迟过高问题
- 确认下游服务的超时配置,确保网关超时不超过下游服务的超时阈值
- 若下游服务偶尔超时,可添加重试过滤器(注意避免重复提交幂等性问题):
filters: - name: Retry args: retries: 2 methods: POST statuses: BAD_GATEWAY, SERVICE_UNAVAILABLE
二、拦截未配置的GET请求
当前路由仅通过Path匹配,未限制请求方法,导致GET请求会被默认资源处理器处理。可通过两种方式拦截:
- 给现有路由添加Method断言
修改每个路由的predicates,仅允许POST请求匹配:
- id: qr_wallet_routes uri: Remote_uri predicates: - Path=/wallet/** - Method=POST # 仅接受POST请求 filters: - RewritePath=/wallet/(?<path>.*), /$\{path}
未匹配的GET请求会返回404或被后续路由处理。
- 全局拦截所有GET请求
添加优先级最高的路由,直接拒绝所有GET请求:
spring: cloud: gateway: routes: - id: block-all-get uri: no://op predicates: - Method=GET filters: - name: SetStatus args: status: 403 # 返回403禁止访问 # 现有路由放在后面 - id: test-route uri: Remote_uri ...
注意此路由需放在所有路由最前面,Spring Cloud Gateway会按顺序匹配路由。
内容的提问来源于stack exchange,提问作者Hussain lala
相关产品推荐
相关产品推荐

