這篇文章主要介紹了SpringBoot2.0中ZipKin的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
10多年的黃南州網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷型網(wǎng)站的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整黃南州建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“黃南州網(wǎng)站設(shè)計(jì)”,“黃南州網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
Zipkin是一種分布式跟蹤系統(tǒng)。它有助于收集解決微服務(wù)架構(gòu)中延遲問(wèn)題所需的時(shí)序數(shù)據(jù)。它管理這些數(shù)據(jù)的收集和查找。Zipkin的設(shè)計(jì)基于 Google Dapper論文。
應(yīng)用程序用于向Zipkin報(bào)告時(shí)間數(shù)據(jù)。Zipkin用戶界面還提供了一個(gè)依賴關(guān)系圖,顯示每個(gè)應(yīng)用程序有多少跟蹤請(qǐng)求。如果您正在解決延遲問(wèn)題或錯(cuò)誤問(wèn)題,則可以根據(jù)應(yīng)用程序,跟蹤長(zhǎng)度,注釋或時(shí)間戳過(guò)濾或排序所有跟蹤。選擇跟蹤后,您可以看到每個(gè)跨度所需的總跟蹤時(shí)間百分比,從而可以識(shí)別問(wèn)題應(yīng)用程序。
這是翻譯過(guò)來(lái)的原意,自己在這里想如果有個(gè)調(diào)用鏈,我們自己該怎么實(shí)現(xiàn)。要去質(zhì)疑任何代碼。
官方流程圖:最關(guān)鍵的是Transport這個(gè)地方,通過(guò)幾種方式傳輸給Conllector。如何在這里支持多種協(xié)議,有興趣的可以進(jìn)去看看源碼。
開始示例,在這里通過(guò)一個(gè)項(xiàng)目調(diào)用不同的方法來(lái)進(jìn)行測(cè)試。
先下載Zipkin的web UI,通過(guò)java -jar zipkin.jar執(zhí)行
項(xiàng)目結(jié)構(gòu):
pom.xml
org.projectlombok lombok true io.zipkin.brave brave-core 3.10.0 io.zipkin.brave brave-spancollector-http 3.10.0 io.zipkin.brave brave-web-servlet-filter 3.10.0 io.zipkin.brave brave-okhttp 3.10.0
application.properties
server.port=9000 ##########請(qǐng)求的項(xiàng)目名########## 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 訪問(wèn)端口號(hào)
server.servlet.context-path 訪問(wèn)項(xiàng)目名
zipkin.serviceName 服務(wù)名
zipkin.url Zipkin的web ui訪問(wèn)地址
zipkin.connectTimeout 連接時(shí)間
zipkin.readTimeout 讀數(shù)據(jù)時(shí)間
zipkin.flushInterval 采集率
zipkin.compressionEnabled 是否壓縮
ZipkinProperties.java
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"; /** * 服務(wù)名稱 */ private String serviceName; /** * zipkin地址 */ private String url; /** * 連接時(shí)間 */ private int connectTimeout; /** * 讀取時(shí)間 */ private int readTimeout; /** * 每間隔多少秒執(zhí)行一次Span信息上傳 */ private int flushInterval; /** * 是否啟動(dòng)壓縮 */ private boolean compressionEnabled; /** * @Description: span(一次請(qǐng)求信息或者一次鏈路調(diào)用)信息收集器 * @Param: * @return: SpanCollector 控制器 * @Author: Mr.Yang * @Date: 2018/7/3 0002 */ @Bean public SpanCollector spanCollector() { Config config = Config.builder() // 默認(rèn)false,span在transport之前是否會(huì)被gzipped .compressionEnabled(compressionEnabled) .connectTimeout(connectTimeout) .flushInterval(flushInterval) .readTimeout(readTimeout) .build(); return create(url, config, new EmptySpanCollectorMetricsHandler()); } /** * @Description: 作為各調(diào)用鏈路,只需要負(fù)責(zé)將指定格式的數(shù)據(jù)發(fā)送給zipkin * @Param: * @return: * @Author: Mr.Yang * @Date: 2018/7/3 0002 */ @Bean public Brave brave(SpanCollector spanCollector) { //調(diào)用服務(wù)的名稱 Builder builder = new Builder(serviceName); builder.spanCollector(spanCollector); //采集率 builder.traceSampler(Sampler.ALWAYS_SAMPLE); return builder.build(); } /** * @Description: 設(shè)置server的(服務(wù)端收到請(qǐng)求和服務(wù)端完成處理,并將結(jié)果發(fā)送給客戶端)過(guò)濾器 * @Param: * @return: 過(guò)濾器 * @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: 設(shè)置client的(發(fā)起請(qǐng)求和獲取到服務(wù)端返回信息)攔截器 * @Param: * @return: OkHttpClient 返回請(qǐng)求實(shí)例 * @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
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: 服務(wù)一 * @author: Mr.Yang * @create: 2018-07-03 21:58 **/ @RestController @RequestMapping("server1") public class ZipkinBraveController1 { @Autowired private OkHttpClient client; /** * @Description: 第一步調(diào)用 * @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
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: 服務(wù)二 * @author: Mr.Yang * @create: 2018-07-03 21:58 **/ @RestController @RequestMapping("server2") public class ZipkinBraveController2 { @Autowired private OkHttpClient client; /** * @Description: 第二步調(diào)用 * @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
package com.cms.contorller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @program: zjsz-user * @description: 服務(wù)三 * @author: Mr.Yang * @create: 2018-07-03 21:58 **/ @RestController @RequestMapping("server3") public class ZipkinBraveController3 { /** * @Description: 第三步調(diào)用 * @Param: * @return: 字符串 * @Author: Mr.Yang * @Date: 2018/7/3 */ @RequestMapping("/zipkin") public String service1() throws Exception { Thread.sleep(200); return "你好,歡迎進(jìn)入Zipkin的世界"; } }
項(xiàng)目啟動(dòng)后,訪問(wèn)http://localhost:9000/zipkinTest/server1/zipkin 就可以看到
你好,歡迎進(jìn)入Zipkin的世界
我們通過(guò)http://localhost:9411/zipkin 查看zipkin的web UI
查看每條調(diào)用鏈的詳情
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“SpringBoot2.0中ZipKin的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!