使用Pinpoint做分布式跟踪

在使用Dubbo进行服务化或者整合应用后,假设某个服务后台日志显示有异常,这个服务又被多个应用调用的情况下,我们通常很难判断是哪个应用调用的,问题的起因是什么,因此我们需要一套分布式跟踪系统来快速定位问题,Pinpoint可以帮助我们快速定位问题(当然,解决方案也不止这一种)。

什么是Pinpoint(摘自Pinpoint学习笔记)

Pinpoint是一个开源的 APM (Application Performance Management/应用性能管理)工具,用于基于java的大规模分布式系统。仿照Google Dapper,Pinpoint通过跟踪分布式应用之间的调用来提供解决方案,以帮助分析系统的总体结构和内部模块之间如何相互联系。

注:对于各个模块之间的通讯英文原文中用的是transaction一词,但是我觉得如果翻译为"事务"容易引起误解,所以替换为"交互"或者"调用"这种比较直白的字眼。

在使用上力图简单高效:

  • 安装agent,不需要修改哪怕一行代码
  • 最小化性能损失

    服务器地图(ServerMap)

通过可视化分布式系统的模块和他们之间的相互联系来理解系统拓扑。点击某个节点会展示这个模块的详情,比如它当前的状态和请求数量。

实时活动线程图表(Realtime Active Thread Chart)

实时监控应用内部的活动线程。

请求/应答分布图表(Request/Response Scatter Chart)

长期可视化请求数量和应答模式来定位潜在问题。通过在图表上拉拽可以选择请求查看更多的详细信息。

调用栈(CallStack)

在分布式环境中为每个调用生成代码级别的可视图,在单个视图中定位瓶颈和失败点。

巡查(Inspector)

查看应用上的其他详细信息,比如CPU使用率,内存/垃圾回收,TPS,和JVM参数。

支持模块

  • JDK 6+
  • Tomcat 6/7/8, Jetty 8/9, JBoss EAP 6, Resin 4, Websphere 6/7/8, Vertx 3.3/3.4/3.5
  • Spring, Spring Boot (Embedded Tomcat, Jetty)
  • Apache HTTP Client 3.x/4.x, JDK HttpConnector, GoogleHttpClient, OkHttpClient, NingAsyncHttpClient
  • Thrift Client, Thrift Service, DUBBO PROVIDER, DUBBO CONSUMER
  • ActiveMQ, RabbitMQ
  • MySQL, Oracle, MSSQL, CUBRID,POSTGRESQL, MARIA
  • Arcus, Memcached, Redis, CASSANDRA
  • iBATIS, MyBatis
  • DBCP, DBCP2, HIKARICP
  • gson, Jackson, Json Lib
  • log4j, Logback
  • 自定义模块

    Pinpoint与Dubbo的结合

启动Pinpoint

参考Pinpoint的Quick start搭建环境(不需要启动TestApp)

准备Dubbo示例程序

创建API包

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.example</groupId>
  7. <artifactId>demo-api</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </project>

新建API接口:

  1. package com.example.demoapi;
  2. public interface HelloService {
  3. String sayHello(String name);
  4. }

实现 Dubbo 服务提供方

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>demo-provider</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>demo-provider</name>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.0.3.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <repositories>
  22. <repository>
  23. <id>sonatype-nexus-snapshots</id>
  24. <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  25. <releases>
  26. <enabled>false</enabled>
  27. </releases>
  28. <snapshots>
  29. <enabled>true</enabled>
  30. </snapshots>
  31. </repository>
  32. </repositories>
  33. <dependencies>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>com.alibaba.boot</groupId>
  40. <artifactId>dubbo-spring-boot-starter</artifactId>
  41. <version>0.2.0</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>com.example</groupId>
  45. <artifactId>demo-api</artifactId>
  46. <version>0.0.1-SNAPSHOT</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-starter-test</artifactId>
  51. <scope>test</scope>
  52. </dependency>
  53. </dependencies>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>
  • 实现 HelloService 接口:
    1. package com.example.demoprovider.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.demoapi.HelloService;

@Service(version = "${demo.service.version}",
application = "${dubbo.application.id}",
protocol = "${dubbo.protocol.id}",
registry = "${dubbo.registry.id}")
public class HelloServiceImpl implements HelloService {
static int i = 0;
@Override
public String sayHello(String name) {
i++;
if (i % 3 == 0) {
throw new RuntimeException("ex");
}
return "Hello " + name + "!";
}
}

  • 编写 Spring Boot 引导程序:
    1. package com.example.demoprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoProviderApplication {

  1. <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> {
  2. SpringApplication.run(DemoProviderApplication.class, args);
  3. }

}

  • 配置 application.properties:
    1. # Spring boot application
      spring.application.name = dubbo-provider-demo
      server.port = 9090
      management.port = 9091

