坑#Spring Cloud Gateway#DataBufferLimitException

现象

POST请求数据量大(Content-Length: 305000+)的时候Gateway报错, 报错栈如下:

复制代码
org.springframework.core.io.buffer.DataBufferLimitException: Exceeded limit on max bytes to buffer : 262144
	at org.springframework.core.io.buffer.LimitedDataBufferList.raiseLimitException(LimitedDataBufferList.java:99)

尝试修改max-in-memory-size配置无效

原因

在Gateway配置文件routes节点中使用了CacheRequestBody

复制代码
public class CacheRequestBodyGatewayFilterFactory
       extends AbstractGatewayFilterFactory<CacheRequestBodyGatewayFilterFactory.Config> {

    private final List<HttpMessageReader<?>> messageReaders;

    public CacheRequestBodyGatewayFilterFactory() {
       super(CacheRequestBodyGatewayFilterFactory.Config.class);
       // 这块有问题
       this.messageReaders = HandlerStrategies.withDefaults().messageReaders();
    }
}

解决

方式一

参考: https://github.com/spring-cloud/spring-cloud-gateway/issues/1658

复制代码
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cloud.gateway.filter.factory.CacheRequestBodyGatewayFilterFactory;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;

@Bean
public BeanPostProcessor cacheRequestBodyGatewayFilterFactoryBeanPostProcessor() {
    return new BeanPostProcessor() {
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof CacheRequestBodyGatewayFilterFactory cacheFactoryBean) {
                // use java reflection to replace messageReaders in CacheRequestBodyGatewayFilterFactory
                // with message readers configured from CodecConfigurer
                Field messageReadersField = ReflectionUtils.findField(CacheRequestBodyGatewayFilterFactory.class,
                        "messageReaders");
                messageReadersField.setAccessible(true);
                ReflectionUtils.setField(messageReadersField, cacheFactoryBean, codecConfigurer.getReaders());
                messageReadersField.setAccessible(false);
                return cacheFactoryBean;
            }
            return bean;
        }
    };
}
方式二

参考: https://blog.csdn.net/fujiakai/article/details/134972127

重写org.springframework.core.codec.AbstractDataBufferDecoder类

  1. 在当前项目创建相同包名和类名文件
  2. 调整maxInMemorySize默认值大小即可
相关推荐
m0_740043731 天前
【无标题】
java·spring boot·spring·spring cloud·微服务
编程彩机1 天前
互联网大厂Java面试:从微服务到分布式缓存的技术场景解析
redis·spring cloud·消息队列·微服务架构·openfeign·java面试·分布式缓存
Anastasiozzzz2 天前
Nginx和Ribbon的区别
后端·spring cloud·ribbon
码农水水2 天前
从 OpenFeign 到 RestClient:Spring Cloud 新时代的轻量化 HTTP 调用方案
java·运维·后端·spring·http·spring cloud·面试
what丶k2 天前
SpringBoot3 配置文件使用全解析:从基础到实战,解锁灵活配置新姿势
java·数据库·spring boot·spring·spring cloud
小信丶2 天前
@Activate 注解详解:应用场景与实战示例
java·spring boot·后端·spring·spring cloud·微服务·dubbo
编程彩机2 天前
互联网大厂Java面试:从Spring MVC到微服务架构场景解析
java·spring cloud·微服务·分布式事务·spring mvc
鸽鸽程序猿3 天前
【JavaEE】【SpringCloud】 熔断和限流 Alibaba Sentinel
spring cloud·java-ee·sentinel
Roye_ack3 天前
【微服务 Day8】SpringCloud实战开发(Elasticsearch02 + DSL查询、聚合)
spring cloud·微服务·架构·dsl·聚合
努力也学不会java3 天前
【Spring Cloud】注册中心-Nacos
java·人工智能·spring boot·后端·spring·spring cloud