解决GateWay报错:Exceeded limit on max bytes to buffer : 262144

场景: 前端传来了一个大的字符串 发现请求不通 一番调试发现SpringGateway 默认内存缓冲区262144字节
网上查了很多种常见的解决方案无效之后 直接重写底层

网友的解决方案

方案1(无效)

直接修改缓冲区大小

spring:codec:max-in-memory-size: 1048576

方案2(无效)

方案3 无效

gateway-2.2.3以上版本修复了该bug,在GatewayAutoConfiguration中加入了配置写入,但只限ReadBodyPredicateFactory类,如自定义类型需要使用方案二。

package org.springframework.cloud.gateway.handler.predicate;
...
public class ReadBodyPredicateFactoryextends AbstractRoutePredicateFactory<ReadBodyPredicateFactory.Config> {
...private final List<HttpMessageReader<?>> messageReaders;public ReadBodyPredicateFactory() {super(Config.class);this.messageReaders = HandlerStrategies.withDefaults().messageReaders();}/*** GatewayAutoConfiguration初始化配置写入相关配置*/public ReadBodyPredicateFactory(List<HttpMessageReader<?>> messageReaders) {super(Config.class);this.messageReaders = messageReaders;}
...}

方案4(无效)

重写ReadBodyPredicateFactory,注入ServerCodecConfigurer,使用ServerCodecConfigurer.getReaders()获取相关配置。

