前言
Netty自帶一個打印日誌的LoggingHandler。該類打印的日誌很詳細,但是我個人不喜歡這種風格。
HttpClient
該類由spring-cloud-gateway自動創建
回顧Netty,我們需要在ChannelPipeline添加自己的ChannelHandler。
創建LogginHandler
public class LoggingHandler extends ChannelDuplexHandler {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if(msg instanceof ByteBuf){
ByteBuf buf = (ByteBuf)msg;
String s = buf.toString(StandardCharsets.UTF_8);
//換行
String[] aar = s.split("\r\n|\n");
for(String ar : aar){
logger.info(" ==> "+ar);
}
}
super.write(ctx, msg, promise);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if(msg instanceof ByteBuf){
ByteBuf buf = (ByteBuf)msg;
String s = buf.toString(StandardCharsets.UTF_8);
//換行
String[] aar = s.split("\r\n|\n");
for(String ar : aar){
logger.info(" <== "+ar);
}
}
super.channelRead(ctx, msg);
}
添加Handler到ChannelPipeline
找到HttpClient後我們需要重新配置,並且返回新的HttpClient。那麼怎麼在spring容器中替換掉老的對象了。答案是:BeanPostProcessor
。
@Service
public class HttpClientBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof HttpClient){
HttpClient client = (HttpClient)bean;
return client.tcpConfiguration(tcpClient -> tcpClient.bootstrap(b-> BootstrapHandlers.updateConfiguration(b,"log",
(connectionObserver, channel) -> channel.pipeline().addFirst("log", new LoggingHandler()))
));
}
return bean;
}
}
到此結束,看看效果如何: 請求:
2020-11-17 08:58:54.387 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> POST /api/getMerCheckDiff HTTP/1.1
2020-11-17 08:58:54.387 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> Accept: */*
2020-11-17 08:58:54.387 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> accept-encoding: gzip, deflate
2020-11-17 08:58:54.387 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> content-length: 400
2020-11-17 08:58:54.388 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> cache-control: no-cache
2020-11-17 08:58:54.388 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> Postman-Token: 06aec57b-1848-46a9-a79f-dfa86e487c78
2020-11-17 08:58:54.388 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> Content-Type: application/json
2020-11-17 08:58:54.388 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> User-Agent: PostmanRuntime/6.4.1
2020-11-17 08:58:54.388 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> Forwarded: proto=http;host="127.0.0.1:8766";for="127.0.0.1:50708"
2020-11-17 08:58:54.388 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> X-Forwarded-For: 127.0.0.1
2020-11-17 08:58:54.388 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> X-Forwarded-Proto: http
2020-11-17 08:58:54.388 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> X-Forwarded-Port: 8081
2020-11-17 08:58:54.388 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> X-Forwarded-Host: 127.0.0.1:8081
2020-11-17 08:58:54.388 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> host: 10.0.73.20:8782
2020-11-17 08:58:54.408 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==> {"tradeDate":"20201011,20201015","limit":1,"page":1}
2020-11-17 08:58:54.410 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : ==>
返回:
2020-11-17 08:58:54.481 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : <== HTTP/1.1 200
2020-11-17 08:58:54.482 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : <== Content-Type: application/json;charset=utf8
2020-11-17 08:58:54.482 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : <== Transfer-Encoding: chunked
2020-11-17 08:58:54.482 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : <== Date: Tue, 17 Nov 2020 00:58:54 GMT
2020-11-17 08:58:54.482 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : <==
2020-11-17 08:58:54.482 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : <== 1b2
2020-11-17 08:58:54.482 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : <== {"status":200,"code":"000000","message":"SUCCESS","data":{"total":627,"rows":[{"total":0,"id":6135,"orderNo":"WWTHMLT20201011_1","merCode":"94221081766d4bf1895bc4326c870d1b","merName":"深圳八意府停車場","mallCode":"0004","checkResName":"手動平賬","merAmt":3.44,"channelAmt":3.44,"tradeDate":"20201011","checkDate":"20201012","checkRes":"21","handleStatus":"Y","leftAppCode":"0004","chargeSubjectNo":"","tradeType":"01"}]}}
2020-11-17 08:58:54.495 INFO 20488 --- [ctor-http-nio-2] c.vs.fc.gateway.api.test.LoggingHandler : <== 0