为了让大家更好地了解 "gateway、webflux、reactor-netty请求日志输出方式",我将分别讲解这三个主题,并提供相应的示例代码,在此之前,请确保已经安装好了Java环境,并了解基本的Spring Boot框架。
Gateway请求日志输出方式
Gateway是Spring Cloud的组件之一,可以将多个微服务组合起来作为一个整体对外提供服务。在Gateway中,一般会使用Spring Boot的内置日志系统Logback进行日志输出。为了打印HTTP请求日志,我们需要在Logback的配置文件中添加以下代码:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.cloud.gateway" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
该配置文件中定义了一个名为STDOUT的输出源,将日志输出到标准控制台上,并指定了日志输出的格式。另外,还定义了一个名为org.springframework.cloud.gateway的日志记录器,将级别设置为DEBUG,表示输出所有的debug级别日志。
示例1:当用户访问http://localhost:8080/hello时,输出如下日志:
16:23:42.600 [reactor-http-epoll-5] DEBUG o.s.c.g.h.p.RoutePredicateFactory - RouteDefinition matched: Route{id='0', uri='http://localhost:8081/hello', order=0, predicates=[{GET /hello}], filters=[]}
16:23:42.600 [reactor-http-epoll-5] DEBUG o.s.c.g.f.r.GatewayFilterChain - RouteDefinition{id='0', uri='http://localhost:8081/hello', order=0, predicates=[{GET /hello}], filters=[]} matches request http://localhost:8080/hello
16:23:42.601 [reactor-http-epoll-5] DEBUG o.s.c.g.f.r.RewritePathGatewayFilterFactory - Rewrote HTTP path from "" to "/hello"
16:23:42.602 [reactor-http-epoll-5] DEBUG o.s.c.g.f.w.r.RoutingWebFilter - Route matched: Route{id='0', uri='http://localhost:8081/hello', order=0, predicates=[{GET /hello}], filters=[]}
16:23:42.603 [reactor-http-epoll-5] DEBUG o.s.c.g.f.LoggingGatewayFilterFactory - [80fb7b97-1] [] Received HTTP GET request on http://localhost:8080/hello
16:23:42.603 [reactor-http-epoll-5] DEBUG o.s.c.g.f.LoggingGatewayFilterFactory - [80fb7b97-1] [] [Elapsed time: 0ms]
16:23:42.603 [reactor-http-epoll-5] DEBUG o.s.c.g.f.r.ReadBodyPredicateFactory - Reading body for content-based routing
WebFlux请求日志输出方式
WebFlux是Spring 5中新增的响应式编程框架,其内置的日志系统也是Logback。默认情况下,WebFlux处理器并不会输出HTTP请求日志,因此需要通过配置日志记录器的方式进行输出。具体可以在Logback配置文件中添加以下代码:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.web.reactive" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
与Gateway类似,该配置文件中定义了一个名为STDOUT的输出源,并指定了日志输出的格式。另外,还定义了一个名为org.springframework.web.reactive的日志记录器,将级别设置为DEBUG,表示输出所有的debug级别日志。
示例2:当用户访问http://localhost:8080/hello时,输出如下日志:
2020-01-06 16:27:54.174 DEBUG 23184 --- [ctor-http-nio-3] o.s.w.r.handler.SimpleUrlHandlerMapping : [] Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2020-01-06 16:27:54.206 DEBUG 23184 --- [ctor-http-nio-3] o.s.w.r.f.s.ServerWebExchangeUtils : [] Using gateway exchange with ThreadPoolExecutorTaskExecutor [java.util.concurrent.ThreadPoolExecutor@afaef0b[ThreadNamePrefix=reactor-http, PoolSize=20, ActiveThreads=3, QueueCapacity=1000]] and ServerWebExchange create
2020-01-06 16:27:54.248 DEBUG 23184 --- [ctor-http-nio-3] o.s.w.s.adapter.HttpWebHandlerAdapter : [] [80fb7b97-3] HTTP GET "/hello"
2020-01-06 16:27:54.266 DEBUG 23184 --- [ctor-http-nio-3] o.s.w.r.f.s.ServerWebExchangeUtils : [] Preparing response for ServerWebExchange [id = 7]
2020-01-06 16:27:54.273 DEBUG 23184 --- [ctor-http-nio-3] o.s.w.r.handler.SimpleUrlHandlerMapping : [] Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
Reactor-Netty请求日志输出方式
Reactor-Netty是Spring WebFlux的底层网络框架,它使用SLF4J作为日志系统。默认情况下,Reactor-Netty并不会输出HTTP请求日志,因此需要通过配置日志记录器的方式进行输出。具体可以在Logback配置文件中添加以下代码:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="reactor.netty" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
与上述两个例子略有不同,该配置文件中定义了一个名为reactor.netty的日志记录器,将级别设置为DEBUG,表示输出所有的debug级别日志。
示例3:当用户访问http://localhost:8080/hello时,输出如下日志:
13:47:12.868 [reactor-http-nio-1] DEBUG reactor.netty.tcp.TcpClient - [id: 0x81f5cab6] REGISTERED
13:47:12.868 [reactor-http-nio-1] DEBUG reactor.netty.tcp.TcpClient - [id: 0x81f5cab6] CONNECT: localhost/127.0.0.1:8081
13:47:12.870 [reactor-http-nio-1] DEBUG reactor.netty.tcp.TcpClient - [id: 0x81f5cab6, L:/0:0:0:0:0:0:0:0:49990 - R:localhost/127.0.0.1:8081] ACTIVE
13:47:12.874 [reactor-http-nio-1] DEBUG reactor.netty.tcp.TcpClient - [id: 0x81f5cab6, L:/0:0:0:0:0:0:0:0:49990 - R:localhost/127.0.0.1:8081] WRITE: 67B
13:47:12.881 [reactor-http-nio-1] DEBUG reactor.netty.tcp.TcpClient - [id: 0x81f5cab6, L:/0:0:0:0:0:0:0:0:49990 - R:localhost/127.0.0.1:8081] FLUSH
13:47:12.883 [reactor-http-nio-1] DEBUG reactor.netty.tcp.TcpClient - [id: 0x81f5cab6, L:/0:0:0:0:0:0:0:0:49990 - R:localhost/127.0.0.1:8081] READ: 67B
13:47:12.888 [reactor-http-nio-1] DEBUG reactor.netty.tcp.TcpClient - [id: 0x81f5cab6, L:/0:0:0:0:0:0:0:0:49990 - R:localhost/127.0.0.1:8081] READ COMPLETE
至此,我们已经掌握了"gateway、webflux、reactor-netty请求日志输出方式"的完整攻略,包括了对三个主题的详细讲解和示例代码的提供。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:gateway、webflux、reactor-netty请求日志输出方式 - Python技术站