zipkin是一种分布式跟踪系统。它有助于收集解决微服务架构中延迟问题所需的时序数据。它管理这些数据的收集和查找。zipkin的设计基于 google dapper论文。
应用程序用于向zipkin报告时间数据。zipkin用户界面还提供了一个依赖关系图,显示每个应用程序有多少跟踪请求。如果您正在解决延迟问题或错误问题,则可以根据应用程序,跟踪长度,注释或时间戳过滤或排序所有跟踪。选择跟踪后,您可以看到每个跨度所需的总跟踪时间百分比,从而可以识别问题应用程序。
这是翻译过来的原意,自己在这里想如果有个调用链,我们自己该怎么实现。要去质疑任何代码。
官方流程图:最关键的是transport这个地方,通过几种方式传输给conllector。如何在这里支持多种协议,有兴趣的可以进去看看源码。
开始示例,在这里通过一个项目调用不同的方法来进行测试。
先下载zipkin的web ui,通过java -jar zipkin.jar执行
项目结构:
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<dependency>
<groupid>org.projectlombok</groupid>
<artifactid>lombok</artifactid>
<optional> true </optional>
</dependency>
<!-- zipkin-->
<dependency>
<groupid>io.zipkin.brave</groupid>
<artifactid>brave-core</artifactid>
<version> 3.10 . 0 </version>
</dependency>
<dependency>
<groupid>io.zipkin.brave</groupid>
<artifactid>brave-spancollector-http</artifactid>
<version> 3.10 . 0 </version>
</dependency>
<dependency>
<groupid>io.zipkin.brave</groupid>
<artifactid>brave-web-servlet-filter</artifactid>
<version> 3.10 . 0 </version>
</dependency>
<dependency>
<groupid>io.zipkin.brave</groupid>
<artifactid>brave-okhttp</artifactid>
<version> 3.10 . 0 </version>
</dependency>
<!-- zipkin-->
|
application.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
|
server.port= 9000
##########请求的项目名##########
server.servlet.context-path=/zipkintest
##########zipkin################
zipkin.servicename=zipkin-test
zipkin.url=http: //localhost:9411
zipkin.connecttimeout= 6000
zipkin.readtimeout= 6000
zipkin.flushinterval= 1
zipkin.compressionenabled= true
|
- server.port 访问端口号
- server.servlet.context-path 访问项目名
- zipkin.servicename 服务名
- zipkin.url zipkin的web ui访问地址
- zipkin.connecttimeout 连接时间
- zipkin.readtimeout 读数据时间
- zipkin.flushinterval 采集率
- zipkin.compressionenabled 是否压缩
zipkinproperties.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package com.cms.zipkin;
import com.github.kristofa.brave.brave;
import com.github.kristofa.brave.emptyspancollectormetricshandler;
import com.github.kristofa.brave.sampler;
import com.github.kristofa.brave.spancollector;
import com.github.kristofa.brave.http.defaultspannameprovider;
import static com.github.kristofa.brave.brave.builder;
import static com.github.kristofa.brave.http.httpspancollector.config;
import static com.github.kristofa.brave.http.httpspancollector.create;
import com.github.kristofa.brave.okhttp.braveokhttprequestresponseinterceptor;
import com.github.kristofa.brave.servlet.braveservletfilter;
import lombok.data;
import okhttp3.okhttpclient;
import org.springframework.boot.context.properties.configurationproperties;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
/**
* @program: zjsz-user
* @description: zipkin配置
* @author: mr.yang
* @create: 2018-07-03 21:58
**/
@data
@configuration
@configurationproperties (prefix = zipkinproperties.zipkin_prefix)
public class zipkinproperties {
public static final string zipkin_prefix = "zipkin" ;
/**
* 服务名称
*/
private string servicename;
/**
* zipkin地址
*/
private string url;
/**
* 连接时间
*/
private int connecttimeout;
/**
* 读取时间
*/
private int readtimeout;
/**
* 每间隔多少秒执行一次span信息上传
*/
private int flushinterval;
/**
* 是否启动压缩
*/
private boolean compressionenabled;
/**
* @description: span(一次请求信息或者一次链路调用)信息收集器
* @param:
* @return: spancollector 控制器
* @author: mr.yang
* @date: 2018/7/3 0002
*/
@bean
public spancollector spancollector() {
config config = config.builder()
// 默认false,span在transport之前是否会被gzipped
.compressionenabled(compressionenabled)
.connecttimeout(connecttimeout)
.flushinterval(flushinterval)
.readtimeout(readtimeout)
.build();
return create(url, config, new emptyspancollectormetricshandler());
}
/**
* @description: 作为各调用链路,只需要负责将指定格式的数据发送给zipkin
* @param:
* @return:
* @author: mr.yang
* @date: 2018/7/3 0002
*/
@bean
public brave brave(spancollector spancollector) {
//调用服务的名称
builder builder = new builder(servicename);
builder.spancollector(spancollector);
//采集率
builder.tracesampler(sampler.always_sample);
return builder.build();
}
/**
* @description: 设置server的(服务端收到请求和服务端完成处理,并将结果发送给客户端)过滤器
* @param:
* @return: 过滤器
* @author: mr.yang
* @date: 2018/7/3 0002
*/
@bean
public braveservletfilter braveservletfilter(brave brave) {
braveservletfilter filter = new braveservletfilter(brave.serverrequestinterceptor(),
brave.serverresponseinterceptor(), new defaultspannameprovider());
return filter;
}
/**
* @description: 设置client的(发起请求和获取到服务端返回信息)拦截器
* @param:
* @return: okhttpclient 返回请求实例
* @author: mr.yang
* @date: 2018/7/3 0002
*/
@bean
public okhttpclient okhttpclient(brave brave) {
okhttpclient httpclient = new okhttpclient.builder()
.addinterceptor( new braveokhttprequestresponseinterceptor(
brave.clientrequestinterceptor(),
brave.clientresponseinterceptor(),
new defaultspannameprovider())).build();
return httpclient;
}
}
|
zipkinbravecontroller1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package com.cms.contorller;
import okhttp3.okhttpclient;
import okhttp3.request;
import okhttp3.response;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
/**
* @program: zjsz-user
* @description: 服务一
* @author: mr.yang
* @create: 2018-07-03 21:58
**/
@restcontroller
@requestmapping ( "server1" )
public class zipkinbravecontroller1 {
@autowired
private okhttpclient client;
/**
* @description: 第一步调用
* @param:
* @return: 字符串
* @author: mr.yang
* @date: 2018/7/3
*/
@requestmapping ( "/zipkin" )
public string service1() throws exception {
thread.sleep( 100 );
request request = new request.builder().url( "http://localhost:9000/zipkintest/server2/zipkin" ).build();
response response = client.newcall(request).execute();
return response.body().string();
}
}
|
zipkinbravecontroller2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.cms.contorller;
import okhttp3.okhttpclient;
import okhttp3.request;
import okhttp3.response;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
/**
* @program: zjsz-user
* @description: 服务二
* @author: mr.yang
* @create: 2018-07-03 21:58
**/
@restcontroller
@requestmapping ( "server2" )
public class zipkinbravecontroller2 {
@autowired
private okhttpclient client;
/**
* @description: 第二步调用
* @param:
* @return: 字符串
* @author: mr.yang
* @date: 2018/7/3
*/
@requestmapping ( "/zipkin" )
public string service1() throws exception {
thread.sleep( 100 );
request request = new request.builder().url( "http://localhost:9000/zipkintest/server3/zipkin" ).build();
response response = client.newcall(request).execute();
return response.body().string();
}
}
|
zipkinbravecontroller3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.cms.contorller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
/**
* @program: zjsz-user
* @description: 服务三
* @author: mr.yang
* @create: 2018-07-03 21:58
**/
@restcontroller
@requestmapping ( "server3" )
public class zipkinbravecontroller3 {
/**
* @description: 第三步调用
* @param:
* @return: 字符串
* @author: mr.yang
* @date: 2018/7/3
*/
@requestmapping ( "/zipkin" )
public string service1() throws exception {
thread.sleep( 200 );
return "你好,欢迎进入zipkin的世界" ;
}
}
|
项目启动后,访问http://localhost:9000/zipkintest/server1/zipkin 就可以看到
你好,欢迎进入zipkin的世界
我们通过http://localhost:9411/zipkin 查看zipkin的web ui
查看每条调用链的详情
后面还会讲关于zipkin将数据整合到mysql、elasticsearch中去。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/m0_37444820/article/details/80905760