...
/*** @description: 自定义ReadBodyPredicateFactory,copy之ReadBodyPredicateFactory* @author: lizz* @date: 2020/6/8 14:22*/
@Component
public class GwReadBodyPredicateFactory extends AbstractRoutePredicateFactory<GwReadBodyPredicateFactory.Config> {/*** 获取Spring配置,解决最大body问题*/@AutowiredServerCodecConfigurer codecConfigurer;@Override@SuppressWarnings("unchecked")public AsyncPredicate<ServerWebExchange> applyAsync(GwReadBodyPredicateFactory.Config config) {
...return new AsyncPredicate<ServerWebExchange>() {@Overridepublic Publisher<Boolean> apply(ServerWebExchange exchange) {return ServerWebExchangeUtils.cacheRequestBodyAndRequest(exchange,(serverHttpRequest) -> ServerRequest.create(exchange.mutate().request(serverHttpRequest).build(), codecConfigurer.getReaders()).bodyToMono(inClass).doOnNext(objectValue -> exchange.getAttributes().put(CACHE_REQUEST_BODY_OBJECT_KEY, objectValue)).map(objectValue -> config.getPredicate().test(objectValue)));
...
}

方案5 有效

直接重写SpringGateway底层处理请求请求的 

org.springframework.core.codec.AbstractDataBufferDecoder类
/** Copyright 2002-2019 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.core.codec;import java.util.Map;import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.MimeType;/*** 重写SpringWebFex底层 为了解决GateWay报错:Exceeded limit on max bytes to buffer : 262144 错误* Abstract base class for {@code Decoder} implementations that can decode* a {@code DataBuffer} directly to the target element type.** <p>Sub-classes must implement {@link #decodeDataBuffer} to provide a way to* transform a {@code DataBuffer} to the target data type. The default* {@link #decode} implementation transforms each individual data buffer while* {@link #decodeToMono} applies "reduce" and transforms the aggregated buffer.** <p>Sub-classes can override {@link #decode} in order to split the input stream* along different boundaries (e.g. on new line characters for {@code String})* or always reduce to a single data buffer (e.g. {@code Resource}).** @author Rossen Stoyanchev* @since 5.0* @param <T> the element type*/
@SuppressWarnings("deprecation")
public abstract class AbstractDataBufferDecoder<T> extends AbstractDecoder<T> {private int maxInMemorySize = 256 * 1024*10;protected AbstractDataBufferDecoder(MimeType... supportedMimeTypes) {super(supportedMimeTypes);}/*** Configure a limit on the number of bytes that can be buffered whenever* the input stream needs to be aggregated. This can be a result of* decoding to a single {@code DataBuffer},* {@link java.nio.ByteBuffer ByteBuffer}, {@code byte[]},* {@link org.springframework.core.io.Resource Resource}, {@code String}, etc.* It can also occur when splitting the input stream, e.g. delimited text,* in which case the limit applies to data buffered between delimiters.* <p>By default this is set to 256K.* @param byteCount the max number of bytes to buffer, or -1 for unlimited* @since 5.1.11*/public void setMaxInMemorySize(int byteCount) {this.maxInMemorySize = byteCount;}/*** Return the {@link #setMaxInMemorySize configured} byte count limit.* @since 5.1.11*/public int getMaxInMemorySize() {return this.maxInMemorySize;}@Overridepublic Flux<T> decode(Publisher<DataBuffer> input, ResolvableType elementType,@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {return Flux.from(input).map(buffer -> decodeDataBuffer(buffer, elementType, mimeType, hints));}@Overridepublic Mono<T> decodeToMono(Publisher<DataBuffer> input, ResolvableType elementType,@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {return DataBufferUtils.join(input, this.maxInMemorySize).map(buffer -> decodeDataBuffer(buffer, elementType, mimeType, hints));}/*** How to decode a {@code DataBuffer} to the target element type.* @deprecated as of 5.2, please implement* {@link #decode(DataBuffer, ResolvableType, MimeType, Map)} instead*/@Deprecated@Nullableprotected T decodeDataBuffer(DataBuffer buffer, ResolvableType elementType,@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {return decode(buffer, elementType, mimeType, hints);}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/219672.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【STM32】STM32学习笔记-LED闪烁 LED流水灯 蜂鸣器(06-2)

00. 目录 文章目录 00. 目录01. GPIO之LED电路图02. GPIO之LED接线图03. LED闪烁程序示例04. LED闪烁程序下载05. LED流水灯接线图06. LED流水灯程序示例07. 蜂鸣器接线图08. 蜂鸣器程序示例09. 下载10. 附录 01. GPIO之LED电路图 电路图示例1 电路图示例2 02. GPIO之LED接线图…

持续集成交付CICD:Jenkins使用GitLab共享库实现自动上传前后端项目Nexus制品

目录 一、实验 1.GitLab本地导入前后端项目 2.Jenkins新建前后端项目流水线 3.Sonarqube录入质量阈与质量配置 4.修改GitLab共享库代码 5.Jenkins手动构建前后端项目流水线 6.Nexus查看制品上传情况 7.优化代码获取RELEASE分支 8.优化Jenkins流水线项目名称 一、实验 …

计算机网络:数据链路层(网桥)

带你速通计算机网络期末 目录 一、冲突域和广播域 二、网桥介绍 三、网桥分类—―透明网桥 四、网桥分类―—源路由网桥 五、多接口网桥―—以太网交换机 总结 一、冲突域和广播域 冲突域:在同一个冲突域中的每一个节点都能收到所有被发送的帧。简单的说就是同一时间内只…

华为数通---配置基本QinQ示例

QinQ简介 定义 QinQ&#xff08;802.1Q-in-802.1Q&#xff09;技术是一项扩展VLAN空间的技术&#xff0c;通过在802.1Q标签报文的基础上再增加一层802.1Q的Tag来达到扩展VLAN空间的功能&#xff0c;可以使私网VLAN透传公网。由于在骨干网中传递的报文有两层802.1Q Tag&#x…

MySQL之DQL语句

DQL语句 DQL&#xff08;Data Query Language&#xff09;查询数据 操作查询&#xff1a;select简单的查询&#xff0c;复杂的查询数据库中最核心的语言&#xff0c;最重要的语句使用频繁的语句 指定查询 查询全部 语法&#xff1a; select 全部字段&#xff08;*&#x…

什么是tomcat?tomcat是干什么用的?

目录 Tomcat 的主要用途包括&#xff1a; 托管Java Web应用程序&#xff1a; Servlet 容器&#xff1a; 以下是关于Servlet容器的一些关键特性和功能&#xff1a; 生命周期管理&#xff1a; 多线程支持&#xff1a; HTTP请求处理&#xff1a; HTTP响应生成&#xff1a;…

金融众筹系统源码:适合创业孵化机构 附带完整的搭建教程

互联网技术的发展&#xff0c;金融众筹作为一种新型的融资方式&#xff0c;逐渐成为创业孵化机构的重要手段。为了满足这一需求&#xff0c;金融众筹系统源码就由此而生&#xff0c;并附带了完整的搭建教程。 以下是部分代码示例&#xff1a; 系统特色功能一览&#xff1a; 1.…

不可变对象设计模式

共享的资源&#xff0c;是指在多个线程同时对其访问的情况下&#xff0c;各个线程都会使其发生变化&#xff0c;线程安全的目的是在受控的并发访问中防止数据发生变化。除了使用synchronized关键字同步对资源的写操作之外&#xff0c;还可以在线程间不共享资源状态&#xff0c;…

《从入门到精通:AJAX基础知识解析,前端开发中利器》基础篇

目录 学习目标&#xff1a; 学习目录&#xff1a; 学习时间&#xff1a; 学习内容&#xff1a; 什么是 AJAX&#xff1f; 怎么用 AJAX &#xff1f; 认识 URL 协议 域名 资源路径 获取 - 新闻列表 URL 查询参数 axios&#xff0d;查询参数 常用请求方法和数据提…

Jenkins离线安装部署教程简记

前言 在上一篇文章基于Gitee实现Jenkins自动化部署SpringBoot项目中&#xff0c;我们了解了如何完成基于Jenkins实现自动化部署。 对于某些公司服务器来说&#xff0c;是不可以连接外网的&#xff0c;所以笔者专门整理了一篇文章总结一下&#xff0c;如何基于内网直接部署Jen…

Python OpenCV将32位图像改为8位图像

将32位图像改为8位 背景代码 背景 图片格式为32位图像&#xff0c;需要将它改为8位图像&#xff0c;找了很多博客&#xff0c;说的方法五花八门&#xff0c;基本都不行&#xff0c;现在提供一种方式&#xff0c;能够实现这个功能。 代码 // An highlighted block import os …

网络安全知识点总结

网络安全是确保计算机网络免受未经授权的访问、攻击、破坏、更改或泄露的一系列措施和技术的综合体。以下是关于网络安全的更详细的知识点总结&#xff1a; 防火墙和边界安全: 实施防火墙以监控和控制网络流量。边界防御策略包括网络地址转换&#xff08;NAT&#xff09;和端口…

ELADMIN - 免费开源 admin 后台管理系统,基于 Spring Boot 和 Vue ,包含前端和后端源码

一款简单好用、功能强大的 admin 管理系统&#xff0c;包含前端和后端源码&#xff0c;分享给大家。 ELADMIN 是一款基于 Spring Boot、Jpa 或 Mybatis-Plus、 Spring Security、Redis、Vue 的前后端分离的后台管理系统。 ELADMIN 的作者在 Github 和 Gitee 上看了很多的项目&…

【C++】模板具体化、实例化、特化整理总结

文章目录 模板具体化实例化(隐式实例化)显式实例化显式具体化(特化)具体化&#xff08;全特化&#xff09;部分具体化&#xff08;部分特化、偏特化&#xff09; 模板的具体化分为隐式实例化&#xff09;、显式实例化和显式具体化&#xff09;。 模板以泛型的方式描述函数&…

【密码学】RSA破解方法汇总(PYTHON实现)

源自于密码学的一次大作业~ RSA破解 &#x1f4a1; Alice使用的RSA密码体制&#xff0c;有以下事项需要说明&#xff1a; 1&#xff09; 模数&#x1d441;&#x1d45d;&#x1d45e;规模为1024比特&#xff0c;其中&#x1d45d;&#xff0c;&#x1d45e;为素数&#xff1…

Java项目学生管理系统六后端补充

班级管理 1 班级列表&#xff1a;后端 编写JavaBean【已有】编写Mapper【已有】编写Service编写controller 编写Service 接口 package com.czxy.service;import com.czxy.domain.Classes;import java.util.List;/*** author 桐叔* email liangtongitcast.cn* description*/ p…

【Jenkins】Centos环境安装Jenkins(通过rpm安装)

在Centos操作系统中通过rpm安装Jenkins 参考官网 https://www.jenkins.io/doc/book/installing/linux/#red-hat-centos 1、下载安装Jdk17 下载安装 # 更新您的系统&#xff0c;不一定需要 # sudo yum -y update # 安装将用于下载 Java 17 二进制文件的 wget 命令行工具。 s…

计算机网络传输层(期末、考研)

计算机网络总复习链接&#x1f517; 目录 传输层的功能端口UDP协议UDP数据报UDP的首部格式UDP校验 TCP协议&#xff08;必考&#xff09;TCP报文段TCP连接的建立TCP连接的释放TCP的可靠传输TCP的流量控制零窗口探测报文段 TCP的拥塞控制慢开始和拥塞控制快重传和快恢复 TCP和U…

薅github的羊毛-用pages建自己的博客或静态资源站 - 1/2

注册帐号 准备邮箱注册帐号&#xff0c;在注册界面输入用户名、邮箱及密码完成注册。 添加图片注释&#xff0c;不超过 140 字&#xff08;可选&#xff09; 1. 在注册成过程中&#xff0c;会往邮箱发送验证码&#xff0c;请如实填写即可 2. 验证码没错的话&#xff0c;就代…

AUTOSAR从入门到精通-面向服务的中间件SOME/IP(一)

目录 前言 几个相关概念 几个高频面试题目 SOME/IP 与 CAN 的不同? 通信速度