zoukankan      html  css  js  c++  java
  • WebFlux(reactor)中获取完整response body数据

    场景
    使用Spring Cloud Gateway(SCG)时,想在网关日志中输出返回日志,但由于数据流只能被读取一次,必须使用特定方式进行重写才能正常返回到前端。

    处理过程
    起初使用fluxBody.map读取数据流,会出现多次输出的情况,由于使用的时reactor框架处理数据,导致会出现将一个结果集分为多次处理,会执行多次map,效果不理想。
    Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
    Mono<Void> newMono = super.writeWith(
    fluxBody.map(dataBuffer -> {
    String respBody = dataBuffer.toString(StandardCharsets.UTF_8);
    if (respBody.length() > maxLogLength) {
    respBody = respBody.substring(0, maxLogLength);
    }
    if (PathUtil.checkPath(request.getPath().value(), contentLengthUrls)) {
    httpHeaders.setContentLength(respBody.length());
    }
    //匹配是否需要生成slat
    if (PathUtil.checkPath(request.getPath().value(), loginUrls)) {
    String salt = createSlat(JSON.parseObject(respBody).getString("token"));
    if (StringUtils.isEmpty(salt)) {
    logger.warn("CreateSalt error:id={}", request.getHeaders().getFirst(HEADER_REQUEST_ID));
    } else {
    httpHeaders.add("s", salt);
    }
    }
    logger.info("fgwResponse : id={},body = {}", request.getHeaders().getFirst(HEADER_REQUEST_ID), respBody);
    return bufferFactory.wrap(content);
    })
    );
    //request中有salt则返回到response中
    logger.info("fgwResponse : id={},resp = {}", request.getHeaders().getFirst(HEADER_REQUEST_ID), JSON.toJSONString(exchange.getResponse()));
    return newMono;
    最后查看github上的讨论,开发者给出DataBufferUtils.join(body) .doOnNext的方式。
    @Component
    public class LogRespFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    ServerHttpResponse originalResponse = exchange.getResponse();
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
    @Override
    public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
    //修改header
    HttpHeaders httpHeaders = originalResponse.getHeaders();
    httpHeaders.add("xxxxxx","aaaaaa");
    //输出返回结果
    if (body instanceof Flux) {
    Mono<Void> newMono = super.writeWith(
    DataBufferUtils.join(body)
    .doOnNext(dataBuffer -> {
    String respBody = dataBuffer.toString(StandardCharsets.UTF_8);
    //输出body
    logger.info("fgwResponse : body = {}", respBody);
    })
    );
    //输出response,不包含body
    logger.info("fgwResponse : resp = {}", JSON.toJSONString(exchange.getResponse()));
    return newMono;

    }
    return super.writeWith(body);
    }
    };
    return chain.filter(exchange.mutate().response(decoratedResponse).build());
    }
    }

    ————————————————
    版权声明:本文为CSDN博主「lizz666」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/lizz861109/article/details/106303929

  • 相关阅读:
    SQL语句编写
    触发器
    plot函数中的type中的参数
    【转】R中read.table详解
    7月18日R笔记
    RMySQL在windows下的安装方法
    WinXP下面实现JAVA对R调用 (rJava包设置)
    用R进行文档层次聚类完整实例(tm包)
    R学习之R层次聚类方法(tm包)
    R对term进行层次聚类完整实例(tm包)
  • 原文地址:https://www.cnblogs.com/duanxz/p/14829965.html
Copyright © 2011-2022 走看看