- 实现思路:
通过向log4j中增加唯一ID,并将此ID在不同模块中传递下去,实现流水追踪的功能。 - 关键技术:
1、log4j : 使用MDC封装唯一ID到当前线程中,并在log4j的配置文件里输出MDC的key,这样首先在web层,就已经可以看到transId了。
2、RequestInterceptor:实现接口RequestInterceptor 拦截器,实现apply方法向请求头中写入MDC中装载的唯一ID,这样请求头中就带着我们的transId了。
3、HandlerInterceptorAdapter :继承类 HandlerInterceptorAdapter 拦截器,实现preHandle方法:在业务处理器处理请求之前被调用;在这时,取出请求头中的transId,并重复第一步。 - 总结:
通过MDC,实现日志输出;通过拦截器,设置请求头参数,并在下游拦截并取出请求头,设置到自己的流程中。
通过MDC,实现日志输出;通过拦截器,设置请求头参数,并在下游拦截并取出请求头,设置到自己的流程中。
[Tōngguò MDC, shíxiàn rìzhì shūchū; tōngguò lánjié qì, shèzhì qǐngqiú tóu cānshù, bìng zài xiàyóu lánjié bìng qǔchū qǐngqiú tóu, shèzhì dào zìjǐ de liúchéng zhōng.]
Through the MDC, the log output is realized; through the interceptor, the request header parameter is set, and the request header is intercepted and taken out downstream, and set to its own process.