Error: Exceeded limit on max bytes to buffer : 262144缓冲区异常问题解决记录

456 阅读1分钟

问题介绍

当业务的Http请求体大小超过256K时Spring Cloud Gateway抛出异常:org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144

是由于缓冲区默认最大设置为262144(256K)

image.png

解决方案1

在application.yml中配置缓冲区大小

image.png

但是由于HandlerStrategies.withDefaults() 是每次都需要重新创建对象,并非是spring注入的对象,所以每次获取的都是默认值,导致配置不生效。

所以可以参考ModifyRequestBodyGatewayFilterFactory调整自定义拦截器,使用注入的配置类。
代码如下

image.png

本方案参考 blog.csdn.net/lihaipeng04…

解决方案2  

重写缓冲区AbstractDataBufferDecoder源码自定义缓冲区大小

image.png

路径需与源码路径相同

image.png

根据需求修改缓冲区大小。