前提
本文獲取請(qǐng)求、響應(yīng)body大小方法的前提 : 網(wǎng)關(guān)只做轉(zhuǎn)發(fā)邏輯,不修改請(qǐng)求、相應(yīng)的body內(nèi)容。
SpringCloud Gateway內(nèi)部的機(jī)制類似下圖,HttpServer(也就是NettyServer)接收外部的請(qǐng)求,在Gateway內(nèi)部請(qǐng)求將會(huì)通過(guò)HttpClient(Netty實(shí)現(xiàn)的客戶端)發(fā)送給后端應(yīng)用。
本文的body獲取方式,基于HttpClient端實(shí)現(xiàn),通過(guò)獲取HttpClient發(fā)送、接收后端的請(qǐng)求、響應(yīng)body實(shí)現(xiàn)。如果SpringCloudGateway內(nèi)部邏輯修改了body,那么本文方式獲取的body大小將會(huì)存在歧義誤差。
如果想要在HttpServer層獲取到報(bào)文大小,可以嘗試自定義實(shí)現(xiàn)Netty的ChannelDuplexHandler
,嘗試獲取到報(bào)文大小。
SpringCloud Gateway底層基于異步模型Netty實(shí)現(xiàn),調(diào)用時(shí)相關(guān)的body內(nèi)容不直接加載到內(nèi)存。如果使用簡(jiǎn)單的SpringCloud Gateway Filter讀取報(bào)文,讀取body大小,會(huì)大幅影響網(wǎng)關(guān)性能。因此需要考慮一種方法,在不影響網(wǎng)關(guān)性能的前提下,獲取請(qǐng)求、響應(yīng)body大小。
方式一、重寫(xiě)SpringCloudGateway Filter類
重寫(xiě) NettyRoutingFilter 獲取 Request Body
重寫(xiě)Gateway自帶的org.springframework.cloud.gateway.filter.NettyRoutingFilter
。
修改類的filter內(nèi)的代碼,在底層獲取請(qǐng)求body的大小,并在exchange
保存。
Flux<HttpClientResponse> responseFlux = getHttpClient(route, exchange)
.headers(headers -> {
...
}).request(method).uri(url).send((req, nettyOutbound) -> {
...
return nettyOutbound.send(request.getBody().map(body -> {
// 修改此處代碼,獲取請(qǐng)求body的大小,并將獲取到的結(jié)果存入exchange內(nèi)。
int size = body.readableByteCount();
exchange.getAttributes().put("gw-request-body-size", size);
return getByteBuf(body);
}));
}).responseConnection((res, connection) -> {
...
重寫(xiě) NettyWriteResponseFilter 獲取 Response Body
重寫(xiě)Gateway自帶的org.springframework.cloud.gateway.filter.NettyWriteResponseFilter
。
修改類filter內(nèi)的代碼,在底層獲取響應(yīng)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 -> {
// 獲取響應(yīng)報(bào)文的長(zhǎng)度,并將結(jié)果寫(xiě)入exchange內(nèi)。
int respSize = byteBuf.readableBytes();
exchange.getAttributes().put("gw-response-body-size", respSize);
return wrap(byteBuf, response);
});
...
自定義Filter打印報(bào)文大小
通過(guò)上述的2個(gè)方法,request、response body的大小已經(jīng)寫(xiě)入exchange內(nèi),只需要實(shí)現(xiàn)一個(gè)自定義的Filter,就可以獲取到報(bào)文的大小。假設(shè)自定義的Filter命名為BodySizeFilter,它的Order需要在NettyWriteResponseFilter之前。
在filter方法內(nèi),從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,非重寫(xiě)SpringCloud Gateway類。本文構(gòu)建的SpringCloudGateway版本為2.2.9.RELEASE
。
實(shí)現(xiàn)自定義的Netty ChannelDuplexHandler
重寫(xiě)2個(gè)方法 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;
// 讀取報(bào)文大小,一筆請(qǐng)求可能存在多個(gè) msg,也就是一個(gè)請(qǐng)求報(bào)文,可能分多次經(jīng)過(guò)write方法。
int length = buf.readableBytes();
long size;
// 將結(jié)果以attribute形式保存在channel內(nèi),一筆完整的調(diào)用對(duì)應(yīng)一個(gè)完整的context上下文。
Attribute<Long> sizeAttr = ctx.channel().attr(REQ_SIZE);
if (sizeAttr.get() == null) {
size = 0L;
} else {
size = sizeAttr.get();
}
// 每次累加當(dāng)前請(qǐng)求的報(bào)文大小。
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;
// 獲取響應(yīng)body的大小,一筆響應(yīng)可能存在多個(gè) msg,也就是一個(gè)響應(yīng)報(bào)文,可能分多次經(jīng)過(guò)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;
// 將結(jié)果以attribute形式保存在channel內(nèi),一筆完整的調(diào)用對(duì)應(yīng)一個(gè)完整的context上下文。
ctx.channel().attr(RESP_SIZE).set(size);
}
super.channelRead(ctx, msg);
}
}
將自定義Handler配置到網(wǎng)關(guān)內(nèi)。
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實(shí)現(xiàn)。
return client.tcpConfiguration(tcpClient ->
tcpClient.bootstrap(b ->
BootstrapHandlers.updateConfiguration(b, "client-log", (connectionObserver, channel) -> {
channel.pipeline().addFirst("client-log", new HttpClientLoggingHandler());
})
)
);
}
}
通過(guò)上述自定義的方法,一筆完整的調(diào)用中請(qǐng)求、響應(yīng)body的大小,已經(jīng)被計(jì)算保存在netty channel內(nèi),只需要自定義SpringCloud Gateway Filter獲取到結(jié)果。
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內(nèi)將每個(gè)調(diào)用的Connection保存在exchange內(nèi)
// 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);
// 每次調(diào)用結(jié)束需要清空保存的值(因?yàn)檫B接會(huì)復(fù)用)
respSize.set(null);
reqSize.set(null);
return Mono.empty();
}));
}
@Override
public int getOrder() {
return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1;
}
}
通過(guò)此方法獲取的body大小會(huì)比真實(shí)的body大 ,因?yàn)樗苏?qǐng)求和響應(yīng)頭的信息。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-622653.html
總結(jié)
本人更加推薦使用方式一。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-622653.html
到了這里,關(guān)于SpringCloud Gateway獲取請(qǐng)求響應(yīng)body大小的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!