Service version

demo.service.version = 1.0.0

Base packages to scan Dubbo Components (e.g @Service , @Reference)

dubbo.scan.basePackages = com.example.demoprovider

Dubbo Config properties

ApplicationConfig Bean

dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo

ProtocolConfig Bean

dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

RegistryConfig Bean

dubbo.registry.id = my-registry
dubbo.registry.address = N/A

实现 Dubbo 服务消费方

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>demo-consumer</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>demo-consumer</name>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.0.3.RELEASE</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <repositories>
  22. <repository>
  23. <id>sonatype-nexus-snapshots</id>
  24. <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  25. <releases>
  26. <enabled>false</enabled>
  27. </releases>
  28. <snapshots>
  29. <enabled>true</enabled>
  30. </snapshots>
  31. </repository>
  32. </repositories>
  33. <dependencies>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-web</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>com.alibaba.boot</groupId>
  40. <artifactId>dubbo-spring-boot-starter</artifactId>
  41. <version>0.2.0</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>com.example</groupId>
  45. <artifactId>demo-api</artifactId>
  46. <version>0.0.1-SNAPSHOT</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-starter-test</artifactId>
  51. <scope>test</scope>
  52. </dependency>
  53. </dependencies>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. <configuration>
  60. <classifier>exec</classifier>
  61. </configuration>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>
  • 通过 @Reference 注入 HelloService
    1. package com.example.democonsumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.demoapi.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoConsumerController {
@Reference(version = "${demo.service.version}",
application = "${dubbo.application.id}",
url = "dubbo://<注意,这里填写具体IP>:12345")
private HelloService helloService;

  1. @RequestMapping(&#34;/sayHello&#34;)
  2. <span class="hljs-keyword">public</span> String <span class="hljs-title">sayHello</span><span class="hljs-params">(@RequestParam String name)</span> {
  3. return helloService.sayHello(name);
  4. }

}

直连提供者调用需要填写具体IP地址,如果写localhost也可以,但是会被Pinpoint额外识别为一个未知服务
  • 编写 Spring Boot 引导程序(Web 应用):
    1. package com.example.democonsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoConsumerApplication {

  1. <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> {
  2. SpringApplication.run(DemoConsumerApplication.class, args);
  3. }

}

  • 配置 application.properties:
    1. # Spring boot application
      spring.application.name=dubbo-consumer-demo
      server.port=8080
      management.port=8081

Service Version

demo.service.version=1.0.0

Dubbo Config properties

ApplicationConfig Bean

dubbo.application.id=dubbo-consumer-demo
dubbo.application.name=dubbo-consumer-demo

ProtocolConfig Bean

dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=12345

使用Pinpoint-agent启动服务提供方和服务消费方

启动服务提供方

  • 编译打包

    1. mvn clean package
  • 附加参数启动服务提供方

    1. java -jar -javaagent:$AGENT_PATH/pinpoint-bootstrap-$VERSION.jar -Dpinpoint.agentId=demo-provider -Dpinpoint.applicationName=DP target/demo-provider-0.0.1-SNAPSHOT.jar
  • 附加参数启动服务消费方

    1. java -jar -javaagent:$AGENT_PATH/pinpoint-bootstrap-$VERSION.jar -Dpinpoint.agentId=demo-consumer -Dpinpoint.applicationName=DC target/demo-comsumer-0.0.1-SNAPSHOT-exec.jar
  • 访问消费方地址模拟用户请求
    http://localhost:8080/sayHello?name=ABC

使用Pinpoint快速定位问题

首页

/admin-guide/images/pinpoint-home.png

这里的用户请求是请求DubboProvider数量的双倍,原因是记录了favicon.ico图标请求导致的

调用树

/admin-guide/images/pinpoint-calltree.png

深入跟踪

/admin-guide/images/pinpoint-mixedview.png

其他

示例简单的模拟了Dubbo的提供和调用,并没有进行数据库等其他中间件的应用,详细使用请参照Pinpoint文档。

原文: http://dubbo.apache.org/#!/docs/admin/ops/pinpoint.md?lang=zh-cn