ZipKin原理学习--zipkin支持日志打印追踪信息

     目前在zipkin brave已经提供功能在我们使用logback或log4j等时可以在日志信息中将traceId和spanId等信息打印到运行日志,这样可能对我们通过日志查看解决问题有比较大的帮助。
  
  示例:
  
  pom.xml中添加zipkin相关jar
  
  
  
   io.zipkin.brave
  
   brave-instrumentation-spring-web
  
   5.4.3
  
  

  
  
  
  io.zipkin.brave
  
  brave
  
  5.4.3
  
  

  
  
  
  io.zipkin.brave
  
  brave-context-slf4j
  
  5.4.3
  
  

  
  
  
   io.zipkin.brave
  
   brave-instrumentation-spring-webmvc
  
   5.4.3
  
  

  
  注入zip相关bean:
  
  Configuration
  
  @Import({
  
  TracingClientHttpRequestInterceptor.class,
  
  SpanCustomizingAsyncHandlerInterceptor.class
  
  })
  
  public class TracingConfiguration extends WebMvcConfigurerAdapter {
  
  @Bean
  
  public DelegatingTracingFilter getDelegatingTracingFilter() {
  
  return new DelegatingTracingFilter();
  
  }
  
  @Bean
  
  Sender sender() {
  
   return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans");
  
  }
  
  @Bean
  
  AsyncReporter spanReporter() {
  
   return AsyncReporter.create(sender());
  
  }
  
  @Bean
  
  Tracing tracing(@Value("${zipkin.service:brave-webmvc-example}") String serviceName) {
  
   return Tracing.newBuilder()
  
  .localServiceName(serviceName)
  
  .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name"))
  
  .spanReporter(spanReporter()).currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
  
  //添加日志相关的处理器
  
  .addScopeDecorator(MDCScopeDecorator.create()).build()).build();
  
  }
  
  @Bean
  
  HttpTracing httpTracing(Tracing tracing) {
  
   return HttpTracing.create(tracing);
  
  }
  
  @Autowired
  
  TracingClientHttpRequestInterceptor clientInterceptor;
  
  @Bean
  
  public RestTemplate restTemplate() {
  
   RestTemplate restTemplate = new RestTemplate();
  
   List interceptors =new ArrayList<>(restTemplate.getInterceptors());
  
   interceptors.add(clientInterceptor);
  
   restTemplate.setInterceptors(interceptors);
  
   return restTemplate;
  
  }
  
  @Autowired
  
  SpanCustomizingAsyncHandlerInterceptor serverInterceptor;
  
  @Override
  
  public void addInterceptors(InterceptorRegistry registry) {
  
   registry.addInterceptor(serverInterceptor);
  
  }
  
  }
  
  运行日志:
  
  @RequestMapping("start")
  
  public String start(HttpServletRequest request1,HttpServletResponse response1) throws InterruptedException, IOException {
  
  logger.info("start");
  
  String data = restTemplate.getForObject("http://localhost:9090/foo", String.class);
  
  return data;
  
  }
  
  logback.xml相关配置:
  
  主要添加这个打印格式:
  
  
  
  logback.xml示例:
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  INFO
  
  

  
  
  
  ${CONSOLE_LOG_PATTERN}
  
  utf8
  
  

  
  

  
  
  
  
  
  ${LOG_FILE}
  
  
  
  ${LOG_FILE}.www.hengy178.com%d{yyyy-MM-dd}.gz
  
  7
  
  

  
  
  
  ${CONSOLE_LOG_PATTERN}
  
  utf8
  
  

  
  

  
  
  
  
  
  
  
  
  
  

  
  

  
  日志打印信息:
  
  2018-10-20 21:24:28,656 [af49581a0f632677/af49581a0f632677] [http-nio-8080-exec-1] INFO c.t.controller.HomeController - start

你可能感兴趣的:(ZipKin原理学习--zipkin支持日志打印追踪信息)