SpringCloud Gateway获取请求响应body大小

本文实现方式存在缺陷,建议参考以下文档

https://blog.csdn.net/sweatOtt/article/details/136317313


前提

本文获取请求、响应body大小方法的前提 : 网关只做转发逻辑,不修改请求、相应的body内容。
SpringCloud Gateway内部的机制类似下图,HttpServer(也就是NettyServer)接收外部的请求,在Gateway内部请求将会通过HttpClient(Netty实现的客户端)发送给后端应用。
本文的body获取方式,基于HttpClient端实现,通过获取HttpClient发送、接收后端的请求、响应body实现。如果SpringCloudGateway内部逻辑修改了body,那么本文方式获取的body大小将会存在歧义误差。
如果想要在HttpServer层获取到报文大小,可以尝试自定义实现Netty的ChannelDuplexHandler,尝试获取到报文大小。
在这里插入图片描述

SpringCloud Gateway底层基于异步模型Netty实现,调用时相关的body内容不直接加载到内存。如果使用简单的SpringCloud Gateway Filter读取报文,读取body大小,会大幅影响网关性能。因此需要考虑一种方法,在不影响网关性能的前提下,获取请求、响应body大小。

方式一、重写SpringCloudGateway Filter类

重写 NettyRoutingFilter 获取 Request Body

重写Gateway自带的org.springframework.cloud.gateway.filter.NettyRoutingFilter
修改类的filter内的代码,在底层获取请求body的大小,并在exchange保存。

Flux<HttpClientResponse> responseFlux = getHttpClient(route, exchange)
      .headers(headers -> {
         ...
      }).request(method).uri(url).send((req, nettyOutbound) -> {
         ...
         return nettyOutbound.send(request.getBody().map(body -> {
            // 修改此处代码,获取请求body的大小,并将获取到的结果存入exchange内。
            int size = body.readableByteCount();
            exchange.getAttributes().put("gw-request-body-size", size);
            return getByteBuf(body);
         }));
      }).responseConnection((res, connection) -> {
        ...

重写 NettyWriteResponseFilter 获取 Response Body

重写Gateway自带的org.springframework.cloud.gateway.filter.NettyWriteResponseFilter
修改类filter内的代码,在底层获取响应body的大小,并在exchange保存。

return chain.filter(exchange)
      .doOnError(throwable -> cleanup(exchange))
      .then(Mono.defer(() -> {
        ...
         // TODO: needed?
         final Flux<DataBuffer> body = connection
               .inbound()
               .receive()
               .retain()
               .map(byteBuf -> {
                  // 获取响应报文的长度,并将结果写入exchange内。
                  int respSize = byteBuf.readableBytes();
                  exchange.getAttributes().put("gw-response-body-size", respSize);
                  return wrap(byteBuf, response);
               });
      ...

自定义Filter打印报文大小

通过上述的2个方法,request、response body的大小已经写入exchange内,只需要实现一个自定义的Filter,就可以获取到报文的大小。假设自定义的Filter命名为BodySizeFilter,它的Order需要在NettyWriteResponseFilter之前。
在这里插入图片描述

在filter方法内,从exchange获取request、response body大小。

@Slf4j
@Component
public class BodySizeFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return chain.filter(exchange)
                .then(Mono.defer(() -> {
                    Integer exchangeReq = exchange.getAttribute("gw-request-body-size");
                    Integer exchangeResp = exchange.getAttribute("gw-response-body-size");
                    log.info("req from exchange: {}", exchangeReq);
                    log.info("resp from exchange: {}", exchangeResp);
                    return Mono.empty();
                }));
    }
    @Override
    public int getOrder() {
        return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1;
    }
}

方式二、自定义Netty Handler

另一种方式是基于Netty的Hander,非重写SpringCloud Gateway类。本文构建的SpringCloudGateway版本为2.2.9.RELEASE

实现自定义的Netty ChannelDuplexHandler

重写2个方法 write、channelRead。

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.UUID;

@Slf4j
public class HttpClientLoggingHandler extends ChannelDuplexHandler {
    private static final AttributeKey<Long> RESP_SIZE = AttributeKey.valueOf("resp-size");

    private static final AttributeKey<Long> REQ_SIZE = AttributeKey.valueOf("req-size");

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (msg instanceof ByteBuf) {
            final ByteBuf buf = (ByteBuf) msg;
            // 读取报文大小,一笔请求可能存在多个 msg,也就是一个请求报文,可能分多次经过write方法。
            int length = buf.readableBytes();
            long size;
            // 将结果以attribute形式保存在channel内,一笔完整的调用对应一个完整的context上下文。
            Attribute<Long> sizeAttr = ctx.channel().attr(REQ_SIZE);
            if (sizeAttr.get() == null) {
                size = 0L;
            } else {
                size = sizeAttr.get();
            }
            // 每次累加当前请求的报文大小。
            size += length;
            ctx.channel().attr(REQ_SIZE).set(size);
        }
        super.write(ctx, msg, promise);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof ByteBuf) {
            final ByteBuf buf = (ByteBuf) msg;
            // 获取响应body的大小,一笔响应可能存在多个 msg,也就是一个响应报文,可能分多次经过channelRead方法。
            int length = buf.readableBytes();
            long size;
            Attribute<Long> sizeAttr = ctx.channel().attr(RESP_SIZE);
            if (sizeAttr.get() == null) {
                size = 0L;
            } else {
                size = ctx.channel().attr(RESP_SIZE).get();
            }
            size += length;
            // 将结果以attribute形式保存在channel内,一笔完整的调用对应一个完整的context上下文。
            ctx.channel().attr(RESP_SIZE).set(size);
        }
        super.channelRead(ctx, msg);
    }
}

将自定义Handler配置到网关内。

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.config.HttpClientCustomizer;
import org.springframework.context.annotation.Configuration;
import reactor.netty.channel.BootstrapHandlers;
import reactor.netty.http.client.HttpClient;

@Slf4j
@Configuration
public class GwHttpClientCustomizer implements HttpClientCustomizer {
    @Override
    public HttpClient customize(HttpClient client) {
        // 本文基于2.2.9.RELEASE的SpringCloud Gateway实现。
        return client.tcpConfiguration(tcpClient ->
                tcpClient.bootstrap(b ->
                        BootstrapHandlers.updateConfiguration(b, "client-log", (connectionObserver, channel) -> {
                            channel.pipeline().addFirst("client-log", new HttpClientLoggingHandler());
                        })
                )
        );
    }
}

通过上述自定义的方法,一笔完整的调用中请求、响应body的大小,已经被计算保存在netty channel内,只需要自定义SpringCloud Gateway Filter获取到结果。

import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.netty.Connection;

import java.util.function.Consumer;

import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CLIENT_RESPONSE_CONN_ATTR;

/**
 * @author luobo on 2023/08/01 3:51 PM
 */
@Slf4j
@Component
public class BodySizeFilter implements GlobalFilter, Ordered {

    private static final AttributeKey<Long> REQ_SIZE = AttributeKey.valueOf("req-size");

    private static final AttributeKey<Long> RESP_SIZE = AttributeKey.valueOf("resp-size");

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return chain.filter(exchange)
                .then(Mono.defer(() -> {
                    // SpringCloud Gateway内将每个调用的Connection保存在exchange内
                    // connection 可以获取到 channel
                    Connection connection = exchange.getAttribute(CLIENT_RESPONSE_CONN_ATTR);
                    Attribute<Long> respSize = connection.channel().attr(RESP_SIZE);
                    Attribute<Long> reqSize = connection.channel().attr(REQ_SIZE);
                    long resp;
                    if (respSize.get() == null) {
                        resp = 0L;
                    } else {
                        resp = respSize.get();
                    }
                    long req;
                    if (reqSize.get() == null) {
                        req = 0L;
                    } else {
                        req = reqSize.get();
                    }
                    log.info("------------------------> resp size: {}", resp);
                    log.info("------------------------> req size: {}", req);
                    // 每次调用结束需要清空保存的值(因为连接会复用)
                    respSize.set(null);
                    reqSize.set(null);
                    return Mono.empty();
                }));
    }

    @Override
    public int getOrder() {
        return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1;
    }
}

