Dubbo 埋点接入

更新时间: 2019-06-21

在本文档将演示如何使用 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 接口

新建 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-dependencies</artifactId>
  4. <version>${sofa.boot.version}</version>
  5. </parent>

这里的 ${sofa.boot.version} 指定具体的 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-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-sofa-boot-starter</artifactId>
  4. </dependency>
  • 在工程的 application.properties 文件下添加相关参数
  1. spring.application.name=dubbo-consumer
  2. dubbo.registry.address=zookeeper://localhost:2181
  3. dubbo.consumer.filter=dubboSofaTracerFilter
  4. 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-04-03 11:36:01.909","traceId":"0a0fe8451554262561656100126684","spanId":"0","span.kind":"client","local.app":"dubbo-consumer","protocol":"dubbo","service":"com.alipay.sofa.tracer.examples.dubbo.facade.HelloService","method":"SayHello","invoke.type":"sync","remote.host":"10.15.232.69","remote.port":"20880","local.host":"10.15.232.69","client.serialize.time":35,"client.deserialize.time":0,"req.size.bytes":323,"resp.size.bytes":323,"result.code":"00","current.thread.name":"main","time.cost.milliseconds":252,"baggage":""}
  • dubbo-server-digest.logjson{"time":"2019-04-03 11:36:01.880","traceId":"0a0fe8451554262561656100126684","spanId":"0","span.kind":"server","local.app":"dubbo-provider","service":"com.alipay.sofa.tracer.examples.dubbo.facade.HelloService","method":"SayHello","local.host":"10.15.232.69","local.port":"54178","protocol":"dubbo","server.serialize.time":0,"server.deserialize.time":27,"result.code":"00","current.thread.name":"DubboServerHandler-10.15.232.69:20880-thread-2","time.cost.milliseconds":3,"baggage":""}
    • dubbo-client-stat.log
  1. {"time":"2019-04-03 11:37:01.650","stat.key":{"method":"SayHello","local.app":"dubbo-consumer","service":"com.alipay.sofa.tracer.examples.dubbo.facade.HelloService"},"count":1,"total.cost.milliseconds":252,"success":"Y"}
  • dubbo-server-stat.logjson{"time":"2019-04-03 11:37:01.872","stat.key":{"method":"SayHello","remote.app":"dubbo-provider","service":"com.alipay.sofa.tracer.examples.dubbo.facade.HelloService"},"count":1,"total.cost.milliseconds":3,"success":"Y"}