SpringCloud:Spring Cloud Gateway如何打印报文日志
Spring Cloud Gateway如何打印报文日志
·
最简单的方式是在application.properties中添加:
spring.cloud.gateway.httpclient.wiretap=true
spring.cloud.gateway.httpserver.wiretap=true
然后使用slf4j+logback,配置如下:
<configuration>
<appender name="accessLog" class="ch.qos.logback.core.FileAppender">
<file>D:/temp/logs/access.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}[%thread] %-5level %logger - %m%n </pattern>
</encoder>
</appender>
<appender name="asyncAccessLog" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="accessLog" />
</appender>
<logger name="reactor.netty.http.server.HttpServer" level="DEBUG" additivity="false">
<appender-ref ref="asyncAccessLog" />
</logger>
<logger name="reactor.netty.http.client.HttpClient" level="DEBUG" additivity="false">
<appender-ref ref="asyncAccessLog" />
</logger>
</configuration>
将在日志中打印:
2021-05-07 19:10:25.750[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] REGISTERED
2021-05-07 19:10:25.750[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] ACTIVE
2021-05-07 19:10:25.766[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] READ: 145B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 47 45 54 20 2f 74 65 73 74 20 48 54 54 50 2f 31 |GET /test HTTP/1|
|00000010| 2e 31 0d 0a 73 65 72 76 69 63 65 4e 61 6d 65 3a |.1..serviceName:|
|00000020| 20 73 32 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 | s2..Content-Typ|
|00000030| 65 3a 20 74 65 78 74 2f 70 6c 61 69 6e 0d 0a 43 |e: text/plain..C|
|00000040| 61 63 68 65 2d 43 6f 6e 74 72 6f 6c 3a 20 6e 6f |ache-Control: no|
|00000050| 2d 63 61 63 68 65 0d 0a 48 6f 73 74 3a 20 31 32 |-cache..Host: 12|
|00000060| 37 2e 30 2e 30 2e 31 3a 38 30 37 30 0d 0a 43 6f |7.0.0.1:8070..Co|
|00000070| 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 31 33 |ntent-Length: 13|
|00000080| 0d 0a 0d 0a 68 65 6c 6c 6f 20 77 6f 72 6c 64 0d |....hello world.|
|00000090| 0a |. |
+--------+-------------------------------------------------+----------------+
2021-05-07 19:10:25.785[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82-1, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@14c978d9
2021-05-07 19:10:28.893[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82-1, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] READ COMPLETE
2021-05-07 19:10:28.894[reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:0b533acd] REGISTERED
2021-05-07 19:10:28.913[reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:0b533acd] CONNECT: /127.0.0.1:9090
2021-05-07 19:10:28.919[reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:0b533acd, L:/127.0.0.1:50699 - R:/127.0.0.1:9090] ACTIVE
2021-05-07 19:10:28.942[reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:0b533acd-1, L:/127.0.0.1:50699 - R:/127.0.0.1:9090] WRITE: 386B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 47 45 54 20 2f 54 65 73 74 57 65 62 41 70 70 2f |GET /TestWebApp/|
|00000010| 74 65 73 74 53 65 72 76 6c 65 74 20 48 54 54 50 |testServlet HTTP|
|00000020| 2f 31 2e 31 0d 0a 73 65 72 76 69 63 65 4e 61 6d |/1.1..serviceNam|
|00000030| 65 3a 20 73 32 0d 0a 43 6f 6e 74 65 6e 74 2d 54 |e: s2..Content-T|
|00000040| 79 70 65 3a 20 74 65 78 74 2f 70 6c 61 69 6e 0d |ype: text/plain.|
|00000050| 0a 43 61 63 68 65 2d 43 6f 6e 74 72 6f 6c 3a 20 |.Cache-Control: |
|00000060| 6e 6f 2d 63 61 63 68 65 0d 0a 46 6f 72 77 61 72 |no-cache..Forwar|
|00000070| 64 65 64 3a 20 70 72 6f 74 6f 3d 68 74 74 70 3b |ded: proto=http;|
|00000080| 68 6f 73 74 3d 22 31 32 37 2e 30 2e 30 2e 31 3a |host="127.0.0.1:|
|00000090| 38 30 37 30 22 3b 66 6f 72 3d 22 31 32 37 2e 30 |8070";for="127.0|
|000000a0| 2e 30 2e 31 3a 35 30 36 39 38 22 0d 0a 58 2d 46 |.0.1:50698"..X-F|
|000000b0| 6f 72 77 61 72 64 65 64 2d 46 6f 72 3a 20 31 32 |orwarded-For: 12|
|000000c0| 37 2e 30 2e 30 2e 31 0d 0a 58 2d 46 6f 72 77 61 |7.0.0.1..X-Forwa|
|000000d0| 72 64 65 64 2d 50 72 6f 74 6f 3a 20 68 74 74 70 |rded-Proto: http|
|000000e0| 0d 0a 58 2d 46 6f 72 77 61 72 64 65 64 2d 50 6f |..X-Forwarded-Po|
|000000f0| 72 74 3a 20 38 30 37 30 0d 0a 58 2d 46 6f 72 77 |rt: 8070..X-Forw|
|00000100| 61 72 64 65 64 2d 48 6f 73 74 3a 20 31 32 37 2e |arded-Host: 127.|
|00000110| 30 2e 30 2e 31 3a 38 30 37 30 0d 0a 75 73 65 72 |0.0.1:8070..user|
|00000120| 2d 61 67 65 6e 74 3a 20 52 65 61 63 74 6f 72 4e |-agent: ReactorN|
|00000130| 65 74 74 79 2f 31 2e 30 2e 35 0d 0a 68 6f 73 74 |etty/1.0.5..host|
|00000140| 3a 20 31 32 37 2e 30 2e 30 2e 31 3a 39 30 39 30 |: 127.0.0.1:9090|
|00000150| 0d 0a 61 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 63 |..accept: */*..c|
|00000160| 6f 6e 74 65 6e 74 2d 6c 65 6e 67 74 68 3a 20 31 |ontent-length: 1|
|00000170| 33 0d 0a 0d 0a 68 65 6c 6c 6f 20 77 6f 72 6c 64 |3....hello world|
|00000180| 0d 0a |.. |
+--------+-------------------------------------------------+----------------+
2021-05-07 19:10:28.942[reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:0b533acd-1, L:/127.0.0.1:50699 - R:/127.0.0.1:9090] FLUSH
2021-05-07 19:10:28.954[reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:0b533acd-1, L:/127.0.0.1:50699 - R:/127.0.0.1:9090] READ: 479B
+-------------------------------------------------+
| 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 0d 0a 53 |HTTP/1.1 200 ..S|
|00000010| 65 74 2d 43 6f 6f 6b 69 65 3a 20 4a 53 45 53 53 |et-Cookie: JSESS|
|00000020| 49 4f 4e 49 44 3d 34 44 44 45 34 34 31 33 39 44 |IONID=4DDE44139D|
|00000030| 38 37 43 35 46 42 36 33 43 41 36 37 46 42 35 39 |87C5FB63CA67FB59|
|00000040| 31 43 38 37 42 33 3b 20 50 61 74 68 3d 2f 54 65 |1C87B3; Path=/Te|
|00000050| 73 74 57 65 62 41 70 70 3b 20 48 74 74 70 4f 6e |stWebApp; HttpOn|
|00000060| 6c 79 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 |ly..Content-Type|
|00000070| 3a 20 74 65 78 74 2f 68 74 6d 6c 3b 63 68 61 72 |: text/html;char|
|00000080| 73 65 74 3d 55 54 46 2d 38 0d 0a 43 6f 6e 74 65 |set=UTF-8..Conte|
|00000090| 6e 74 2d 4c 65 6e 67 74 68 3a 20 32 38 30 0d 0a |nt-Length: 280..|
|000000a0| 44 61 74 65 3a 20 46 72 69 2c 20 30 37 20 4d 61 |Date: Fri, 07 Ma|
|000000b0| 79 20 32 30 32 31 20 31 31 3a 31 30 3a 32 38 20 |y 2021 11:10:28 |
|000000c0| 47 4d 54 0d 0a 0d 0a 0d 0a 0d 0a 0d 0a 3c 21 44 |GMT..........<!D|
|000000d0| 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0d 0a 3c 68 |OCTYPE html>..<h|
|000000e0| 74 6d 6c 3e 0d 0a 3c 68 65 61 64 3e 0d 0a 3c 6d |tml>..<head>..<m|
|000000f0| 65 74 61 20 63 68 61 72 73 65 74 3d 22 55 54 46 |eta charset="UTF|
|00000100| 2d 38 22 3e 0d 0a 3c 74 69 74 6c 65 3e 49 6e 73 |-8">..<title>Ins|
|00000110| 65 72 74 20 74 69 74 6c 65 20 68 65 72 65 3c 2f |ert title here</|
|00000120| 74 69 74 6c 65 3e 0d 0a 3c 2f 68 65 61 64 3e 0d |title>..</head>.|
|00000130| 0a 3c 62 6f 64 79 3e 0d 0a 09 3c 62 72 20 2f 3e |.<body>...<br />|
|00000140| 20 7c 30 7c 0d 0a 09 3c 62 72 20 2f 3e 20 e6 b5 | |0|...<br /> ..|
|00000150| 8b e8 af 95 e4 b8 80 e4 b8 8b 0d 0a 09 3c 62 72 |.............<br|
|00000160| 20 2f 3e 20 31 0d 0a 09 3c 62 72 20 2f 3e 20 34 | /> 1...<br /> 4|
|00000170| 0d 0a 09 3c 62 72 20 2f 3e 0d 0a 09 0d 0a 09 3c |...<br />......<|
|00000180| 62 72 20 2f 3e 0d 0a 09 69 73 20 30 0d 0a 09 3c |br />...is 0...<|
|00000190| 62 72 20 2f 3e 20 31 0d 0a 09 3c 62 72 20 2f 3e |br /> 1...<br />|
|000001a0| 20 e6 b5 8b e8 af 95 e4 b8 80 e4 b8 8b e6 9d a5 | ...............|
|000001b0| e4 b8 80 e4 b8 8b 31 0d 0a 09 3c 62 72 20 2f 3e |......1...<br />|
|000001c0| 20 31 0d 0a 09 3c 62 72 20 2f 3e 20 38 0d 0a 3c | 1...<br /> 8..<|
|000001d0| 2f 62 6f 64 79 3e 0d 0a 3c 2f 68 74 6d 6c 3e |/body>..</html> |
+--------+-------------------------------------------------+----------------+
2021-05-07 19:10:28.988[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82-1, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] WRITE: 201B
+-------------------------------------------------+
| 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 53 65 74 2d 43 6f 6f 6b 69 65 3a 20 4a 53 45 |.Set-Cookie: JSE|
|00000020| 53 53 49 4f 4e 49 44 3d 34 44 44 45 34 34 31 33 |SSIONID=4DDE4413|
|00000030| 39 44 38 37 43 35 46 42 36 33 43 41 36 37 46 42 |9D87C5FB63CA67FB|
|00000040| 35 39 31 43 38 37 42 33 3b 20 50 61 74 68 3d 2f |591C87B3; Path=/|
|00000050| 54 65 73 74 57 65 62 41 70 70 3b 20 48 74 74 70 |TestWebApp; Http|
|00000060| 4f 6e 6c 79 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 |Only..Content-Ty|
|00000070| 70 65 3a 20 74 65 78 74 2f 68 74 6d 6c 3b 63 68 |pe: text/html;ch|
|00000080| 61 72 73 65 74 3d 55 54 46 2d 38 0d 0a 44 61 74 |arset=UTF-8..Dat|
|00000090| 65 3a 20 46 72 69 2c 20 30 37 20 4d 61 79 20 32 |e: Fri, 07 May 2|
|000000a0| 30 32 31 20 31 31 3a 31 30 3a 32 38 20 47 4d 54 |021 11:10:28 GMT|
|000000b0| 0d 0a 63 6f 6e 74 65 6e 74 2d 6c 65 6e 67 74 68 |..content-length|
|000000c0| 3a 20 32 38 30 0d 0a 0d 0a |: 280.... |
+--------+-------------------------------------------------+----------------+
2021-05-07 19:10:28.988[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82-1, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] FLUSH
2021-05-07 19:10:28.990[reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:0b533acd, L:/127.0.0.1:50699 - R:/127.0.0.1:9090] READ COMPLETE
2021-05-07 19:10:28.991[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82-1, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] WRITE: 280B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 0d 0a 0d 0a 0d 0a 3c 21 44 4f 43 54 59 50 45 20 |......<!DOCTYPE |
|00000010| 68 74 6d 6c 3e 0d 0a 3c 68 74 6d 6c 3e 0d 0a 3c |html>..<html>..<|
|00000020| 68 65 61 64 3e 0d 0a 3c 6d 65 74 61 20 63 68 61 |head>..<meta cha|
|00000030| 72 73 65 74 3d 22 55 54 46 2d 38 22 3e 0d 0a 3c |rset="UTF-8">..<|
|00000040| 74 69 74 6c 65 3e 49 6e 73 65 72 74 20 74 69 74 |title>Insert tit|
|00000050| 6c 65 20 68 65 72 65 3c 2f 74 69 74 6c 65 3e 0d |le here</title>.|
|00000060| 0a 3c 2f 68 65 61 64 3e 0d 0a 3c 62 6f 64 79 3e |.</head>..<body>|
|00000070| 0d 0a 09 3c 62 72 20 2f 3e 20 7c 30 7c 0d 0a 09 |...<br /> |0|...|
|00000080| 3c 62 72 20 2f 3e 20 e6 b5 8b e8 af 95 e4 b8 80 |<br /> .........|
|00000090| e4 b8 8b 0d 0a 09 3c 62 72 20 2f 3e 20 31 0d 0a |......<br /> 1..|
|000000a0| 09 3c 62 72 20 2f 3e 20 34 0d 0a 09 3c 62 72 20 |.<br /> 4...<br |
|000000b0| 2f 3e 0d 0a 09 0d 0a 09 3c 62 72 20 2f 3e 0d 0a |/>......<br />..|
|000000c0| 09 69 73 20 30 0d 0a 09 3c 62 72 20 2f 3e 20 31 |.is 0...<br /> 1|
|000000d0| 0d 0a 09 3c 62 72 20 2f 3e 20 e6 b5 8b e8 af 95 |...<br /> ......|
|000000e0| e4 b8 80 e4 b8 8b e6 9d a5 e4 b8 80 e4 b8 8b 31 |...............1|
|000000f0| 0d 0a 09 3c 62 72 20 2f 3e 20 31 0d 0a 09 3c 62 |...<br /> 1...<b|
|00000100| 72 20 2f 3e 20 38 0d 0a 3c 2f 62 6f 64 79 3e 0d |r /> 8..</body>.|
|00000110| 0a 3c 2f 68 74 6d 6c 3e |.</html> |
+--------+-------------------------------------------------+----------------+
2021-05-07 19:10:28.992[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82-1, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] FLUSH
2021-05-07 19:10:28.995[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82-1, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] WRITE: 0B
2021-05-07 19:10:28.995[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82-1, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] FLUSH
2021-05-07 19:10:29.013[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82, L:/127.0.0.1:8070 - R:/127.0.0.1:50698] READ COMPLETE
2021-05-07 19:10:29.014[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82, L:/127.0.0.1:8070 ! R:/127.0.0.1:50698] INACTIVE
2021-05-07 19:10:29.014[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:97d2bc82, L:/127.0.0.1:8070 ! R:/127.0.0.1:50698] UNREGISTERED
2021-05-07 19:10:49.090[reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:0b533acd, L:/127.0.0.1:50699 - R:/127.0.0.1:9090] READ COMPLETE
2021-05-07 19:10:49.090[reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:0b533acd, L:/127.0.0.1:50699 ! R:/127.0.0.1:9090] INACTIVE
2021-05-07 19:10:49.090[reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:0b533acd, L:/127.0.0.1:50699 ! R:/127.0.0.1:9090] UNREGISTERED
以上打印的日志人看着不方便,可以使用如下方式,将org.springframework.cloud.gateway.config.GatewayAutoConfiguration.NettyConfiguration中的一些Bean覆盖掉
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.List;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizer;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.cloud.gateway.config.HttpClientCustomizer;
import org.springframework.cloud.gateway.config.HttpClientProperties;
import org.springframework.cloud.gateway.config.HttpClientProperties.Pool.PoolType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import io.netty.channel.ChannelOption;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;
import reactor.netty.transport.ProxyProvider;
import reactor.netty.transport.logging.AdvancedByteBufFormat;
@Configuration
public class LoggingConfiguration {
@Bean
public NettyWebServerFactoryCustomizer nettyServerWiretapCustomizer(Environment environment,
ServerProperties serverProperties) {
return new NettyWebServerFactoryCustomizer(environment, serverProperties) {
@Override
public void customize(NettyReactiveWebServerFactory factory) {
factory.addServerCustomizers(httpServer -> httpServer.wiretap("httpPkg.server", LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL)); // 1
super.customize(factory);
}
};
}
@Bean
public HttpClient gatewayHttpClient(HttpClientProperties properties, List<HttpClientCustomizer> customizers) {
// configure pool resources
HttpClientProperties.Pool pool = properties.getPool();
ConnectionProvider connectionProvider;
if (pool.getType() == PoolType.DISABLED) {
connectionProvider = ConnectionProvider.newConnection();
} else if (pool.getType() == PoolType.FIXED) {
ConnectionProvider.Builder builder = ConnectionProvider.builder(pool.getName())
.maxConnections(pool.getMaxConnections()).pendingAcquireMaxCount(-1)
.pendingAcquireTimeout(Duration.ofMillis(pool.getAcquireTimeout()));
if (pool.getMaxIdleTime() != null) {
builder.maxIdleTime(pool.getMaxIdleTime());
}
if (pool.getMaxLifeTime() != null) {
builder.maxLifeTime(pool.getMaxLifeTime());
}
connectionProvider = builder.build();
} else {
ConnectionProvider.Builder builder = ConnectionProvider.builder(pool.getName())
.maxConnections(Integer.MAX_VALUE).pendingAcquireTimeout(Duration.ofMillis(0))
.pendingAcquireMaxCount(-1);
if (pool.getMaxIdleTime() != null) {
builder.maxIdleTime(pool.getMaxIdleTime());
}
if (pool.getMaxLifeTime() != null) {
builder.maxLifeTime(pool.getMaxLifeTime());
}
connectionProvider = builder.build();
}
HttpClient httpClient = HttpClient.create(connectionProvider)
// TODO: move customizations to HttpClientCustomizers
.httpResponseDecoder(spec -> {
if (properties.getMaxHeaderSize() != null) {
// cast to int is ok, since @Max is Integer.MAX_VALUE
spec.maxHeaderSize((int) properties.getMaxHeaderSize().toBytes());
}
if (properties.getMaxInitialLineLength() != null) {
// cast to int is ok, since @Max is Integer.MAX_VALUE
spec.maxInitialLineLength((int) properties.getMaxInitialLineLength().toBytes());
}
return spec;
}).tcpConfiguration(tcpClient -> {
if (properties.getConnectTimeout() != null) {
tcpClient = tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
properties.getConnectTimeout());
}
// configure proxy if proxy host is set.
HttpClientProperties.Proxy proxy = properties.getProxy();
if (StringUtils.hasText(proxy.getHost())) {
tcpClient = tcpClient.proxy(proxySpec -> {
ProxyProvider.Builder builder = proxySpec.type(proxy.getType()).host(proxy.getHost());
PropertyMapper map = PropertyMapper.get();
map.from(proxy::getPort).whenNonNull().to(builder::port);
map.from(proxy::getUsername).whenHasText().to(builder::username);
map.from(proxy::getPassword).whenHasText().to(password -> builder.password(s -> password));
map.from(proxy::getNonProxyHostsPattern).whenHasText().to(builder::nonProxyHosts);
});
}
return tcpClient;
});
HttpClientProperties.Ssl ssl = properties.getSsl();
if ((ssl.getKeyStore() != null && ssl.getKeyStore().length() > 0)
|| ssl.getTrustedX509CertificatesForTrustManager().length > 0 || ssl.isUseInsecureTrustManager()) {
httpClient = httpClient.secure(sslContextSpec -> {
// configure ssl
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();
X509Certificate[] trustedX509Certificates = ssl.getTrustedX509CertificatesForTrustManager();
if (trustedX509Certificates.length > 0) {
sslContextBuilder = sslContextBuilder.trustManager(trustedX509Certificates);
} else if (ssl.isUseInsecureTrustManager()) {
sslContextBuilder = sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
}
try {
sslContextBuilder = sslContextBuilder.keyManager(ssl.getKeyManagerFactory());
} catch (Exception e) {
// logger.error(e);
}
sslContextSpec.sslContext(sslContextBuilder).defaultConfiguration(ssl.getDefaultConfigurationType())
.handshakeTimeout(ssl.getHandshakeTimeout())
.closeNotifyFlushTimeout(ssl.getCloseNotifyFlushTimeout())
.closeNotifyReadTimeout(ssl.getCloseNotifyReadTimeout());
});
}
if (properties.isWiretap()) {
httpClient = httpClient.wiretap("httpPkg.client", LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL); // 2
}
if (properties.isCompression()) {
httpClient = httpClient.compress(true);
}
if (!CollectionUtils.isEmpty(customizers)) {
customizers.sort(AnnotationAwareOrderComparator.INSTANCE);
for (HttpClientCustomizer customizer : customizers) {
httpClient = customizer.customize(httpClient);
}
}
return httpClient;
}
}
同时,将spring.cloud.gateway.httpserver.wiretap设为false,添加如下logback配置
<logger name="httpPkg" level="DEBUG" additivity="false">
<appender-ref ref="asyncAccessLog" />
</logger>
此时就会生成方便阅读的日志
2021-05-07 20:49:11.197[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] REGISTERED
2021-05-07 20:49:11.197[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] ACTIVE
2021-05-07 20:49:11.212[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] READ: 145B GET /test HTTP/1.1
serviceName: s2
Content-Type: text/plain
Cache-Control: no-cache
Host: 127.0.0.1:8070
Content-Length: 13
hello world
2021-05-07 20:49:11.229[reactor-http-nio-2] DEBUG reactor.netty.http.server.HttpServer - [id:45419f46-1, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@2b1e0c4d
2021-05-07 20:49:14.321[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46-1, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] READ COMPLETE
2021-05-07 20:49:14.322[reactor-http-nio-4] DEBUG httpPkg.client - [id:e623e241] REGISTERED
2021-05-07 20:49:14.341[reactor-http-nio-4] DEBUG httpPkg.client - [id:e623e241] CONNECT: /127.0.0.1:9090
2021-05-07 20:49:14.343[reactor-http-nio-4] DEBUG httpPkg.client - [id:e623e241, L:/127.0.0.1:51389 - R:/127.0.0.1:9090] ACTIVE
2021-05-07 20:49:14.363[reactor-http-nio-4] DEBUG httpPkg.client - [id:e623e241-1, L:/127.0.0.1:51389 - R:/127.0.0.1:9090] WRITE: 386B GET /TestWebApp/testServlet HTTP/1.1
serviceName: s2
Content-Type: text/plain
Cache-Control: no-cache
Forwarded: proto=http;host="127.0.0.1:8070";for="127.0.0.1:51388"
X-Forwarded-For: 127.0.0.1
X-Forwarded-Proto: http
X-Forwarded-Port: 8070
X-Forwarded-Host: 127.0.0.1:8070
user-agent: ReactorNetty/1.0.5
host: 127.0.0.1:9090
accept: */*
content-length: 13
hello world
2021-05-07 20:49:14.364[reactor-http-nio-4] DEBUG httpPkg.client - [id:e623e241-1, L:/127.0.0.1:51389 - R:/127.0.0.1:9090] FLUSH
2021-05-07 20:49:14.376[reactor-http-nio-4] DEBUG httpPkg.client - [id:e623e241-1, L:/127.0.0.1:51389 - R:/127.0.0.1:9090] READ: 479B HTTP/1.1 200
Set-Cookie: JSESSIONID=BB0DEE6EAC0886DE20EF5D70D5168656; Path=/TestWebApp; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Length: 280
Date: Fri, 07 May 2021 12:49:14 GMT
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<br /> |0|
<br /> 测试一下
<br /> 1
<br /> 4
<br />
<br />
is 0
<br /> 1
<br /> 测试一下来一下1
<br /> 1
<br /> 8
</body>
</html>
2021-05-07 20:49:14.395[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46-1, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] WRITE: 201B HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=BB0DEE6EAC0886DE20EF5D70D5168656; Path=/TestWebApp; HttpOnly
Content-Type: text/html;charset=UTF-8
Date: Fri, 07 May 2021 12:49:14 GMT
content-length: 280
2021-05-07 20:49:14.395[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46-1, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] FLUSH
2021-05-07 20:49:14.397[reactor-http-nio-4] DEBUG httpPkg.client - [id:e623e241, L:/127.0.0.1:51389 - R:/127.0.0.1:9090] READ COMPLETE
2021-05-07 20:49:14.399[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46-1, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] WRITE: 280B
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<br /> |0|
<br /> 测试一下
<br /> 1
<br /> 4
<br />
<br />
is 0
<br /> 1
<br /> 测试一下来一下1
<br /> 1
<br /> 8
</body>
</html>
2021-05-07 20:49:14.399[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46-1, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] FLUSH
2021-05-07 20:49:14.411[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46-1, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] WRITE: 0B
2021-05-07 20:49:14.411[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46-1, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] FLUSH
2021-05-07 20:49:14.429[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46, L:/127.0.0.1:8070 - R:/127.0.0.1:51388] READ COMPLETE
2021-05-07 20:49:14.430[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46, L:/127.0.0.1:8070 ! R:/127.0.0.1:51388] INACTIVE
2021-05-07 20:49:14.430[reactor-http-nio-2] DEBUG httpPkg.server - [id:45419f46, L:/127.0.0.1:8070 ! R:/127.0.0.1:51388] UNREGISTERED
2021-05-07 20:49:34.499[reactor-http-nio-4] DEBUG httpPkg.client - [id:e623e241, L:/127.0.0.1:51389 - R:/127.0.0.1:9090] READ COMPLETE
2021-05-07 20:49:34.499[reactor-http-nio-4] DEBUG httpPkg.client - [id:e623e241, L:/127.0.0.1:51389 ! R:/127.0.0.1:9090] INACTIVE
2021-05-07 20:49:34.499[reactor-http-nio-4] DEBUG httpPkg.client - [id:e623e241, L:/127.0.0.1:51389 ! R:/127.0.0.1:9090] UNREGISTERED
其中,1、2中的参数 AdvancedByteBufFormat.TEXTUAL是关键,它指定生成文本。如果不指定,它的默认值为 AdvancedByteBufFormat.HEX_DUMP,即显示二进制。
更多推荐
已为社区贡献8条内容
所有评论(0)