坑#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默认值大小即可
相关推荐
后端小张16 分钟前
【JAVA 进阶】深入理解Sentinel:分布式系统的流量守卫者
java·开发语言·spring boot·后端·spring·spring cloud·sentinel
2024暴富24 分钟前
SpringBoot基于Mybatis拦截器实现数据权限(图文)
spring boot·spring cloud·mybatis
Mr.Pascal1 天前
深度解读一下 springcloud 的 pom.xml 用到的标签
xml·spring boot·spring cloud
Roye_ack1 天前
【微服务 Day1】SpringCloud实战开发(Mybatis-plus + Docker)
spring cloud·docker·微服务·mybatis
梵得儿SHI1 天前
SpringCloud 核心组件精讲:OpenFeign 实战指南-服务调用优雅实现方案(含自定义拦截器、超时重试、LoadBalance 整合避坑)
spring boot·spring·spring cloud·负载均衡·openfeign的核心应用·微服务调用·熔断组件
YDS8291 天前
SpringCloud —— 配置管理
java·spring·spring cloud
后端小张1 天前
【JAVA 进阶】深入拆解SpringBoot自动配置:从原理到实战的完整指南
java·开发语言·spring boot·后端·spring·spring cloud·springboot
serendipity_hky1 天前
【SpringCloud | 第5篇】Seata分布式事务
分布式·后端·spring·spring cloud·seata·openfeign
C182981825752 天前
restTemplate/Feign(Spring Cloud)或OKHttp Apache HttpClient 这几个关系与底层实现
spring cloud·okhttp·apache
沉浮yu大海2 天前
基于SpringBoot3+Java17+Nacos的配置中心和本地配置文件加解密
java·spring cloud·nacos·java17