zipkin分布式链路追踪

作者: Ian | 2019-06-04 | 阅读

转载 dubbo集成zipkin:https://blog.csdn.net/wenj91/article/details/80926698

介绍

Zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 Google Dapper的论文设计而来,由 Twitter 公司开发贡献。其主要功能是聚集来自各个异构系统的实时监控数据。

架构

ZipKin可以分为两部分,一部分是zipkin server,用来作为数据的采集存储、数据分析与展示;zipkin client是zipkin基于不同的语言及框架封装的一些列客户端工具,这些工具完成了追踪数据的生成与上报功能,架构如下:

Zipkin Server主要包括四个模块:

(1)Collector 接收或收集各应用传输的数据 (2)Storage 存储接受或收集过来的数据,当前支持Memory,MySQL,Cassandra,ElasticSearch等,默认存储在内存中。 (3)API(Query) 负责查询Storage中存储的数据,提供简单的JSON API获取数据,主要提供给web UI使用 (4)Web 提供简单的web界面

ZipKin几个概念

在追踪日志中,有三个最重要的概念: spanId、traceId、parentId

  1. traceId:用来确定一个追踪链的16字符长度的字符串,在某个追踪链中保持不变。
  2. spanId:区域Id,在一个追踪链中spanId可能存在多个,每个spanId用于表明在某个服务中的身份,也是16字符长度的字符串。
  3. parentId:在跨服务调用者的spanId会传递给被调用者,被调用者会将调用者的spanId作为自己的parentId,然后自己再生成spanId。

    其他概念:

  4. name:span的名称,主要用于在界面上展示,一般是接口方法名,name的作用是让人知道它是哪里采集的span,不然某个span耗时高我都不知道是哪个服务节点耗时高
  5. timestamp:span创建时的时间戳,用来记录采集的时刻。
  6. duration:持续时间,即span的创建到span完成最终的采集所经历的时间,除去span自己逻辑处理的时间,该时间段可以理解成对于该跟踪埋点来说服务调用的总耗时
  7. annotations:基本标注列表,一个标注可以理解成span生命周期中重要时刻的数据快照,比如一个标注中一般包含发生时刻(timestamp)、事件类型(value)、端点(endpoint)等信息

事件类型

  1. cs:客户端/消费者发起请求
  2. cr:客户端/消费者接收到应答
  3. sr:服务端/生产者接收到请求
  4. ss:服务端/生产者发送应答
  5. binaryAnnotations:业务标注列表,如果某些跟踪埋点需要带上部分业务数据(比如url地址、返回码和异常信息等),可以将需要的数据以键值对的形式放入到这个字段中。

dubbo集成zipkin

zipkin服务端

wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'
java -jar zipkin.jar

打开浏览器http://your_host:9411就可以看到追踪。

zipkin客户端集成

zipkin tracing配置

import brave.Tracing;
import brave.servlet.TracingFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import zipkin2.Span;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.Reporter;
import zipkin2.reporter.Sender;
import zipkin2.reporter.okhttp3.OkHttpSender;

import javax.servlet.Filter;

@Configuration
public class TracingConfig {

    /**
     * 配置zipkin服务地址
     */
    @Value("${zipkin.tracing.endpoint:http://localhost:9411/api/v2/spans}")
    private String zipkinEndPoint;

    @Value("${zipkin.tracing.local-service-name:local-service-name}")
    private String localServiceName;

    /**
     * 配置sender
     * @return
     */
    @Bean
    public Sender sender(){
        OkHttpSender sender = OkHttpSender
                .newBuilder()
                .endpoint(zipkinEndPoint)
                .build();
        return sender;
    }

    /**
     * 配置reporter
     * @param sender
     * @return
     */
    @Bean
    public Reporter<Span> reporter(Sender sender){
       return AsyncReporter
               .builder(sender)
               .build();
    }

    /**
     * 配置dubbo-consumer tracing
     * @param reporter
     * @return
     */
    @Bean
    public Tracing tracing(Reporter reporter){
       return Tracing.newBuilder()
                .localServiceName(localServiceName)
                .spanReporter(reporter)
                .build();
    }

