Kis*_*ani 5 logging distributed vert.x
在vertx的多个模块中执行日志时,基本要求是我们应该能够将单个请求的所有日志关联起来。
由于vertx是异步的,因此保留logid,conversationid和eventid的最佳位置。
我们可以实施任何解决方案或模式吗?
令人惊讶的是,关于这个问题却缺乏好的答案,考虑到它是如此简单,这很奇怪。
假设您在收到请求或消息时在 MDC 上下文中设置correlationId,我发现传播它的最简单方法是使用拦截器在上下文之间传递值:
vertx.eventBus()
.addInboundInterceptor(deliveryContext -> {
MultiMap headers = deliveryContext.message().headers();
if (headers.contains("correlationId")) {
MDC.put("correlationId", headers.get("correlationId"));
deliveryContext.next();
}
})
.addOutboundInterceptor(deliveryContext -> {
deliveryContext.message().headers().add("correlationId", MDC.get("correlationId"));
deliveryContext.next();
});
Run Code Online (Sandbox Code Playgroud)
在基于线程的系统中,当前上下文由当前线程保存,因此MDC或任何ThreadLocal都可以。
在基于参与者的系统(例如Vertx)中,您的上下文就是消息,因此您必须在发送的每条消息中添加一个关联ID。
对于任何处理程序/回调,您都必须将其作为方法参数传递或引用最终的方法变量。
为了通过事件总线发送消息,您可以将有效负载包装在JsonObject中,然后将相关性ID添加到包装对象中
vertx.eventBus().send("someAddr",
new JsonObject().put("correlationId", "someId")
.put("payload", yourPayload));
Run Code Online (Sandbox Code Playgroud)
或者您可以使用 DeliveryOption
//send
vertx.eventBus().send("someAddr", "someMsg",
new DeliveryOptions().addHeader("correlationId", "someId"));
//receive
vertx.eventBus().consumer("someAddr", msg -> {
String correlationId = msg.headers().get("correlationId");
...
});
Run Code Online (Sandbox Code Playgroud)
还有更多更复杂的选择,例如在事件总线上使用Interceptor,Emanuel Idi曾使用该拦截器来实现对Vert.x的Zipkin支持,https://github.com/emmanuelidi/vertx-zipkin,但我不确定有关此集成的当前状态。
| 归档时间: |
|
| 查看次数: |
1192 次 |
| 最近记录: |