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