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

这篇具有很好参考价值的文章主要介绍了解决GateWay报错:Exceeded limit on max bytes to buffer : 262144。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

网友的解决方案

方案1(无效)

直接修改缓冲区大小

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

方案2(无效)

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

方案3 无效

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

package org.springframework.cloud.gateway.handler.predicate;
...
public class ReadBodyPredicateFactory
		extends 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问题
     */
    @Autowired
    ServerCodecConfigurer codecConfigurer;
 
    @Override
    @SuppressWarnings("unchecked")
    public AsyncPredicate<ServerWebExchange> applyAsync(GwReadBodyPredicateFactory.Config config) {
...
        return new AsyncPredicate<ServerWebExchange>() {
            @Override
            public 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;
	}


	@Override
	public 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));
	}

	@Override
	public 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
	@Nullable
	protected T decodeDataBuffer(DataBuffer buffer, ResolvableType elementType,
			@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

		return decode(buffer, elementType, mimeType, hints);
	}

}

解决GateWay报错:Exceeded limit on max bytes to buffer : 262144,gateway文章来源地址https://www.toymoban.com/news/detail-758566.html

到了这里,关于解决GateWay报错:Exceeded limit on max bytes to buffer : 262144的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【IDEA启动项目报错java: java.lang.OutOfMemoryError: GC overhead limit exceeded解决方案】

    使用IDEA启动Spring Boot项目时,报内存溢出错误,导致服务启动失败: Error:java: java.lang.OutOfMemoryError: GC overhead limit exceeded 报此错说明启动期间内存不够用了,把idea的启动进程堆内存值设大点就行了。 设置窗口:Settings —Build,Execution,Deployment— Complier 把 build process heap size 值改

    2024年02月02日
    浏览(65)
  • 【爬虫 | Python】解决‘Requests Max Retries Exceeded With Url‘报错的问题

    我们在写爬虫的时候,经常会遇到这样的报错信息: HTTPConnectionPool(host=‘xxx.xxx.com’, port=443): Max retries exceeded with url: /api/v2/oauth (Caused by NewConnectionError(’urllib3.connection.HTTPConnection object at 0x7fac5953ab70: Failed to establish a new connection: [Errno 110] Connection timed out’)) 这里有3个重要的信

    2023年04月12日
    浏览(39)
  • requests.exceptions.SSLError: HTTPSConnectionPool Max retries exceeded with url 报错解决方法

    发现报错: 修改方法: 把 url 的 https 改为 http 不报错了

    2024年02月15日
    浏览(39)
  • torch.hub.load报错urllib.error.HTTPError: HTTP Error 403: rate limit exceeded

    在运行DINOv2的示例代码时,需要载入预训练的模型,比如: torch.hub.load报错“urllib.error.HTTPError: HTTP Error 403: rate limit exceeded”,具体报错信息如下: Traceback (most recent call last):   File \\\"/data1/domainnet/dinov2/demo.py\\\", line 15, in module     backbone_model = torch.hub.load(repo_or_dir=\\\"facebookresearch/

    2024年02月04日
    浏览(45)
  • vue报错Uncaught runtime errors: × ERROR ResizeObserver loop limit exceeded at handleError (webpack

    Uncaught runtime errors: × ERROR ResizeObserver loop limit exceeded at handleError (webpack-internal:///./node_modules/webpack-dev-server/client/overlay.js:252:58) at eval (webpack-internal:///./node_modules/webpack-dev-serve 问题原因: 使用了el-table组件+弹性布局 弹性布局 单独使用都不会报错,但是两个结合在一起就产生了

    2024年02月11日
    浏览(49)
  • doris查询报错err: Error 1105: errCode = 2, detailMessage = Memory limit exceeded:<consuming tracker:...

    为了防止用户的一个查询可能因为消耗内存过大。查询进行了内存控制,一个查询任务,在单个 BE 节点上默认使用不超过 2GB 内存。 用户在使用时,如果发现报 Memory limit exceeded 错误,一般是超过内存限制了。 遇到内存超限时,用户应该尽量通过优化自己的 sql 语句来解决。

    2024年02月10日
    浏览(41)
  • http请求报错SSLError: HTTPSConnectionPool:Max retries exceeded with url

    1、问题描述: 迭代请求http web服务,中途遇到异常报错: requests.exceptions.SSLError: HTTPSConnectionPool(host=\\\'xxx.com\\\', port=443): Max retries exceeded with url:xxx 2、问题排查 3、问题原因: http的连接数超过最大限制。默认的情况下连接是keep-alive的,所以导致服务器保持了太多连接而不能再新

    2024年02月12日
    浏览(49)
  • 【Vue】运行Vue项目时使用element-ui报错:ResizeObserver loop limit exceeded at eval...

    vue3项目使用element plus的el-table组件,在切换页面时报错 报错信息为: ResizeObserver loop limit exceeded at eval (webpack-internal:///./node_modules/webpack-dev-server/client/overlay.js:296:58) 如下图: 相关代码如下: 查阅相关解答之后,找到了比较可能的原因: 网友:Qyouu element-ui 自2.3.5版本后,Ta

    2024年02月11日
    浏览(78)
  • java.lang.OutOfMemoryError: GC overhead limit exceeded问题分析及解决

    出现该问题的原因:当GC为释放很小空间占用大量时间时会抛出此异常,即(Sun 官方对此的定义,超过98%的时间用来做GC并且回收了不到2%的堆内存时会抛出此异常)。一般是因为堆太小,导致异常的原因:没有足够的内存。 对于该项目我的启动命令如下:堆内存空间开辟的

    2024年01月21日
    浏览(52)
  • Spark写数据到Doris报错node and exceeded the max retry times

    用spark dataframe向doris写数据时,报下面错误: Failed to load data on BE: http://192.168.50.10:18040/api/mydb/dwd_virtual_table/_stream_load? node and exceeded the max retry times. 发现表没写入成功。刚开始很困惑,后来发现是 dataFrame中的字段和目标表不一致 。 这种提示很不友好,有没有更好方式提示,

    2024年02月11日
    浏览(58)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包