From 5390da7ec54bf70cc58d54d7ee37749526bc18be Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 30 Jul 2020 12:33:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AF=B7=E6=B1=82=E4=BD=93?= =?UTF-8?q?=E8=B6=85=E8=BF=87256KB=E6=97=A0=E6=B3=95=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/ServerWebExchangeUtil.java | 7 +-- .../gateway/codec/MessageReaderFactory.java | 46 +++++++++++++++++++ .../manager/EnvironmentKeys.java | 11 ++++- 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/codec/MessageReaderFactory.java diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java index 502178ed..19ad75c0 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/ServerWebExchangeUtil.java @@ -3,6 +3,7 @@ package com.gitee.sop.gatewaycommon.gateway; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.gitee.sop.gatewaycommon.bean.SopConstants; +import com.gitee.sop.gatewaycommon.gateway.codec.MessageReaderFactory; import com.gitee.sop.gatewaycommon.gateway.common.FileUploadHttpServletRequest; import com.gitee.sop.gatewaycommon.gateway.common.RequestContentDataExtractor; import com.gitee.sop.gatewaycommon.gateway.common.SopServerHttpRequestDecorator; @@ -48,11 +49,7 @@ public class ServerWebExchangeUtil { private static final FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter(); - private static final List> messageReaders; - - static { - messageReaders = HandlerStrategies.withDefaults().messageReaders(); - } + private static final List> messageReaders = MessageReaderFactory.build(); /** * 重定向 diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/codec/MessageReaderFactory.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/codec/MessageReaderFactory.java new file mode 100644 index 00000000..95d6ff56 --- /dev/null +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/codec/MessageReaderFactory.java @@ -0,0 +1,46 @@ +package com.gitee.sop.gatewaycommon.gateway.codec; + +import com.gitee.sop.gatewaycommon.manager.EnvironmentKeys; +import org.springframework.core.codec.AbstractDataBufferDecoder; +import org.springframework.http.codec.HttpMessageReader; +import org.springframework.util.ReflectionUtils; +import org.springframework.web.reactive.function.server.HandlerStrategies; + +import java.lang.reflect.Method; +import java.util.List; + +/** + * @author tanghc + */ +public class MessageReaderFactory { + + public static final String METHOD_SET_MAX_IN_MEMORY_SIZE = "setMaxInMemorySize"; + public static final String METHOD_GET_DECODER = "getDecoder"; + public static final int DEFAULT_SIZE = 256 * 1024; + + public static List> build() { + String maxInMemorySizeValueStr = EnvironmentKeys.MAX_IN_MEMORY_SIZE.getValue(); + int maxInMemorySizeValue = Integer.parseInt(maxInMemorySizeValueStr); + List> messageReaders = HandlerStrategies.withDefaults().messageReaders(); + if (DEFAULT_SIZE == maxInMemorySizeValue) { + return messageReaders; + } + // 设置POST缓存大小 + for (HttpMessageReader httpMessageReader : messageReaders) { + Method[] methods = ReflectionUtils.getDeclaredMethods(httpMessageReader.getClass()); + for (Method method : methods) { + String methodName = method.getName(); + if (METHOD_SET_MAX_IN_MEMORY_SIZE.equals(methodName)) { + ReflectionUtils.invokeMethod(method, httpMessageReader, maxInMemorySizeValue); + } else if (METHOD_GET_DECODER.equals(methodName)) { + Object decoder = ReflectionUtils.invokeMethod(method, httpMessageReader); + if (decoder instanceof AbstractDataBufferDecoder) { + AbstractDataBufferDecoder bufferDecoder = (AbstractDataBufferDecoder) decoder; + bufferDecoder.setMaxInMemorySize(maxInMemorySizeValue); + } + } + } + } + return messageReaders; + } +} diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java index 3fa1b20f..890c8c6a 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java @@ -39,9 +39,16 @@ public enum EnvironmentKeys { /** * 预发布域名 */ - PRE_DOMAIN("pre.domain"); + PRE_DOMAIN("pre.domain"), - private String key; + /** + * post请求body缓存大小 + */ + MAX_IN_MEMORY_SIZE("spring.codec.max-in-memory-size", "262144") + + ; + + private final String key; private String defaultValue; public String getKey() {