Spring Cloud Gateway 自定义打印请求和响应报文
Spring Cloud Gateway在debug级别下,默认的日志打印格式如下,不符合正常人的阅读格式,并且不好复制。83152 [reactor-http-nio-2] DEBUG r.netty.http.server.HttpServer - [id: 0x2c9d1b8b, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:55396] WRIT
·
Spring Cloud Gateway
在debug
级别下,默认的日志打印格式如下,不符合正常人的阅读格式,并且不好复制。
83152 [reactor-http-nio-2] DEBUG r.netty.http.server.HttpServer - [id: 0x2c9d1b8b, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:55396] WRITE: 115B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d |HTTP/1.1 200 OK.|
|00000010| 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74 |.Content-Type: t|
|00000020| 65 78 74 2f 68 74 6d 6c 3b 63 68 61 72 73 65 74 |ext/html;charset|
|00000030| 3d 55 54 46 2d 38 0d 0a 43 6f 6e 74 65 6e 74 2d |=UTF-8..Content-|
|00000040| 4c 65 6e 67 74 68 3a 20 31 31 0d 0a 44 61 74 65 |Length: 11..Date|
|00000050| 3a 20 54 75 65 2c 20 31 31 20 4d 61 79 20 32 30 |: Tue, 11 May 20|
|00000060| 32 31 20 30 37 3a 34 35 3a 33 34 20 47 4d 54 0d |21 07:45:34 GMT.|
|00000070| 0a 0d 0a |... |
+--------+-------------------------------------------------+----------------+
83156 [reactor-http-nio-2] DEBUG r.netty.http.server.HttpServer - [id: 0x2c9d1b8b, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:55396] WRITE: 11B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 65 6c 6c 6f 20 77 6f 72 6c 64 |hello world |
+--------+-------------------------------------------------+----------------+
上述的日志由netty
内打印,通过LoggingHandler
打印,我们只需要自定义LoggingHandler
实现自己的日志打印规则即可。修改方式如下。 继承io.netty.channel.ChannelDuplexHandler
,实现自定义处理器。
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
@Slf4j
public class LoggingHandler extends ChannelDuplexHandler {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof ByteBuf) {
final ByteBuf buf = (ByteBuf) msg;
int length = buf.readableBytes();
if (length > 0) {
final String content = buf.toString(StandardCharsets.UTF_8);
//换行
final StringBuilder sb = new StringBuilder();
Arrays.stream(content.split("\r\n|\n"))
.forEach(str -> sb.append(str).append("\n"));
log.info("request meta: {}", sb.toString());
}
}
super.write(ctx, msg, promise);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ByteBuf) {
final ByteBuf buf = (ByteBuf) msg;
int length = buf.readableBytes();
if (length > 0) {
String content = buf.toString(StandardCharsets.UTF_8);
final StringBuilder sb = new StringBuilder(length);
Arrays.stream(content.split("\r\n|\n"))
.forEach(str -> sb.append(str).append("\n"));
log.info("response meta: {}", sb.toString());
}
}
super.channelRead(ctx, msg);
}
}
上面的代码通过在处理请求前获取数据内容,打印请求和响应报文。考虑到这里通过ByteBuf
获取请求的内容,对网关性能还是有一些影响的。
接下来要将自定义的日志打印对象设置到netty
对象内。首先参考org.springframework.cloud.gateway.config.GatewayAutoConfiguration.NettyConfiguration#gatewayHttpClient
方法,该方法内创建了HttpClient Bean
,我们可以修改HttpClient
创建过程,将自定义的日志打印方式设置到netty
内部。
...
@Slf4j
@Configuration
public class Configurer {
@Bean
public HttpClient gatewayHttpClient(HttpClientProperties properties,
List<HttpClientCustomizer> customizers) {
...
HttpClient httpClient = HttpClient.create(connectionProvider)
// TODO: move customizations to HttpClientCustomizers
.httpResponseDecoder(spec -> {
...
}).tcpConfiguration(tcpClient -> {
...
if (StringUtils.hasText(proxy.getHost())) {
...
}
// 增加这一段代码,其他都和原有的一样,添加我们自定义的请求和响应报文打印方式。
tcpClient = tcpClient.bootstrap(b-> BootstrapHandlers.updateConfiguration(b,"log",
(connectionObserver, channel) -> {
channel.pipeline().addFirst("log", new LoggingHandler());
})
);
return tcpClient;
});
...
return httpClient;
}
}
效果
80015 [reactor-http-nio-4] INFO c.l.sidecar.example.LoggingHandler - request meta: GET /hello HTTP/1.1
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
sec-ch-ua-mobile: ?0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Cookie: _ga=GA1.1.270405173.1557761261
Forwarded: proto=http;host="localhost:8080";for="0:0:0:0:0:0:0:1:55396"
X-Forwarded-For: 0:0:0:0:0:0:0:1
X-Forwarded-Proto: http
X-Forwarded-Port: 8080
X-Forwarded-Host: localhost:8080
host: localhost:8761
content-length: 0
83108 [reactor-http-nio-4] INFO c.l.sidecar.example.LoggingHandler - response meta: HTTP/1.1 200
Content-Type: text/html;charset=UTF-8
Content-Length: 11
Date: Tue, 11 May 2021 07:45:34 GMT
hello world
更多推荐
已为社区贡献4条内容
所有评论(0)