Zipkin

- Zipkin Spring Cloud

Zipkin 核心原理

1. 架构组成

+-----------+     +-----------+     +-----------+
| Collector | <-- | Reporter | <-- | Instrument|
+-----------+     +-----------+     +-----------+
      |                  |
      v                  v
+-----------+     +-----------+
| Storage   |     | UI        |
+-----------+     +-----------+

2. 核心概念

1. cs (Client Sent): 客户端发送请求的时间。
2. sr (Server Received): 服务器接收到请求的时间。
3. ss (Server Sent): 服务器发送响应的时间。
4. cr (Client Received): 客户端接收到响应的时间。
5. ca (Client Address): 客户端的地址信息。
6. sa (Server Address): 服务器的地址信息。
7. error (Error): 表示请求过程中是否发生了错误。
8. mvc.controller (MVC Controller): 与 Spring MVC 控制器相关的注解。
9. mvc.method (MVC Method): 与 Spring MVC 方法相关的注解。
10. http.method (HTTP Method): HTTP 请求的方法(如 GET, POST 等)。
11. http.path (HTTP Path): HTTP 请求的路径。
12. http.status_code (HTTP Status Code): HTTP 响应的状态码。
13. http.url (HTTP URL): HTTP 请求的完整 URL。
14. sql.query (SQL Query): 执行的 SQL 查询语句。
15. sql.statement (SQL Statement): 执行的 SQL 语句。
16. rabbitmq.message (RabbitMQ Message): 与 RabbitMQ 消息相关的注解。
17. redis.command (Redis Command): 执行的 Redis 命令。

3. 数据流

1. 应用通过 Reporter 发送追踪数据
2. Collector 接收并验证数据
3. 数据存储到 Storage
4. UI 从 Storage 查询并展示数据

源码分析

1. Span 数据结构

public final class Span implements Serializable {
    public static Builder newBuilder() {
        return new Builder();
    }
    
    public static final class Builder {
        private String traceId;
        private String parentId;
        private String id;
        private String name;
        private long timestamp;
        private long duration;
        private List<Annotation> annotations;
        private Map<String, String> tags;
        // ... other fields and methods ...
    }
}

2. Reporter 接口

public interface Reporter<S> {
    void report(S span);
    
    default void close() throws IOException {}
}

3. Collector 组件

public abstract class Collector {
    public abstract void accept(List<Span> spans);
    
    protected void storeSpans(List<Span> spans) {
        // 存储逻辑
    }
}

4. Storage 组件

public interface StorageComponent {
    SpanConsumer spanConsumer();
    
    SpanStore spanStore();
    
    AutocompleteTags autocompleteTags();
}

5. UI 组件

@RestController
public class ZipkinUi {
    @GetMapping("/")
    public String index() {
        return "index";
    }
    
    @GetMapping("/traces/{id}")
    public String traceDetail(@PathVariable String id) {
        // 查询并返回追踪详情
    }
}

重要知识点

1. Zipkin 的工作原理是什么?

2. Zipkin 支持哪些存储后端?

3. 如何集成 Zipkin 和 Spring Cloud Sleuth

spring:
  zipkin:
    base-url: http://localhost:9411
    sender:
      type: web # 使用HTTP发送
  sleuth:
    sampler:
      probability: 1.0 # 采样率

4. ZipKin 的数据模型怎么的?

5. 如何自定义 Zipkin 的 Reporter?

// ... existing code ...
@Bean
Reporter<Span> myReporter() {
    return new AsyncReporter<>(URLConnectionSender.create("http://localhost:9411/api/v2/spans"));
}
// ... existing code ...

6. Zipkin 的新优化策略有哪些?

7. 如何实现 Zipkin 的高可用

8. Zipkin 的 UI 有哪一些?

9. 如何处理 Zipkin 的数据存储问题?

10. Zipkin 如何支持多语言?