问题背景
DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
问题的产生原因很容易查到,REST API接口请求数据超过256K,被网关拦截。正常我们加大缓冲区的配置即可,-1表示不限制。但是并没有解决问题,下面介绍我的分析过程和解决办法。
spring:
codec:
max-in-memory-size: 1048576
异常信息
org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
at org.springframework.core.io.buffer.LimitedDataBufferList.raiseLimitException(LimitedDataBufferList.java:98) ~[spring-core-5.2.15.RELEASE.jar:5.2.15.RELEASE]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ HTTP POST "/ho-server/epc/collision/saveRecord" [ExceptionHandlingWebHandler]
Stack trace:
at org.springframework.core.io.buffer.LimitedDataBufferList.raiseLimitException(LimitedDataBufferList.java:98) ~[spring-core-5.2.15.RELEASE.jar:5.2.15.RELEASE]
at org.springframework.core.io.buffer.LimitedDataBufferList.updateCount(LimitedDataBufferList.java:91) ~[spring-core-5.2.15.RELEASE.jar:5.2.15.RELEASE]
at org.springframework.core.io.buffer.LimitedDataBufferList.add(LimitedDataBufferList.java:57) ~[spring-core-5.2.15.RELEASE.jar:5.2.15.RELEASE]
at reactor.core.publisher.MonoCollect$CollectSubscriber.onNext(MonoCollect.java:111) [reactor-core-3.3.17.RELEASE.jar:3.3.17.RELEASE]
at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2393) ~[reactor-core-3.3.17.RELEASE.jar:3.3.17.RELEASE]
at reactor.core.publisher.MonoCollect$CollectSubscriber.onSubscribe(MonoCollect.java:96) [reactor-core-3.3.17.RELEASE.jar:3.3.17.RELEASE]
解决办法
新增包名org.springframework.core.codec 覆盖 AbstractDataBufferDecoder方法
将maxInMemorySize 修改成需要的大小(项目上亲测有效)
private int maxInMemorySize = 5 * 1024 * 1024;代表能够缓存5M数据 默认是256kb