微服务框架(十六)Spring Boot及Dubbo zipkin 链路追踪组件埋点

简介: 此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。 本文第一部分为调用链、OpenTracing、Zipkin和Jeager的简述;第二部分为Spring Boot及Dubbo zipkin 链路追踪组件埋点

  此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。
  本文第一部分为调用链、OpenTracing、Zipkin和Jeager的简述;第二部分为Spring Boot及Dubbo zipkin 链路追踪组件埋点

本系列文章中所使用的框架版本为Spring Boot 2.0.3-RELEASE,Spring 5.0.7-RELEASE,Dubbo 2.6.2。

调用链

在广义上,一个调用链代表一个事务或者流程在(分布式)系统中的执行过程。在 OpenTracing 标准中,调用链是多个 Span 组成的一个有向无环图Directed Acyclic Graph,简称 DAG),每一个 Span代表调用链中被命名并计时的连续性执行片段。

下图是一个分布式调用的例子:客户端发起请求,请求首先到达负载均衡器,接着经过认证服务、计费服务,然后请求资源,最后返回结果。

数据被采集存储后,分布式追踪系统一般会选择使用包含时间轴的时序图来呈现这个调用链。

OpenTracing

为了解决不同的分布式追踪系统 API 不兼容的问题,诞生了 OpenTracing 规范。
OpenTracing 是一个轻量级的标准化层,它位于应用程序/类库追踪或日志分析程序之间。

+-------------+  +---------+  +----------+  +------------+
| Application |  | Library |  |   OSS    |  |  RPC/IPC   |
|    Code     |  |  Code   |  | Services |  | Frameworks |
+-------------+  +---------+  +----------+  +------------+
       |              |             |             |
       |              |             |             |
       v              v             v             v
  +------------------------------------------------------+
  |                     OpenTracing                      |
  +------------------------------------------------------+
     |                |                |               |
     |                |                |               |
     v                v                v               v
+-----------+  +-------------+  +-------------+  +-----------+
|  Tracing  |  |   Logging   |  |   Metrics   |  |  Tracing  |
| System A  |  | Framework B |  | Framework C |  | System D  |
+-----------+  +-------------+  +-------------+  +-----------+

OpenTracing 的优势

  • OpenTracing 已进入 CNCF,正在为全球的分布式追踪,提供统一的概念和数据标准。
  • OpenTracing 通过提供平台无关、厂商无关的 API,使得开发人员能够方便的添加(或更换)追踪系统的实现。

Zipkin

Zipkin是一种分布式跟踪系统。它有助于收集解决微服务架构中的延迟问题所需的时序数据。它管理这些数据的收集和查找。Zipkin的设计基于 Google Dapper论文。

Jeager

Jaeger受DapperOpenZipkin的启发,是Uber Technologies公开发布的分布式跟踪系统。它用于监视和排除基于微服务的分布式系统,包括:

  • 分布式上下文传播
  • 分布式事务监控
  • 根本原因分析
  • 服务依赖分析
  • 性能/延迟优化

Uber发表了一篇博客文章Evolving Distributed Tracing at Uber,在那里他们解释了Jaeger所做的架构性选择的历史和原因

依赖

brave依赖

