简体   繁体   English

如何在 Spring Boot 2 中将 MDC 与 hystrix 一起使用?

[英]How to use MDC in with hystrix in Spring Boot 2?

We are using hystrix in our spring boot application.我们在 Spring Boot 应用程序中使用 hystrix。 We wants to use MDC for appending specific attributes like Request Id and Request URI and Loggedin User to every log statement.我们希望使用 MDC 将特定属性(如请求 ID、请求 URI 和登录用户)附加到每个日志语句。 This mechanism is not working wherever hystrix is implemented.这种机制在任何实施 hystrix 的地方都不起作用。

@HystrixCommand(fallbackMethod = "callFallback", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000")}, ignoreExceptions = Exception.class)
public GenericResponse callUser(User user) {
  //Implementation
   log.info("Creating user called");
}

Its working perfectly fine for non hystrix annotated methods.对于非 hystrix 注释方法,它工作得非常好。 I do understand that MDC is thread specific and hystrix does executes on seperate thread.我知道 MDC 是特定于线程的,而 hystrix 确实在单独的线程上执行。 Please suggest the work around as i can't comment the hystrix.请建议解决方法,因为我无法评论 hystrix。

You can use HystrixCommandExecutionHook to ensure flow of ThreadLocal or MDC variables across all hystrix threads.您可以使用HystrixCommandExecutionHook来确保ThreadLocalMDC变量在所有 hystrix 线程中的流动。

You can find complete documentation by netflix here .您可以在此处找到 Netflix 的完整文档。

For more help on how to use hook to solve your purpose, you can refer to this blog更多关于如何使用 hook 来解​​决你的目的的帮助,你可以参考这个博客

In the blog, you can use MDC.get("key") & MDC.set("key", value) instead of getThreadLocals() & setThreadLocals() to solve your purpose.在博客中,您可以使用MDC.get("key") & MDC.set("key", value)而不是getThreadLocals() & setThreadLocals()来解决您的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM