dubbo服务间日志追踪解决方案
技术背景
springboot 集成dubbo,logback , 主要组件MDC,RpcContext,实现日志追踪。
业务场景
RPC服务间调用,消费端和服务端标记追踪ID记录,前后保持一致,方便查找、监控某个业务的完整的执行流程,比如:客户端发起下单操作,后端要经过扣款服务,减库存服务,商品服务,用户服务,一套流程走下来,可能业务流程非常多,且要经过多个系统,如果日志记录的不是特别详细,可能查找问题特别慢。
步骤:
- 将拦截器配置到应用中
也可以配置多个拦截器类,比如登录校验等
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class SessionConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TraceIdInterceptor()).addPathPatterns("/**");
}
}
- spring拦截器类
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
/**
* 日志追踪
*/
@Component
public class TraceIdInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response