    /**
     * 配置http tracing
     * @param reporter
     * @return
     */
    @Bean
    public Tracing tracing2(Reporter reporter){
        return Tracing.newBuilder()
                .localServiceName(localServiceName + "_http")
                .spanReporter(reporter)
                .build();
    }

    /**
     * 配置servlet filter
     * @param tracing2
     * @return
     */
    @Bean
    public Filter filter(Tracing tracing2){
        return TracingFilter.create(tracing2);
    }

    /**
     * 注册filter
     * @param filter
     * @return
     */
    @Bean
    public FilterRegistrationBean filterRegistration(Filter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.addUrlPatterns("/*");
        registration.setName("zipkin-filter");
        registration.setOrder(1);
        return registration;
    }
}

启用自动配置 在resources目录新建META-INF目录: 新建文件:spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.github.wenj91.dubbo.zipkin.config.TracingConfig

ZipkinHelper

ZipkinHelper主要用于实现span的构建并上报


import brave.Span;
import brave.Tracer;
import brave.propagation.Propagation;
import com.alibaba.dubbo.remoting.exchange.ResponseCallback;
import com.alibaba.dubbo.rpc.*;
import com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter;
import com.alibaba.dubbo.rpc.support.RpcUtils;
import com.alibaba.fastjson.JSON;
import zipkin2.Endpoint;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * @author wenj91
 * @Description:
 * @date 2018/6/22 10:44
 */
public class ZipkinHelper {

    static final Propagation.Setter<Map<String, String>, String> SETTER =
            new Propagation.Setter<Map<String, String>, String>() {
                @Override
                public void put(Map<String, String> carrier, String key, String value) {
                    carrier.put(key, value);
                }
                @Override
                public String toString() {
                    return JSON.toJSONString(this);
                }
            };
    static final Propagation.Getter<Map<String, String>, String> GETTER =
            new Propagation.Getter<Map<String, String>, String>() {
                @Override
                public String get(Map<String, String> carrier, String key) {
                    return carrier.get(key);
                }

                @Override
                public String toString() {
                    return JSON.toJSONString(this);
                }
            };

    static void buildSpan(Span span, Span.Kind kind, InetSocketAddress remoteAddress, String service, String method){
        if (!span.isNoop()) {
            span.kind(kind).start();
            span.kind(kind);
            span.name(service + "/" + method);
            Endpoint.Builder remoteEndpoint = Endpoint.newBuilder().port(remoteAddress.getPort());
            if (!remoteEndpoint.parseIp(remoteAddress.getAddress())) {
                remoteEndpoint.parseIp(remoteAddress.getHostName());
            }
            span.remoteEndpoint(remoteEndpoint.build());
        }
    }

    static Result spanTracing(Span span, Tracer tracer, Invoker<?> invoker, Invocation invocation, RpcContext rpcContext){
        boolean isOneway = false;
        boolean deferFinish = false;
        try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) {
            Result result = invoker.invoke(invocation);
            if (result.hasException()) {
                onError(result.getException(), span);
            }
            isOneway = RpcUtils.isOneway(invoker.getUrl(), invocation);
            Future<Object> future = rpcContext.getFuture(); // the case on async client invocation
            if (future instanceof FutureAdapter) {
                deferFinish = true;
                ((FutureAdapter) future).getFuture().setCallback(new ZipkinHelper.FinishSpanCallback(span));
            }
            return result;
        } catch (Exception e) {
            onError(e, span);
            throw e;
        } finally {
            if (isOneway) {
                span.flush();
            } else if (!deferFinish) {
                span.finish();
            }
        }
    }

    static void onError(Throwable error, Span span) {
        span.error(error);
        if (error instanceof RpcException) {
            span.tag("dubbo.error_code", Integer.toString(((RpcException) error).getCode()));
        }
    }

    static final class FinishSpanCallback implements ResponseCallback {
        final Span span;

        FinishSpanCallback(Span span) {
            this.span = span;
        }

        @Override public void done(Object response) {
            span.finish();
        }

        @Override public void caught(Throwable exception) {
            onError(exception, span);
            span.finish();
        }
    }
}

DubboZipkinConsumerFilter