通过此方法获取的body大小会比真实的body大 ,因为它包含了请求和响应头的信息。


总结

本人更加推荐使用方式一。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Gateway 是一个反向代理和路由器,它可以拦截进入的请求并将它们路由到不同的服务。Spring Cloud Gateway 可以通过以下步骤获取请求体: 1. 在 Spring Cloud Gateway 的路由配置中,定义一个过滤器: ```java @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_route", r -> r.path("/foo/**") .filters(f -> f.filter(new RequestBodyFilter())) .uri("http://localhost:8080")) .build(); } ``` 这里定义了一个名为 `RequestBodyFilter` 的过滤器,用于获取请求体。 2. 实现 `RequestBodyFilter` 过滤器: ```java public class RequestBodyFilter implements GatewayFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { return exchange.getRequest().getBody() .flatMap(body -> { // 处理请求体 byte[] bytes = new byte[body.readableByteCount()]; body.read(bytes); String requestBody = new String(bytes); System.out.println(requestBody); // 重新设置请求体 DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes); exchange.getRequest().mutate().body(buffer); return chain.filter(exchange); }); } } ``` 这里的 `filter` 方法会获取请求体,并进行处理。处理完成后,需要重新设置请求体,以便后续的过滤器或路由器可以正确地处理请求。 3. 在请求中发送请求体: ```bash curl -X POST http://localhost:8080/foo -d '{"name": "John"}' ``` 这里使用 `curl` 命令发送一个 POST 请求,并在请求体中包含 JSON 数据。Spring Cloud Gateway 会拦截这个请求,并使用定义的过滤器获取请求体。 以上就是 Spring Cloud Gateway 获取请求体的方法。需要注意的是,获取请求体可能会影响性能,因此应该谨慎使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值