本文介绍了在Spring Boot 2中如何在hystrix中使用MDC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在我们的Spring Boot应用程序中使用hystrix.我们希望使用MDC将特定属性(例如请求ID和请求URI以及登录用户)附加到每个日志语句中.无论何时实施hystrix,此机制均不起作用.

We are using hystrix in our spring boot application. We wants to use MDC for appending specific attributes like Request Id and Request URI and Loggedin User to every log statement. This mechanism is not working wherever hystrix is implemented.

@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");
}

对于非带注释的方法,它工作得很好.我确实了解MDC是特定于线程的,并且hystrix确实在单独的线程上执行.请提出解决方法,因为我无法评论hystrix.

Its working perfectly fine for non hystrix annotated methods. I do understand that MDC is thread specific and hystrix does executes on seperate thread. Please suggest the work around as i can't comment the hystrix.

推荐答案

您可以使用HystrixCommandExecutionHook来确保ThreadLocalMDC变量在所有hystrix线程中的流动.

You can use HystrixCommandExecutionHook to ensure flow of ThreadLocal or MDC variables across all hystrix threads.

您可以通过netflix找到完整的文档这里.

You can find complete documentation by netflix here.

有关如何使用挂钩来解决您的目的的更多帮助,您可以参考此博客

For more help on how to use hook to solve your purpose, you can refer to this blog

在博客中,您可以使用MDC.get("key")& MDC.set("key", value)代替getThreadLocals()& setThreadLocals()解决您的目的.

In the blog, you can use MDC.get("key") & MDC.set("key", value) instead of getThreadLocals() & setThreadLocals() to solve your purpose.

这篇关于在Spring Boot 2中如何在hystrix中使用MDC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 11:03