使用 spring webclient 对 http 请求进行可读的调试日志记录

2024-05-09

我正在使用 Spring 反应式 WebClient 向 http 服务器发送请求。为了查看正在发送的底层请求和响应,我启用了调试日志记录reactor.ipc.netty包裹。

可以正常查看传出请求的标头。

虽然我通过 http 发送和接收纯文本,但日志包含以下格式的请求和响应(是十六进制吗?)

我不确定如何以易于理解的方式查看记录的数据。最好以易于理解的方式记录请求和响应

这是记录数据的片段

         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 47 45 54 20 2f 53 65 61 72 63 68 5f 47 43 2e 61 |GET /Search_GC.a|
|00000010| 73 70 78 20 48 54 54 50 2f 31 2e 31 0d 0a 75 73 |spx HTTP/1.1..us|
|00000020| 65 72 2d 61 67 65 6e 74 3a 20 52 65 61 63 74 6f |er-agent: Reacto|
|00000030| 72 4e 65 74 74 79 2f 30 2e 37 2e 32 2e 52 45 4c |rNetty/0.7.2.REL|
|00000040| 45 41 53 45 0d 0a 68 6f 73 74 3a 20 63 65 6f 6b |EASE..host: ceok|
|00000050| 61 72 6e 61 74 61 6b 61 2e 6b 61 72 2e 6e 69 63 |arnataka.kar.nic|
|00000060| 2e 69 6e 0d 0a 61 63 63 65 70 74 3a 20 2a 2f 2a |.in..accept: */*|
|00000070| 0d 0a 61 63 63 65 70 74 2d 65 6e 63 6f 64 69 6e |..accept-encodin|
|00000080| 67 3a 20 67 7a 69 70 0d 0a 63 6f 6e 74 65 6e 74 |g: gzip..content|
|00000090| 2d 6c 65 6e 67 74 68 3a 20 30 0d 0a 0d 0a       |-length: 0....  |
+--------+-------------------------------------------------+----------------+

发现由于同一个库而必须发生的未解答的问题:读取具有 PooledUnsafeDirectByteBuf 的 HttpContent https://stackoverflow.com/questions/44565005/reading-a-httpcontent-that-has-a-pooledunsafedirectbytebuf?rq=1

提出了一个问题here https://github.com/reactor/reactor-netty/issues/233

似乎有一种正统的观点认为反应式客户端不需要调试。这是一个完全没有意义的争论,因为我们使用像这样的工具rest client, postman, curl, httpie和其他人发送请求并查看响应


他们改变了reactor.netty.http.client.HttpClient https://projectreactor.io/docs/netty/release/api/reactor/netty/http/client/HttpClient.html类,在我升级到io.projectreactor.netty:reactor-netty-http:1.0.5以下代码是可编译的并且可以执行您所期望的操作。 (我不确定哪个是minimal版本,我从旧版本升级,但我想它是1.0.0。这是一个传递依赖,我升级了spring-boot-starter-webflux from 2.3.4.RELEASE to 2.4.4.)

关键部分是调用wiretap() https://projectreactor.io/docs/netty/release/api/reactor/netty/transport/Transport.html#wiretap-java.lang.String-io.netty.handler.logging.LogLevel-reactor.netty.transport.logging.AdvancedByteBufFormat-java.nio.charset.Charset-:

wiretap("reactor.netty.http.client.HttpClient", LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL, StandardCharsets.UTF_8)

它还记录请求和响应的标头和正文。

整个上下文是这样的:

package com.example;

import io.netty.handler.logging.LogLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
import reactor.netty.transport.logging.AdvancedByteBufFormat;

import java.nio.charset.StandardCharsets;

@Slf4j
class RestClientTest {
    private WebClient createWebClient() {
        final HttpClient httpClient = HttpClient.create()
                .wiretap(HttpClient.class.getCanonicalName(), LogLevel.DEBUG, AdvancedByteBufFormat.TEXTUAL, StandardCharsets.UTF_8);
        return WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .build();
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    private static class User {
        int id;
        int userId;
        String title;
        String body;
    }

    @Test
    void createUsersReactive() {
        final WebClient webClient = createWebClient();
        final String url = "http://jsonplaceholder.typicode.com/posts";
        final Mono<User> userMono = webClient.method(HttpMethod.POST)
                .uri(url)
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .header("X-My-Header", "MyValue1", "MyValue2")
                .body(BodyInserters.fromValue(User.builder().userId(1).title("foo").body("bar").build()))
                .retrieve()
                .bodyToMono(User.class);
        final User user = userMono.block();
        log.info("Created user: " + user);
    }
}

并且根据您的要求,日志输出是人类可读的:

... reactor.netty.http.client.HttpClient     : [id:e7d7ed93] REGISTERED
... reactor.netty.http.client.HttpClient     : [id:e7d7ed93] CONNECT: jsonplaceholder.typicode.com/<unresolved>:80
... reactor.netty.http.client.HttpClient     : [id:e7d7ed93] ACTIVE
... r.netty.http.client.HttpClientConnect    : [id:e7d7ed93-1] Handler is being applied: {uri=http://jsonplaceholder.typicode.com/posts, method=POST}
... reactor.netty.http.client.HttpClient     : [id:e7d7ed93-1] WRITE: 217B POST /posts HTTP/1.1
user-agent: ReactorNetty/1.0.5
host: jsonplaceholder.typicode.com
accept: */*
Content-Type: application/json;charset=UTF-8
X-My-Header: MyValue1
X-My-Header: MyValue2
content-length: 46


... reactor.netty.http.client.HttpClient     : [id:e7d7ed93-1] WRITE: 46B {"id":0,"userId":1,"title":"foo","body":"bar"}
... reactor.netty.http.client.HttpClient     : [id:e7d7ed93-1] FLUSH
... reactor.netty.http.client.HttpClient     : [id:e7d7ed93-1] READ: 1347B HTTP/1.1 201 Created
Date: Tue, 13 Apr 2021 12:49:33 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 65
X-Powered-By: Express
X-Ratelimit-Limit: 1000
X-Ratelimit-Remaining: 999
X-Ratelimit-Reset: 1618318233
Vary: Origin, X-HTTP-Method-Override, Accept-Encoding
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Access-Control-Expose-Headers: Location
Location: http://jsonplaceholder.typicode.com/posts/101
X-Content-Type-Options: nosniff
Etag: W/"41-0LtsWqhuQ7Zsjlj0tYnOrT/Vw5o"
Via: 1.1 vegur
CF-Cache-Status: DYNAMIC
cf-request-id: 096ce0bd560000736722853000000001
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=laAKjgGcoi8SLu%2F6VX5pQIAksdmj9xi31elC5Ld97eljznKIpYjdkQsittoMJp3lJoQIwOACmj89bKSa%2Ff15gRHRmyasV2Xcl%2FmVjJBJm7ytbWocp39UBd90JwVM"}],"max_age":604800,"group":"cf-nel"}
NEL: {"max_age":604800,"report_to":"cf-nel"}
Server: cloudflare
CF-RAY: 63f4d0a88ed07367-CPH
alt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Set-Cookie: __cfduid=d11c86fbd953f7cf768cf7db0c346f22b1618318173; expires=Thu, 13-May-21 12:49:33 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax

