Zipkin/Brave 整合Spring-MVC 框架实战

1. Zipkin是啥

Zipkin是一个分布式追踪系统,可以有效的在微服务架构中发现延时问题,是基于Google Dapper论文设计的。其源码位于Github的OpenZipkin/Zipkin

2. Brave是啥

Brave充当了Zipkin 系统中的Collector,把服务中的一些数据封装成Span实时的发送到Zipkin中去。其名字由 荷兰语中的Dapper翻译而来。

3. Brave 能干啥

Brave 可以监听到你的服务中的http请求,并且把会给每个请求创建一个spanId和tracingId,并且把这两个Id存放到ThreadLocal中,在该请求处理结束之前,你的log里都可以获取到这两个Id,即使不使用外部带(out-of-band)的zipkin,也可以很方便的追踪本地请求处理链条。

4. 整合Spring-MVC

上实战。

  4.1 pom文件中添加如下依赖


        
            io.zipkin.brave
            brave-spring-beans
        

        
            io.zipkin.brave
            brave
        

        
            io.zipkin.brave
            brave-context-slf4j
        

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

  4.2 项目中添加Tracing配置类

import brave.Tracing;
import brave.context.slf4j.MDCScopeDecorator;
import brave.http.HttpTracing;
import brave.propagation.B3Propagation;
import brave.propagation.ExtraFieldPropagation;
import brave.propagation.ThreadLocalCurrentTraceContext;
import brave.servlet.TracingFilter;
import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.servlet.Filter;

/**
 * @Author: Yoruichi
 * @Date: 2018/11/14 6:27 PM
 */
@Configuration
@Import(SpanCustomizingAsyncHandlerInterceptor.class)
public class TracingConfig {

    private static final Logger log = LoggerFactory.getLogger(TracingConfig.class);

    @Bean
    public Tracing tracing() {
        return Tracing.newBuilder()
//配置本地服务名称
                .localServiceName("API-Service")
//配置propagation工厂类
                .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "SIGNATURE"))
//配置slf4j的MDC                
                .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder().addScopeDecorator(MDCScopeDecorator.create()).build())
//配置span reporter 这里是打印span到log文件,默认是打印到console,如果要发送到zipkin服务需要在这里配置sender
                .spanReporter(span -> log.info("{}", span))
                .build();
    }

    /**
     * decides how to name and tag spans. By default they are named the same as the http method.
     */
    @Bean
    HttpTracing httpTracing(Tracing tracing) {
        return HttpTracing.create(tracing);
    }

    /**
     * Creates server spans for http requests
     */
    @Bean
    Filter tracingFilter(HttpTracing httpTracing) {
        return TracingFilter.create(httpTracing);
    }

}

  4.3 在web.xml中添加Filter配置

  
    tracingFilter
    brave.spring.webmvc.DelegatingTracingFilter
  
  
    tracingFilter
    /*
  

  4.4 在application-content.xml中添加interceptor配置


        
            
            
        

  interceptor也可以通过Java代码方式添加配置,具体方式此处略。

4.5 在logback中增加配置


 

做好上面这些配置工作之后启动你的服务,发送请求你可以在你的log文件中看到对应的信息了

2018-11-15 14:35:12 - [77849f28f2458165/77849f28f2458165] - [INFO] - [SignatureInterceptor.java:76] - [http-nio-7305-exec-1] - preHandle - c.s.e.a.i.SignatureInterceptor - requestURI=/api/v1/tracing
2018-11-15 14:35:12 - [77849f28f2458165/77849f28f2458165] - [INFO] - [SignatureInterceptor.java:77] - [http-nio-7305-exec-1] - preHandle - c.s.e.a.i.SignatureInterceptor - queryString=null
2018-11-15 14:35:12 - [77849f28f2458165/77849f28f2458165] - [INFO] - [SignatureInterceptor.java:78] - [http-nio-7305-exec-1] - preHandle - c.s.e.a.i.SignatureInterceptor - body=
2018-11-15 14:35:12 - [77849f28f2458165/77849f28f2458165] - [INFO] - [Logger.java:193] - [http-nio-7305-exec-1] - info - c.s.e.a.controller.BaseController - #tracing(): in 370.20ms

 

你可能感兴趣的:(working)