坑#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默认值大小即可
相关推荐
Don.TIk5 小时前
ChapterOne-搭建项目骨架
java·spring·spring cloud·mybatis
摇滚侠13 小时前
SpringCloud 面试题 真正的 offer 偏方 Java 基础 Java 高级
java·spring·spring cloud
沪漂阿龙16 小时前
Spring Cloud 面试题深度解析:微服务架构、注册中心、配置中心、Gateway、OpenFeign、负载均衡、熔断降级全攻略
spring cloud·微服务·架构
Cosolar1 天前
吃透 Spring Cloud Gateway:基于 Spring Boot 3 的核心原理、企业级实战与避坑指南
java·spring cloud·架构
Ting-yu1 天前
Spring AI Alibaba零基础速成(3) ---- ChatClient使用
java·spring·spring cloud·spring ai
栀椩1 天前
Docker 命令速查手册
spring cloud·docker
huipeng9262 天前
基于SpringCloud的博客系统
java·运维·后端·spring·spring cloud·微服务
Devin~Y2 天前
大厂Java面试实录:Spring Boot/Cloud、JVM、Redis、Kafka、MyBatis 到 RAG/Agent 的三轮连环问(含答案详解)
java·jvm·spring boot·redis·spring cloud·kafka·mybatis
☞遠航☜2 天前
搭建基础的springcloud alibaba项目练习
后端·spring·spring cloud
Devin~Y3 天前
大厂Java面试实录:Spring Boot/Cloud + Redis + Kafka + JVM + RAG(Spring AI)三轮追问(小Y翻车版)
java·jvm·spring boot·redis·spring cloud·kafka·mybatis