{
  "id": 101,
  "userId": 1,
  "title": "foo",
  "body": "bar"
}
... r.n.http.client.HttpClientOperations     : [id:e7d7ed93-1] Received response (auto-read:false) : [Date=Tue, 13 Apr 2021 12:49:33 GMT, Content-Type=application/json; charset=utf-8, X-Powered-By=Express, X-Ratelimit-Limit=1000, X-Ratelimit-Remaining=999, X-Ratelimit-Reset=1618318233, Vary=Origin, X-HTTP-Method-Override, Accept-Encoding, Access-Control-Allow-Credentials=true, Cache-Control=no-cache, Pragma=no-cache, Expires=-1, Access-Control-Expose-Headers=Location, Location=http://jsonplaceholder.typicode.com/posts/101, X-Content-Type-Options=nosniff, Etag=W/"41-0LtsWqhuQ7Zsjlj0tYnOrT/Vw5o", Via=1.1 vegur, CF-Cache-Status=DYNAMIC, cf-request-id=096ce0bd560000736722853000000001, Report-To={"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=laAKjgGcoi8SLu%2F6VX5pQIAksdmj9xi31elC5Ld97eljznKIpYjdkQsittoMJp3lJoQIwOACmj89bKSa%2Ff15gRHRmyasV2Xcl%2FmVjJBJm7ytbWocp39UBd90JwVM"}],"max_age":604800,"group":"cf-nel"}, NEL={"max_age":604800,"report_to":"cf-nel"}, Server=cloudflare, CF-RAY=63f4d0a88ed07367-CPH, alt-svc=h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, Proxy-Connection=Keep-Alive, Connection=Keep-Alive, Set-Cookie=__cfduid=d11c86fbd953f7cf768cf7db0c346f22b1618318173; expires=Thu, 13-May-21 12:49:33 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax, content-length=65]
... r.n.http.client.HttpClientOperations     : [id:e7d7ed93-1] Received last HTTP packet
... reactor.netty.http.client.HttpClient     : [id:e7d7ed93] READ COMPLETE
... com.example.RestClientTest              : Created user: RestClientIT.User(id=101, userId=1, title=foo, body=bar)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 spring webclient 对 http 请求进行可读的调试日志记录 的相关文章

随机推荐

  • Android Material主题alpha颜色问题

    我已经创建了一个构建版本为 5 0 的应用程序 我在下面编写了主题
  • ruby中如何获取屏幕分辨率

    如何在ruby脚本中获取屏幕分辨率 高度 宽度 在 Linux 上 x y xrandr scan current d x d flatten 在 Windows 上 使用WIN32OLE等 http www ruby forum com
  • Meteor - 使用 Meteor.wrapAsync() 包装 NPM

    我正在尝试使用 Meteor wrapAsync 包装超级代理 NPM 一切正常 直到下面代码的最后一行 这导致我的流星应用程序崩溃 var superagent Meteor npmRequire superagent Example o
  • 禁用 Azure 应用服务上的某些 W3C 日志记录字段

    在 Azure 应用服务中记录 Web 服务器日志时 默认情况下会记录每个字段 并且似乎没有任何方法可以禁用特定字段 有没有解决的办法 或者我错过了什么 只要 applicationhost config 中的 IIS 支持 您就可以使用X
  • Android 中的 XmlPullParser 陷入困境

    经过多个小时的搜索和调试后 我仍然停留在同一个地方 并且 Eclipse 没有帮助我 我试图解析这个 RSS 提要 http fr espnf1 com rss motorsport story feeds 0 xml type 2 这很简
  • 通过 cmake 链接 libc++ 时 libc++abi 的链接问题

    我正在尝试构建一个简单的 hello world C 使用 LLVM Clang 3 7 0 的程序 根据工具链的源代码构建libc 使用命令行 clang std c 14 stdlib libc fno exceptions hello
  • 如何在MVVM中实现appSettings

    我正在尝试摆脱我使用的警告appSettings在 WPF 项目中 应用程序配置
  • 将文本视图包裹在图像视图周围

    晚上好 我正在尝试将文本包裹在图像视图中 就像这里提出的问题一样 Textview 环绕 View https stackoverflow com questions 3626750 textview wrap around view 在风
  • 悬停时 jQuery 动画边框颜色?

    Using a 颜色插件 http plugins jquery com project color悬停时设置背景颜色动画 function listing 2 li a mouseover function this animate ba
  • 如何防止 Activity 在后退操作时重新加载

    我有连接到互联网以获取数据的应用程序 我可以多层次访问数据 假设我从第 3 级开始 在第 4 级我决定返回 每当我按回之前的活动时 就会从互联网重新加载数据 有可能阻止这种情况吗 我尝试以单顶模式运行该活动 将数据加载代码移至 single
  • Internet Explorer 8 + 放气

    我有一个非常奇怪的问题 我真的希望有人能给出答案 因为我不知道还能去哪里问 我正在用 C 编写一个 cgi 应用程序 它由 Apache 执行并输出 HTML 代码 我自己在 C 应用程序中压缩 HTML 输出 因为我的 Web 主机由于某
  • 如何在flutter中从设备存储读取CSV文件

    我想将数据从 flutter 中的 CSV 文件导入到 firebase 数据库中 因此 我使用文件选择器从设备中选择 CSV 文件 现在我如何从该文件中读取数据 首先从 dart 包导入 file picker 和 CSV 包 比定义方法
  • 如何向 addrange select 语句添加异步“await”?

    我有一个这样的函数 public async Task
  • Windows 消息循环而不是 QApplication::exec() / QApplication::processEvents()

    我是否想念任何一个Qt如果我替换功能QApplication exec 使用标准 Windows 消息循环实现 这应该可以澄清我的意思 运行事件处理的常用 Qt 方式 int main int argc char argv QApplica
  • asp.net vb 用户控件在调用页面上引发事件

    我正在尝试了解用户控件 我创建了一个具有文本框和按钮的用户控件 我想要做的是 当我单击用户控件中的按钮时 在 aspx 页面中填充标签 我知道我可以在页面上有一个按钮 使用用户控件上的一些属性来获取该信息 但我想知道如何使用用户控件的按钮来
  • 在 Python 中对数据进行求和

    Given that the fitting function is of type 我打算将这样的函数拟合到我拥有的实验数据 x y f x 中 但后来我有一些疑问 当涉及求和时 如何定义拟合函数 一旦定义了函数 即def func re
  • 使用 MongoDB docker 镜像停止虚拟机而不丢失数据

    我已经在 AWS EC2 上的虚拟机中安装了官方的 MongoDB docker 映像 并且数据库上已经有数据 如果我停止虚拟机 以节省过夜费用 我会丢失数据库中包含的所有数据吗 在这些情况下我怎样才能让它持久 有多种选择可以实现此目的 但
  • MapStruct:如何将输入对象传递给表达式?

    在 MapStruct 版本 1 1 0 Final 中 这是可能的 Mappings Mapping target transaction process details expression java MappingHelper map
  • Meteor js:使Session对象部分持久化

    在我的流星应用程序中 我使用会话来存储有关用户活动的临时信息 我想使用 amplify js 将此信息的某些部分保留到浏览器 但不是全部 我想要一种拥有 临时 会话密钥和 持久 会话密钥的方法 例如我可以打电话 Session set pe
  • 使用 spring webclient 对 http 请求进行可读的调试日志记录

    我正在使用 Spring 反应式 WebClient 向 http 服务器发送请求 为了查看正在发送的底层请求和响应 我启用了调试日志记录reactor ipc netty包裹 可以正常查看传出请求的标头 虽然我通过 http 发送和接收纯