如何在分布式Vertx系统中关联日志事件 [英] How to correlate log events in distributed Vertx system
问题描述
在vertx的多个模块中进行日志记录时,基本要求是我们应该能够将单个请求的所有日志相关联.
while doing logs in the multiple module of vertx, it is a basic requirement that we should be able to correlate all the logs for a single request.
由于vertx是异步的,因此保留logid,conversationid和eventid的最佳位置.
as vertx being asynchronous what will be the best place to keep logid, conversationid, eventid.
我们可以实现的任何解决方案或模式吗?
any solution or patterns we can implement?
推荐答案
在基于线程的系统中,当前上下文由当前线程保存,因此MDC或任何ThreadLocal都可以.
In a thread based system, you current context is held by the current thread, thus MDC or any ThreadLocal would do.
在基于Actor的系统(例如Vertx)中,您的上下文就是消息,因此您必须在发送的每条消息中添加一个关联ID.
In an actor based system such as Vertx, your context is the message, thus you have to add a correlation ID to every message you send.
对于任何处理程序/回调,您都必须将其作为方法参数传递或引用最终的方法变量.
For any handler/callback you have to pass it as method argument or reference a final method variable.
要通过事件总线发送消息,可以将有效载荷包装在JsonObject中,然后将相关性ID添加到包装对象中
For sending messages over the event bus, you could either wrap your payload in a JsonObject and add the correlation id to the wrapper object
vertx.eventBus().send("someAddr",
new JsonObject().put("correlationId", "someId")
.put("payload", yourPayload));
,或者您可以使用DeliveryOption
//send
vertx.eventBus().send("someAddr", "someMsg",
new DeliveryOptions().addHeader("correlationId", "someId"));
//receive
vertx.eventBus().consumer("someAddr", msg -> {
String correlationId = msg.headers().get("correlationId");
...
});
还有更多更复杂的选择,例如在事件总线上使用拦截器,Emanuel Idi曾使用该拦截器对Vert.x实现Zipkin支持,
There are also more sophisticated options possible, such as using an Interceptor on the eventbus, which Emanuel Idi used to implement Zipkin support for Vert.x, https://github.com/emmanuelidi/vertx-zipkin, but I'm not sure about the current status of this integration.
这篇关于如何在分布式Vertx系统中关联日志事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!