spring gateway 截取response 长度缺失
网上找到一段获取修改response的代码:https://blog.csdn.net/tianyaleixiaowu/article/details/83618037
代码如下:
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import java.nio.charset.Charset; /**
* @author wuweifeng wrote on 2018/10/31.
*/
@Component
public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
@Override
public int getOrder() {
// -1 is response write filter, must be called before that
return -2;
} @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpResponse originalResponse = exchange.getResponse();
DataBufferFactory bufferFactory = originalResponse.bufferFactory();
ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (body instanceof Flux) {
Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
return super.writeWith(fluxBody.map(dataBuffer -> {
// probably should reuse buffers
byte[] content = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(content);
//释放掉内存
DataBufferUtils.release(dataBuffer);
String s = new String(content, Charset.forName("UTF-8"));
//TODO,s就是response的值,想修改、查看就随意而为了
byte[] uppedContent = new String(content, Charset.forName("UTF-8")).getBytes();
return bufferFactory.wrap(uppedContent);
}));
}
// if body is not a flux. never got there.
return super.writeWith(body);
}
};
// replace response with decorator
return chain.filter(exchange.mutate().response(decoratedResponse).build());
}
}
实践后发现,在多次测试或者压力测试时,会间歇性出现问题。
问题在这里
  public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
                if (body instanceof Flux) {
                    Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
                    return super.writeWith(fluxBody.map(dataBuffer -> {
                        // probably should reuse buffers
                        byte[] content = new byte[dataBuffer.readableByteCount()];
这段代码中,重写了response的构造方法,将body映射为需要的byte[]以便于后面操作
但是这里的dataBuffer经常取到的长度低于实际长度,丢失了内容。
还没找到内容,先换一种方法实现,待续...
spring gateway 截取response 长度缺失的更多相关文章
- spring cloud gateway获取response body
		
网关发起请求后,微服务返回的response的值要经过网关才发给客户端.本文主要讲解在spring cloud gateway 的过滤器中获取微服务的返回值,因为很多情况我们需要对这个返回进行处理.网 ...
 - asp.net截取指定长度的字符串内容
		
/// <summary> /// 用于截取指定长度的字符串内容 /// </summary> /// <param name="sString"&g ...
 - .net截取指定长度汉字超出部分以指定的字符代替
		
下面是我在网上搜索,然后加以整理的关于在.net中截取指定长度汉字超出部分以指定的字符代替,来拓展一下自己的思路. 方法一 :在后台的select语句中直接操作或是在数据库中写一个存储过程 Selec ...
 - js实现超过长度的字符截取指定长度(中文字符算2个字符),超出部分以...显示
		
//超过长度的字符截取指定长度,超出部分以...显示 function subString(str, len) { var newLength = 0; var newStr = "&quo ...
 - 解决 ASP.NET Core MySql varchar 字符串截取(长度 255)
		
ASP.NET Core 中使用 MySql,如果字段类型为varchar,不管设置多少长度,插入或更新数据的时候,会自动截断(截取 255 长度的字符). 出现问题的原因,就是使用了MySql.Da ...
 - DEDE中如何过滤掉Html标签,并且截取字符串长度
		
在dede标签中只要使用2个函数就可以. [field:body function="cn_substr(Html2text(@me),80)"/] Html2text()函数是去 ...
 - sql中从指定位置截取指定长度字符串
		
1. 字符串函数应用 --从指定索引截取指定长度的字符串 ,) --获取字符串中指定字符的索引(从1开始) select charindex(',','ab,cdefg') --实际应用中的语句 , ...
 - Oracle 截取指定长度的字符
		
去掉回车,换行符号,截取指定长度的字符 具体代码示例: --Function --去掉前后空格,截取字符,字符长度为P_Length create or replace function get_St ...
 - 用C#截取指定长度的中英文混合字符串
		
很早以前写过一篇文章(用C#截取指定长度的中英文混合字符串),但是对性能没有测试,有人说我写的这个方法性能有问题,后来想,可能真会有BT之需求要求传入一个几万K甚至几M体积的字符串进来,那将会影响正则 ...
 
随机推荐
- 因数表进阶:1--x的因数和
			
紧接着上一个文章,进阶一个因数表,来自牛客网一道比赛题: 打从1到n所有因数的和 代码如下: #include<cstdio> #define ll long long using nam ...
 - 详解~实现Runnable方法创建线程之为什么要将Runnable接口的子类对象传递给Thread的构造函数
			
/** * @author zhao * @TIME 0419 22:56 End *定义线程的第二种方法:实现Runnable接口(不考虑安全问题) *步骤:1,定义一个子类实现Runnable接口 ...
 - (转)为什么wait(),notify()和notifyAll()必须在同步块或同步方法中调用
			
我们常用wait(),notify()和notifyAll()方法来进行线程间通信.线程检查一个条件后就行进入等待状态,例如,在“生产者-消费者”模型中,生产者线程发现缓冲区满了就等待,消费者线程通过 ...
 - BZOJ1515 : [POI2006]Lis-The Postman
			
首先,如果这个图本身就不存在欧拉回路,那么显然无解. 对于每个子串: 1.如果里面有不存在的边,那么显然无解. 2.如果里面有一条边重复出现,那么显然也无解. 3.对于每条边,维护其前驱与后继,若前驱 ...
 - Mac上tesseract-OCR的安装配置
			
Mac上tesseract-OCR的安装配置 tesseract简介 OCR(Optical Character Recognition)即光学字符识别技术,专门用于对图片文字进行识别,并获取文本. ...
 - 小甲鱼python第二讲课后习题
			
0.什么是BIF BIF为内置函数,英语全称为Build-in-Function Python3用input()取代了Python2的raw_input(),接收用户输入 1.用课堂上小甲鱼教的方法数 ...
 - 透明Panel
			
unit TransparentPanel; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Varia ...
 - oracle操作字符串:拼接、替换、截取、查找、长度、判断
			
1.拼接字符串 1)可以使用“||”来拼接字符串 select '拼接'||'字符串' as str from dual 2)通过concat()函数实现 select concat('拼接', '字 ...
 - 2012服务器在IIS部署的SLL(https)网址谷歌浏览器无法访问的问题解决
			
服务器环境:Windows Server 2012,IIS8. 当绑定了https,使用IE和Firefox浏览器能够正常访问,但是使用谷歌浏览器会出现net::ERR_CONNECTION_ABOR ...
 - Go语言二叉树定义及遍历算法实现
			
// binary_tree 二叉树 package Algorithm import ( "reflect" ) // 二叉树定义 type BinaryTree struct ...