坑#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默认值大小即可
相关推荐
b***46248 小时前
IoT DC3 是一个基于 Spring Cloud 的开源的、分布式的物联网(IoT)平台本地部署步骤
物联网·spring cloud·开源
稚辉君.MCA_P8_Java16 小时前
Gemini永久会员 containerd部署java项目 kubernetes集群
后端·spring cloud·云原生·容器·kubernetes
q***518918 小时前
SpringCloud系列教程:微服务的未来(十四)网关登录校验、自定义过滤器GlobalFilter、GatawayFilter
java·spring cloud·微服务
q***721920 小时前
springBoot 和springCloud 版本对应关系
spring boot·后端·spring cloud
q***385120 小时前
SpringCloud实战【九】 SpringCloud服务间调用
java·spring boot·spring cloud
q***49451 天前
SpringGateway网关(Spring Gateway是Spring自己编写的,也是SpringCloud中的组件)
spring·spring cloud·gateway
我是小妖怪,潇洒又自在1 天前
springcloud alibaba搭建
后端·spring·spring cloud
l***74941 天前
springboot与springcloud对应版本
java·spring boot·spring cloud
摇滚侠1 天前
2025最新 SpringCloud 教程,Nacos-总结,笔记19
java·笔记·spring cloud
阿拉斯攀登1 天前
Spring Cloud Gateway 的内置路由过滤器使用
spring cloud·微服务·gateway