SpringBoot项目414解决方案

背景描述

feign调用get接口时,gateway报错io.netty.handler.codec.http.TooLongHttpLineException: An HTTP line is larger than 4096 bytes。postman访问状态码为414

问题显而易见,就是url参数过长导致,可以将请求改为post,同时将employeeNums由url参数变更为body参数即可解决,但是由于这是一个已发版的业务服务feign调用另一个已发版的业务服务,贸然调整对已有的用户会有影响,同时该get请求参数会这么长的情况极少见,因此考虑能否针对出现问题的用户特殊处理下

问题追踪

控制台报错如下

2024-08-27 20:31:54.717  WARN 17744 --- [ctor-http-nio-5] r.n.http.server.HttpServerOperations     : [20858f13, L:/127.0.0.1:18080 - R:/127.0.0.1:58703] Decoding failed: FULL_REQUEST(decodeResult: failure(io.netty.handler.codec.http.TooLongHttpLineException: An HTTP line is larger than 4096 bytes.), version: HTTP/1.0, content: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 0, cap: 0))
GET /bad-request HTTP/1.0

IDEA捕获TooLongHttpLineException异常

访问问题接口,进入断点

发现this是LineParser

寻找其构造方法,定位到io.netty.handler.codec.http.HttpObjectDecoder#HttpObjectDecoder(int, int, int, boolean, boolean, int, boolean, boolean)

在构造函数处打断点,重新访问问题接口,向前跟踪

定位到reactor.netty.http.server.HttpServerConfig#configureHttp11Pipeline

可以看到取得是reactor.netty.http.HttpDecoderSpec#maxInitialLineLength(),进入HttpDecoderSpec,在设置值的入口打一个断点

重新访问接口,发现未访问设置值的方法,那么应该是在项目启动时装配的,重新启动项目,进入设置值方法,向前跟进

定位到org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizer#customizeRequestDecoder

可以到是从org.springframework.boot.autoconfigure.web.ServerProperties.Netty获取的maxInitialLineLength,进入org.springframework.boot.autoconfigure.web.ServerProperties,发现其前缀为server

因此maxInitialLineLength的值可以通过yml配置

配置后重新启动项目,发现已经被设置值了

但是Resume Program该断点后,会发现又进了一次断点,向前跟踪,定位至org.springframework.cloud.gateway.config.HttpClientFactory#httpResponseDecoder

会发现org.springframework.cloud.gateway.config.HttpClientProperties也设置了该值

那么就需要验证下是哪个配置生效了,分别在yml中配置不同的小于4096的值

放开断点,重新启动项目访问问题接口,可以看到控制台报错信息如下,重要信息为An HTTP line is larger than 1000 bytes.,可以看到netty的配置生效

2024-08-28 09:54:23.734  WARN 9092 --- [ctor-http-nio-3] r.n.http.server.HttpServerOperations     : [ed3268da, L:/127.0.0.1:18080 - R:/127.0.0.1:57331] Decoding failed: FULL_REQUEST(decodeResult: failure(io.netty.handler.codec.http.TooLongHttpLineException: An HTTP line is larger than 1000 bytes.), version: HTTP/1.0, content: UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 0, cap: 0))
GET /bad-request HTTP/1.0

在网上搜索了下,org.springframework.cloud.gateway.config.HttpClientProperties#setMaxInitialLineLength是Spring Cloud Gateway中用于配置HTTP客户端的属性,

org.springframework.boot.autoconfigure.web.ServerProperties.Netty#setMaxInitialLineLength:

是Spring Boot中用于配置Netty服务器的属性。稳妥起见,这两个字段的长度最好保持一致。

结论

yml文件中配置


spring:
  cloud:
    gateway:
      httpclient:
        max-initial-line-length: 40960

server:
  netty:
    max-initial-line-length: 40960

  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值