坑#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默认值大小即可
相关推荐
墨着染霜华10 小时前
SpringCloud Gateway线上Nginx代理后自定义下划线请求头丢失(app_name/app_version获取不到)完美解决
nginx·spring cloud·gateway
Rain的Java大神之路17 小时前
高并发下的热点账户余额扣减:Redis+Lua脚本实现无锁记账
java·spring boot·redis·后端·spring cloud·缓存·lua
YOU OU1 天前
优雅实现远程调用-OpenFeign
spring cloud
YOU OU1 天前
Spring Cloud概述
后端·spring·spring cloud
随遇而安zx1 天前
SpringCloud---可观测性与监控:Actuator / Micrometer / Prometheus / Grafana 深度解析
spring cloud·grafana·prometheus
LlmCraft|大模型工程实践2 天前
08. Docker Compose 一键编排:多容器应用的指挥家
java·spring cloud·eureka
橘子编程2 天前
Java邮件发送全攻略:从入门到实战
java·开发语言·spring boot·spring·spring cloud·maven
苏渡苇2 天前
HandlerInterceptor 和 WebFilter:两套 HTTP 请求拦截
spring boot·spring·http·spring cloud
随遇而安zx2 天前
SpringCloud---安全(Security / OAuth2 / JWT)设计思想与深度解析
安全·spring cloud
骇客野人2 天前
SpringBoot+SpringCloud高并发系统设计、搭建与落地实施方案
java·spring boot·spring cloud