<properties>
     <brave.version>5.4.2</brave.version>
     <zipkin-reporter.version>2.7.9</zipkin-reporter.version>
 </properties>

 <dependencyManagement>
     <dependencies>
         <!-- 引入 zipkin brave 的 BOM 文件 -->
         <dependency>
             <groupId>io.zipkin.brave</groupId>
             <artifactId>brave-bom</artifactId>
             <version>${brave.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>

         <!-- 引入 zipkin repoter 的 BOM 文件 -->
         <dependency>
             <groupId>io.zipkin.reporter2</groupId>
             <artifactId>zipkin-reporter-bom</artifactId>
             <version>${zipkin-reporter.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>

 <dependencies>
     <!-- 1. brave 的 spring bean 支持 -->
     <dependency>
         <groupId>io.zipkin.brave</groupId>
         <artifactId>brave-spring-beans</artifactId>
         <version>${brave.version}</version>
     </dependency>

     <!-- 2. 在 SLF4J 的 MDC (Mapped Diagnostic Context) 中支持 traceId 和 spanId -->
     <dependency>
         <groupId>io.zipkin.brave</groupId>
         <artifactId>brave-context-slf4j</artifactId>
         <version>${brave.version}</version>
     </dependency>

     <!-- 3. 使用 okhttp3 作为 reporter -->
     <dependency>
         <groupId>io.zipkin.reporter2</groupId>
         <artifactId>zipkin-sender-okhttp3</artifactId>
         <version>${zipkin-reporter.version}</version>
     </dependency>
 </dependencies>

Dubbo & brave

<!-- 1. brave 对 dubbo 的集成 -->
<dependency>
    <groupId>io.zipkin.brave</groupId>
    <artifactId>brave-instrumentation-dubbo-rpc</artifactId>
    <version>${brave.version}</version>
</dependency>

Spring web & brave

<!-- brave 对 spring web 的集成 -->
<dependency>
    <groupId>io.zipkin.brave</groupId>
    <artifactId>brave-instrumentation-spring-web</artifactId>
    <version>${brave.version}</version>
</dependency>
<dependency>
    <groupId>io.zipkin.brave</groupId>
    <artifactId>brave-instrumentation-spring-webmvc</artifactId>
    <version>${brave.version}</version>
</dependency>

使用方法

zipkin

zipkin.url = http://<domain>/api/v2/spans?userName=<username>&userKey=<userkey>

Spring bean 注入

  • @ComponentScan
  • 配置 autoconfigurespring.factories)。
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=brave.webmvc.TracingConfiguration
    

服务端

dubbo.provider.filter=tracing

客户端

dubbo.consumer.filter=tracing

tracing为链路追踪filter组件启用

插件埋点

RPC插件埋点

通过brave.dubbo.rpcTracingFilter实现Dubbo Filter接口,注入对应的spring bean

@Configuration
public class TracingConfiguration {
   
   

    @Value("${zipkin.url}")
    private String url;

    /**
     * Configuration for how to send spans to Zipkin
     */
    @Bean
    Sender sender() {
   
   
        return OkHttpSender.create(url);
    }

    /**
     * Configuration for how to buffer spans into messages for Zipkin
     */
    @Bean
    AsyncReporter<Span> spanReporter() {
   
   
        return AsyncReporter.create(sender());
    }

    /**
     * Controls aspects of tracing such as the name that shows up in the UI
     */
    @Bean
    Tracing tracing(@Value("${spring.application.name}") String serviceName) {
   
   
        return Tracing.newBuilder()
                .localServiceName(serviceName)
                .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name"))
                .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
                        .addScopeDecorator(MDCScopeDecorator.create())
                        .build()
                )
                .spanReporter(spanReporter()).build();
    }
}

HTTP插件埋点

通过brave.servletTracingFilter实现Servlet Filter接口,注入对应的spring bean

@Configuration
@Import(SpanCustomizingAsyncHandlerInterceptor.class)
public class HttpTracingConfiguration {
   
   


    @Value("${zipkin.url}")
    private String url;

    /**
     * Configuration for how to send spans to Zipkin
     */
    @Bean
    Sender sender() {
   
   
        return OkHttpSender.create(url);
    }

    /**
     * Configuration for how to buffer spans into messages for Zipkin
     */
    @Bean
    AsyncReporter<Span> spanReporter() {
   
   
        return AsyncReporter.create(sender());
    }

    /**
     * Controls aspects of tracing such as the name that shows up in the UI
     */
    @Bean
    Tracing tracing(@Value("${spring.application.name}") String serviceName) {
   
   
        return Tracing.newBuilder()
                .localServiceName(serviceName)
                .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, "user-name"))
                .currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder()
                        .addScopeDecorator(MDCScopeDecorator.create())
                        .build()
                )
                .spanReporter(spanReporter()).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 client spans for http requests
     * <p>
     * We are using a BPP as the Frontend supplies a RestTemplate bean prior to this configuration
     */
    @Bean
    BeanPostProcessor connectionFactoryDecorator(final BeanFactory beanFactory) {
   
   
        return new BeanPostProcessor() {
   
   
            @Override
            public Object postProcessBeforeInitialization(Object bean, String beanName) {
   
   
                return bean;
            }

            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) {
   
   
                if (!(bean instanceof RestTemplate)) {
   
   
                    return bean;
                }
                RestTemplate restTemplate = (RestTemplate) bean;
                List<ClientHttpRequestInterceptor> interceptors =
                        new ArrayList<>(restTemplate.getInterceptors());
                interceptors.add(0, getTracingInterceptor());
                restTemplate.setInterceptors(interceptors);
                return bean;
            }

            // Lazy lookup so that the BPP doesn't end up needing to proxy anything.
            ClientHttpRequestInterceptor getTracingInterceptor() {
   
   
                return TracingClientHttpRequestInterceptor.create(beanFactory.getBean(HttpTracing.class));
            }
        };
    }

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

