The underlying logic of the "Internet Architecture" call chain system

There are many call chain systems: Dapper, Hawkeye, hydra, cat, zipkin, skywalking. In fact, no matter it is any call chain system, the underlying implementation is consistent. Let's understand its underlying implementation together.

(1) The essence of the call chain system

  • What process does a web page have to go through to reach the user?

This is Ali's favorite question in early interviews. Asking this question is to understand your breadth of technology.

  • network transport layer

# 该命令可直接跟踪网络通信所经过的节点
tracert www.baidu.com
复制代码

  • load balancing layer

Early monitoring systems only monitored network services.

  • system service layer

Today's monitoring systems go directly to the inside of the system. Application systems, databases, third-party resource services, and interfaces for requesting third-party APIs.

  • Call chain basic elements

Basically all calling systems are these elements, possibly by different names.

1. Events

Specific actions during request processing

2. Node

The system node that the request passes through, that is, the spatial attribute of the event.

3. Time

The start and end time of the event

4. Relationship

The event is related to the previous event.

0.1 and 0.1.1, 0.1.2 is the parent-child relationship 02 and 0.2.3, 0.2.1, 0.2.2 is the parent-child relationship. Eating is an event, holding chopsticks is a sub-event of eating, and opening your mouth is a sub-event of eating. Mouth movement is a sub-event of eating. are nested relationships.

  • The call chain system is essentially used to answer these questions

Just like writing an essay, time, place, task, event. (Events are one by one, and it is necessary to ensure that the relationship between them is not intricate, and that these call events are not chaotic before they are completed. The theory is very simple, but if you really need to understand it, you need to use it in a production environment. Solve many, many problems.)

1. What time? 2. On what node? 3. What happened? 4. Who triggered this event?

  • event capture

In fact, the output information

1. Hard-coded buried point capture 2. AOP buried point capture 3. Public component buried point capture 4. Bytecode instrumentation capture

  • event chaining

The purpose of event concatenation

1. All events are related to the same call 2. There is a hierarchical relationship between events

In order to achieve these two goals, almost all call chain systems will have the following two properties:

trackID: Unique in the entire system, events with the same value represent the same call. eventID (spanID): unique in one call and exhibit the hierarchical relationship of events

1. How to generate TrackID 2. How to pass parameters

  • Concatenated process:

1.由跟踪的起点⽣成⼀个TrackId, ⼀直传递⾄所有节点,并保存在事件属性值当中。 2.由跟踪的起点⽣成初始EventId(SpanID),每捕捉⼀个事件ID加1,每传递⼀次,层级加1。

  • trackId与eventId 的传递

  • eventId ⾃增⽣成⽅式

埋在具体某个实现⽅法类,当多线程调⽤该⽅法时如何保证⾃增正确性?

解决办法是每个跟踪请求创建⼀个互相独⽴的会话,EventId的⾃增都基于该会话实现。通常会话对象的存储基于ThreadLocal实现。

  • 事件的开始与结束

我们知道⼀个事件是⼀个时间段内系统执⾏的若⼲动作,所以对于事件捕捉必须包含开启监听和结束监听两个动作?如果⼀个事件在⼀个⽅法内完成的,这个问题是⽐较好解决的,我们只要在⽅法的开始创建⼀个Event对象,在⽅法结束时调⽤该对像的close ⽅法即可。

public void addUser(){ 
// 方法的开始处,开启一个监听 
Event event=new Event(); 
//业务代码执行 
..... 
..... 
// 方法的结束处,关闭一个监听 
event.close(); 
} 
复制代码

但如果⼀个事件的开始和结束触发分布在多个对象或⽅法当中,情况就会变得异常复杂。⽐如⼀个JDBC执⾏事件,应该是在构建 Statement 时开始,在Statement 关闭时结束。怎样把这两个触发动作对应到同⼀个事件当中去呢(即传递Event对象)?在这⾥的解决办法是对返回结果进⾏动态代理,把Event放置到代理对象的属性当中,以达到付递的⽬标。当这个⽅法只是适应JDBC这⼀个场景,其它场景需要重新设计Event 传递路径,⽬前还没有通⽤的解决办法。

// JDBC事件开始 
Connection.prepareStatement(String sql); 
//JDBC 事件结束 
PreparedStatement.close(); 
复制代码
  • 上传
  1. 基于Http请求直接上传
  2. 打印⽇志,然后在基于Flume或Logstash采集上传。

第⼀种相对简单,直接把数据发送服务进⾏持久化,但如果系统流量较⼤的情况下,会影响系统本身的性能,造成压⼒。第⼆种相对复杂,但可以应对⼤流量,通常情况下会采⽤第⼆种解决办法

(二)项⽬部署

  • 调⽤链Agent 如何部署
  1. 下载 agent.zip ⾄应⽤系统
  2. 解压缩 agent.zip
  3. 添加jvm 参数 -javaagent: <cbt-agent-bootstrap-1.0-SNAPSHOT.jar 路径>
  4. 重启应⽤

1.下载Agent.zip 2.在你启动项⽬的JVM参数⾥添加 -javaagent:cbt-agent-bootstrap-1.0-SNAPSHOT.jar 3.重启你的应⽤,观察你的应⽤⽇志,如果发现以下⽇志代表启动成功了

[2018-06-12:10:32:42]加载藏宝图配置文件来自G:\git\cbt-
agent\out\conf\cbt.properties
[2018-06-12:10:32:43]藏宝图服务登陆成功! 
[2018-06-12:10:32:43]藏宝图服务启动成功! 
复制代码

4.登陆 调⽤链管理WEB⻚⾯ client.cbtu.pro:9978/trace/reque…

PS:调用链系统并非自己原创,也是通过网络学习获得的,但是我会在上边进行改良,以达到自己需要,并会告诉大家如何搭建,最终可以在微服务项目上受用。

Guess you like

Origin juejin.im/post/7103428915103793183