一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot2.0 ZipKin示例代碼

SpringBoot2.0 ZipKin示例代碼

2021-06-11 13:26攻城遭雷劈 Java教程

這篇文章主要介紹了SpringBoot2.0 ZipKin示例代碼,詳細的介紹了什么是ZipKin以及SpringBoot2.0 ZipKin示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下

zipkin是一種分布式跟蹤系統。它有助于收集解決微服務架構中延遲問題所需的時序數據。它管理這些數據的收集和查找。zipkin的設計基于 google dapper論文。

應用程序用于向zipkin報告時間數據。zipkin用戶界面還提供了一個依賴關系圖,顯示每個應用程序有多少跟蹤請求。如果您正在解決延遲問題或錯誤問題,則可以根據應用程序,跟蹤長度,注釋或時間戳過濾或排序所有跟蹤。選擇跟蹤后,您可以看到每個跨度所需的總跟蹤時間百分比,從而可以識別問題應用程序。

這是翻譯過來的原意,自己在這里想如果有個調用鏈,我們自己該怎么實現。要去質疑任何代碼。

官方流程圖:最關鍵的是transport這個地方,通過幾種方式傳輸給conllector。如何在這里支持多種協議,有興趣的可以進去看看源碼。

SpringBoot2.0 ZipKin示例代碼

開始示例,在這里通過一個項目調用不同的方法來進行測試。

先下載zipkin的web ui,通過java -jar zipkin.jar執行

SpringBoot2.0 ZipKin示例代碼

項目結構:

SpringBoot2.0 ZipKin示例代碼

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

SpringBoot2.0 ZipKin示例代碼

查看每條調用鏈的詳情

SpringBoot2.0 ZipKin示例代碼

后面還會講關于zipkin將數據整合到mysql、elasticsearch中去。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/m0_37444820/article/details/80905760

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人精品一区二区 | 99久久精品免费看国产四区 | 精品视频一区二区三区免费 | 2021海角社区最新版 | 亚洲系列在线 | 亚洲国产精品久久久久久 | 不良网站在线观看 | xxxx意大利xxxxhd| 欧美黑人ⅹxxx片 | 国产人成77777视频网站 | 亚洲国产精品综合久久网络 | 韩国美女vip内部2020 | 精品性影院一区二区三区内射 | 亚洲六月丁香六月婷婷蜜芽 | 免费看又黄又爽又猛的视频软件- | 性欧美video 性满足久久久久久久久 | 天天舔天天干 | 亚洲风情无码免费视频 | 国产在线步兵一区二区三区 | 男人的j伸到女人的屁股眼 男人吃奶动态图 | 欧美一级鲁丝片免费看 | 单身男女韩剧在线看 | 国产高清不卡视频在线播放 | 扒开双腿猛进入爽爽视频ai | 天天色色色 | 国产东北三老头伦一肥婆 | 青草国产| 国产午夜免费不卡精品理论片 | 日韩在线一区二区三区 | 丝袜捆绑调教视频免费区 | 日本在线亚州精品视频在线 | 调教女高中生第3部分 | 国产亚洲人成网站在线观看不卡 | 污小说在线阅读 | 羞羞视频麻豆 | 免费369看片入口 | 精品久久久久中文字幕日本 | 91亚洲精品久久91综合 | 99久久爱热6在线播放 | 4455永久在线视频观看 | 欧美成人免费一区在线播放 |