参考资料:

  1. 链路追踪 Tracing Analysis
  2. 在 Dubbo 中使用 Zipkin
  3. 分布式系统调用跟踪实践
  4. Zipkin vs Jaeger: Getting Started With Tracing
  5. Zipkin官方文档
  6. Jeager官方文档
  7. 开放分布式追踪(OpenTracing)入门与 Jaeger 实现
  8. 全链路监控(一):方案概述与比较
相关实践学习
分布式链路追踪Skywalking
Skywalking是一个基于分布式跟踪的应用程序性能监控系统,用于从服务和云原生等基础设施中收集、分析、聚合以及可视化数据,提供了一种简便的方式来清晰地观测分布式系统,具有分布式追踪、性能指标分析、应用和服务依赖分析等功能。 分布式追踪系统发展很快,种类繁多,给我们带来很大的方便。但在数据采集过程中,有时需要侵入用户代码,并且不同系统的 API 并不兼容,这就导致了如果希望切换追踪系统,往往会带来较大改动。OpenTracing为了解决不同的分布式追踪系统 API 不兼容的问题,诞生了 OpenTracing 规范。OpenTracing 是一个轻量级的标准化层,它位于应用程序/类库和追踪或日志分析程序之间。Skywalking基于OpenTracing规范开发,具有性能好,支持多语言探针,无侵入性等优势,可以帮助我们准确快速的定位到线上故障和性能瓶颈。 在本套课程中,我们将全面的讲解Skywalking相关的知识。从APM系统、分布式调用链等基础概念的学习加深对Skywalking的理解,从0开始搭建一套完整的Skywalking环境,学会对各类应用进行监控,学习Skywalking常用插件。Skywalking原理章节中,将会对Skywalking使用的agent探针技术进行深度剖析,除此之外还会对OpenTracing规范作整体上的介绍。通过对本套课程的学习,不止能学会如何使用Skywalking,还将对其底层原理和分布式架构有更深的理解。本课程由黑马程序员提供。
相关文章
|
7天前
|
安全 Java API
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)
27 0
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
|
7天前
|
Java API 微服务
【Spring Boot系列】通过OpenAPI规范构建微服务服务接口
【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
155 0
|
1天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
10天前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【4月更文挑战第17天】Spring Cloud是Java微服务治理的首选框架,整合了Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心)。通过Eureka实现服务注册与发现,Ribbon提供负载均衡,Hystrix实现熔断保护,Zuul作为API网关,Config Server集中管理配置。理解并运用Spring Cloud进行微服务治理是现代Java开发者的关键技能。
|
25天前
|
负载均衡 网络协议 Java
构建高效可扩展的微服务架构:利用Spring Cloud实现服务发现与负载均衡
本文将探讨如何利用Spring Cloud技术实现微服务架构中的服务发现与负载均衡,通过注册中心来管理服务的注册与发现,并通过负载均衡策略实现请求的分发,从而构建高效可扩展的微服务系统。
|
1月前
|
SQL 监控 Java
nacos常见问题之dubbo+nacos+springboot3的native打包成功后运行出现异常如何解决
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
37 2
|
1月前
|
SpringCloudAlibaba 负载均衡 Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
66 1
|
1月前
|
Java Nacos Sentinel
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
205 0
|
Java 知识图谱 容器
SpringBoot扫描不到组件?给你提供几种方案
SpringBoot扫描不到组件?给你提供几种方案
700 0
SpringBoot扫描不到组件?给你提供几种方案