全部产品
云市场

Dubbo 埋点接入

更新时间:2020-02-18 09:27:17

在本文档将基于 工程地址 演示如何使用 SOFATracer 对 Dubbo 进行埋点。

基础环境

本案例使用的各框架组件的版本如下:

  • SOFABoot 3.1.1/SpringBoot 2.1.0.RELEASE
  • SOFATracer 2.4.0/3.0.4
  • JDK 8

本案例包括三个子模块:

  • tracer-sample-with-dubbo-consumer:服务调用方
  • tracer-sample-with-dubbo-provider:服务提供方
  • tracer-sample-with-dubbo-facade:接口

原理

SOFATracer 对象 Dubbo 的埋点实现依赖于 Dubbo 的 SPI 机制来实现,Tracer 中基于 调用拦截扩展自定义了 DubboSofaTracerFilter 用于实现对 Dubbo 的调用埋点。由于 DubboSofaTracerFilter 并没有成为 Dubbo 的官方扩展,因此在使用 SOFATracer 时需要安装 调用拦截扩展 中所提供的方式进行引用,即:

  1. <!-- 消费方调用过程拦截 -->
  2. <dubbo:reference filter="dubboSofaTracerFilter" />
  3. <!-- 消费方调用过程缺省拦截器,将拦截所有reference -->
  4. <dubbo:consumer filter="dubboSofaTracerFilter"/>
  5. <!-- 提供方调用过程拦截 -->
  6. <dubbo:service filter="dubboSofaTracerFilter" />
  7. <!-- 提供方调用过程缺省拦截器,将拦截所有service -->
  8. <dubbo:provider filter="dubboSofaTracerFilter"/>

新建 SOFABoot 工程作为父工程

在创建好一个 Spring Boot 的工程之后,接下来就需要引入 SOFABoot 的依赖,首先,需要将上文中生成的 Spring Boot 工程的 zip 包解压后,修改 Maven 项目的配置文件 pom.xml,将

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>${spring.boot.version}</version>
  5. <relativePath/>
  6. </parent>

替换为:

  1. <parent>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>sofaboot-enterprise-dependencies</artifactId>
  4. <version>${sofa.boot.version}</version>
  5. </parent>

这里的 ${sofa.boot.version} 指定具体的 SOFABoot 版本,参考 SOFABoot 版本说明

新建 tracer-sample-with-dubbo-facade

提供一个接口:

  1. public interface HelloService {
  2. String SayHello(String name);
  3. }

新建 tracer-sample-with-dubbo-provider

  • 在工程模块的 pom 文件中添加 SOFATracer 依赖:

    1. <dependency>
    2. <groupId>com.alipay.sofa</groupId>
    3. <artifactId>tracer-enterprise-sofa-boot-starter</artifactId>
    4. </dependency>

    SOFATracer 版本受 SOFABoot 版本管控,如果使用的 SOFABoot 版本不匹配,则需要手动指定 tracer 版本,且版本需高于 2.4.0.

  • 在工程的 application.properties 文件下添加相关参数:

    1. # Spring boot application
    2. spring.application.name=dubbo-provider
    3. # Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
    4. dubbo.scan.base-packages=com.alipay.sofa.tracer.examples.dubbo.impl
    5. ## Filter 必须配置
    6. dubbo.provider.filter=dubboSofaTracerFilter
    7. # Dubbo Protocol
    8. dubbo.protocol.name=dubbo
    9. ## Dubbo Registry
    10. dubbo.registry.address=zookeeper://localhost:2181
    11. logging.path=./logs
  • 使用注解方式发布 Dubbo 服务:

    1. @Service
    2. public class HelloServiceImpl implements HelloService {
    3. @Override
    4. public String SayHello(String name) {
    5. return "Hello , "+name;
    6. }
    7. }

新建 tracer-sample-with-dubbo-consumer

  • 在工程模块的 pom 文件中添加 SOFATracer 依赖:

    1. <dependency>
    2. <groupId>com.alipay.sofa</groupId>
    3. <artifactId>tracer-enterprise-sofa-boot-starter</artifactId>
    4. </dependency>
  • 在工程的 application.properties 文件下添加相关参数:

    1. spring.application.name=dubbo-consumer
    2. dubbo.registry.address=zookeeper://localhost:2181
    3. # Filter 必须配置
    4. dubbo.consumer.filter=dubboSofaTracerFilter
    5. logging.path=./logs
  • 服务引用:

    1. @Reference(async = false)
    2. public HelloService helloService;
    3. @Bean
    4. public ApplicationRunner runner() {
    5. return args -> {
    6. logger.info(helloService.SayHello("sofa"));
    7. };
    8. }

测试

先后启动 tracer-sample-with-dubbo-provider 和 tracer-sample-with-dubbo-consumer 两个工程,然后查看以下日志:

  • dubbo-client-digest.log

    1. {"time":"2019-09-02 23:36:08.250","local.app":"dubbo-consumer","traceId":"1e27a79c156743856804410019644","spanId":"0","span.kind":"client","result.code":"00","current.thread.name":"http-nio-8080-exec-2","time.cost.milliseconds":"205ms","protocol":"dubbo","service":"com.glmapper.bridge.boot.service.HelloService","method":"SayHello","invoke.type":"sync","remote.host":"192.168.2.103","remote.port":"20880","local.host":"192.168.2.103","client.serialize.time":35,"client.deserialize.time":5,"req.size.bytes":336,"resp.size.bytes":48,"error":"","sys.baggage":"","biz.baggage":""}
  • dubbo-server-digest.log

    1. {"time":"2019-09-02 23:36:08.219","local.app":"dubbo-provider","traceId":"1e27a79c156743856804410019644","spanId":"0","span.kind":"server","result.code":"00","current.thread.name":"DubboServerHandler-192.168.2.103:20880-thread-2","time.cost.milliseconds":"9ms","protocol":"dubbo","service":"com.glmapper.bridge.boot.service.HelloService","method":"SayHello","local.host":"192.168.2.103","local.port":"62443","server.serialize.time":0,"server.deserialize.time":27,"req.size.bytes":336,"resp.size.bytes":0,"error":"","sys.baggage":"","biz.baggage":""}
  • dubbo-client-stat.log

    1. {"time":"2019-09-02 23:36:13.040","stat.key":{"method":"SayHello","local.app":"dubbo-consumer","service":"com.glmapper.bridge.boot.service.HelloService"},"count":1,"total.cost.milliseconds":205,"success":"true","load.test":"F"}
  • dubbo-server-stat.log

    1. {"time":"2019-09-02 23:36:13.208","stat.key":{"method":"SayHello","local.app":"dubbo-provider","service":"com.glmapper.bridge.boot.service.HelloService"},"count":1,"total.cost.milliseconds":9,"success":"true","load.test":"F"}

对 Dubbo 2.6.x 版本的兼容

SOFATracer 中 2.4.1/3.0.6 版本开始也提供了对于 Dubbo 2.6.x 版本系列的支持,详见 tracer-dubbo-2.6.x

注意

  • 默认情况下,tracerlog 日志将会打在用户根目录下。
  • SOFATracer 日志目录依赖 log-sofa-boot-starter 来根据 application.properties 配置的 logging.path 来决定,因此如果是 springboot 工程,请引入 log-sofa-boot-starter