DubboZipkinConsumerFilter是dubbo消费端过滤器,用于拦截zipkin上下文消息


import brave.Span;
import brave.Tracer;
import brave.Tracing;
import brave.propagation.TraceContext;
import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
import com.alibaba.dubbo.rpc.*;
import com.alibaba.dubbo.rpc.support.RpcUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

import static com.github.wenj91.dubbo.zipkin.filter.ZipkinHelper.SETTER;

@Activate(group = Constants.CONSUMER)
public class DubboZipkinConsumerFilter implements Filter {
    private static final Logger log = LoggerFactory.getLogger(DubboZipkinConsumerFilter.class);

    private SpringExtensionFactory springExtensionFactory = new SpringExtensionFactory();
    private Tracer tracer;

    // tracing上下文消息注入
    private TraceContext.Injector<Map<String, String>> injector;

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        log.info("dubbo zipkin consumer filter......");

        Tracing tracing = springExtensionFactory.getExtension(Tracing.class, "tracing");
        tracer = tracing.tracer();
        if (tracer == null){
            return invoker.invoke(invocation);
        }

        if (null == injector){
            injector = tracing.propagation().injector(SETTER);
        }

        RpcContext rpcContext = RpcContext.getContext();
        Span span = tracer.nextSpan();
        injector.inject(span.context(), invocation.getAttachments());

        ZipkinHelper.buildSpan(span, Span.Kind.CONSUMER, rpcContext.getRemoteAddress(), invoker.getInterface().getSimpleName(),
                RpcUtils.getMethodName(invocation));

        return ZipkinHelper.spanTracing(span, tracer, invoker, invocation, rpcContext);
    }

}

DubboZipkinProviderFilter

DubboZipkinProviderFilter是dubbo服务端过滤器,用于拦截zipkin上下文消息


import brave.Span;
import brave.Tracer;
import brave.Tracing;
import brave.propagation.TraceContext;
import brave.propagation.TraceContextOrSamplingFlags;
import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
import com.alibaba.dubbo.rpc.*;
import com.alibaba.dubbo.rpc.support.RpcUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

import static com.github.wenj91.dubbo.zipkin.filter.ZipkinHelper.*;

@Activate(group = Constants.PROVIDER)
public class DubboZipkinProviderFilter implements Filter {

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

    private SpringExtensionFactory springExtensionFactory = new SpringExtensionFactory();
    private Tracer tracer;

    // tracing上下文消息提取
    private TraceContext.Extractor<Map<String, String>> extractor;

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        log.info("dubbo zipkin provider filter......");

        Tracing tracing = springExtensionFactory.getExtension(Tracing.class, "tracing");
        tracer = tracing.tracer();
        if (null == tracer){
            return invoker.invoke(invocation);
        }

        if (null == extractor){
            extractor = tracing.propagation().extractor(GETTER);
        }

        TraceContextOrSamplingFlags extracted = extractor.extract(invocation.getAttachments());
        Span span = extracted.context() != null
                ? tracer.joinSpan(extracted.context())
                : tracer.nextSpan(extracted);

        RpcContext rpcContext = RpcContext.getContext();
        ZipkinHelper.buildSpan(span, Span.Kind.SERVER, rpcContext.getRemoteAddress(), invoker.getInterface().getSimpleName(),
                RpcUtils.getMethodName(invocation));

        return ZipkinHelper.spanTracing(span, tracer, invoker, invocation, rpcContext);
    }
}

在META-INF添加dubbo文件夹, 新建文件:com.alibaba.dubbo.rpc.Filter 文件内容:

zipkinConsumerDubboFilter=com.github.wenj91.dubbo.zipkin.filter.DubboZipkinConsumerFilter
zipkinProviderDubboFilter=com.github.wenj91.dubbo.zipkin.filter.DubboZipkinProviderFilter

spring boot相关配置

dubbo provider添加配置:

zipkin:
  tracing:
    local-service-name: zipkin-provider

dubbo consumer添加配置:

zipkin:
  tracing:
    local-service-name: zipkin-consumer

Zipkin jar 下载地址项目demo



  相关文章:


留言区:

TOP