淺談Spring-cloud 之 sleuth 服務(wù)鏈路跟蹤
這篇文章主要講述服務(wù)追蹤組件zipkin,Spring Cloud Sleuth集成了zipkin組件。
一、簡介
Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle examples), and you will see the correlation data being collected in logs, as long as you are logging requests.
Spring Cloud Sleuth 主要功能就是在分布式系統(tǒng)中提供追蹤解決方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相應(yīng)的依賴即可。
二、服務(wù)追蹤分析
微服務(wù)架構(gòu)上通過業(yè)務(wù)來劃分服務(wù)的,通過REST調(diào)用,對外暴露的一個接口,可能需要很多個服務(wù)協(xié)同才能完成這個接口功能,如果鏈路上任何一個服務(wù)出現(xiàn)問題或者網(wǎng)絡(luò)超時,都會形成導(dǎo)致接口調(diào)用失敗。隨著業(yè)務(wù)的不斷擴(kuò)張,服務(wù)之間互相調(diào)用會越來越復(fù)雜。
隨著服務(wù)的越來越多,對調(diào)用鏈的分析會越來越復(fù)雜。它們之間的調(diào)用關(guān)系也許如下:
三、術(shù)語
Span:基本工作單元,例如,在一個新建的span中發(fā)送一個RPC等同于發(fā)送一個回應(yīng)請求給RPC,span通過一個64位ID唯一標(biāo)識,trace以另一個64位ID表示,span還有其他數(shù)據(jù)信息,比如摘要、時間戳事件、關(guān)鍵值注釋(tags)、span的ID、以及進(jìn)度ID(通常是IP地址)
span在不斷的啟動和停止,同時記錄了時間信息,當(dāng)你創(chuàng)建了一個span,你必須在未來的某個時刻停止它。
Trace:一系列spans組成的一個樹狀結(jié)構(gòu),例如,如果你正在跑一個分布式大數(shù)據(jù)工程,你可能需要創(chuàng)建一個trace。
Annotation:用來及時記錄一個事件的存在,一些核心annotations用來定義一個請求的開始和結(jié)束
- cs - Client Sent -客戶端發(fā)起一個請求,這個annotion描述了這個span的開始
- sr - Server Received -服務(wù)端獲得請求并準(zhǔn)備開始處理它,如果將其sr減去cs時間戳便可得到網(wǎng)絡(luò)延遲
- ss - Server Sent -注解表明請求處理的完成(當(dāng)請求返回客戶端),如果ss減去sr時間戳便可得到服務(wù)端需要的處理請求時間
- cr - Client Received -表明span的結(jié)束,客戶端成功接收到服務(wù)端的回復(fù),如果cr減去cs時間戳便可得到客戶端從服務(wù)端獲取回復(fù)的所有所需時間
將Span和Trace在一個系統(tǒng)中使用Zipkin注解的過程圖形化:
四、構(gòu)建工程
基本知識講解完畢,下面我們來實(shí)戰(zhàn),本文的案例主要有三個工程組成:一個server-zipkin,它的主要作用使用ZipkinServer 的功能,收集調(diào)用數(shù)據(jù),并展示;一個service-hi,對外暴露hi接口;一個service-miya,對外暴露miya接口;這兩個service可以相互調(diào)用;并且只有調(diào)用了,server-zipkin才會收集數(shù)據(jù)的,這就是為什么叫服務(wù)追蹤了。
4.1 構(gòu)建server-zipkin
建一個spring-boot工程取名為server-zipkin,在其pom引入依賴:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在其程序入口類, 加上注解@EnableZipkinServer,開啟ZipkinServer的功能:
@SpringBootApplication @EnableZipkinServer public class ServerZipkinApplication { public static void main(String[] args) { SpringApplication.run(ServerZipkinApplication.class, args); } }
在配置文件application.yml指定服務(wù)端口為:
server.port=9411
4.2 創(chuàng)建service-hi
在其pom引入起步依賴spring-cloud-starter-zipkin,代碼如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--compile('org.springframework.cloud:spring-cloud-starter-zipkin')--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
在其配置文件application.yml指定zipkin server的地址,頭通過配置“spring.zipkin.base-url”指定:
server.port=8988 spring.zipkin.base-url=http://localhost:9411 spring.application.name=service-hi
通過引入spring-cloud-starter-zipkin依賴和設(shè)置spring.zipkin.base-url就可以了。
對外暴露接口:
@SpringBootApplication @RestController public class ServiceHiApplication { public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); } private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName()); @Autowired private RestTemplate restTemplate; @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } @RequestMapping("/hi") public String callHome(){ LOG.log(Level.INFO, "calling trace service-hi "); return restTemplate.getForObject("http://localhost:8989/miya", String.class); } @RequestMapping("/info") public String info(){ LOG.log(Level.INFO, "calling trace service-hi "); return "i'm service-hi"; } @Bean public AlwaysSampler defaultSampler(){ return new AlwaysSampler(); } }
4.3 創(chuàng)建service-miya
創(chuàng)建過程痛service-hi,引入相同的依賴,配置下spring.zipkin.base-url。
對外暴露接口:
@SpringBootApplication @RestController public class ServiceMiyaApplication { public static void main(String[] args) { SpringApplication.run(ServiceMiyaApplication.class, args); } private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName()); @RequestMapping("/hi") public String home(){ LOG.log(Level.INFO, "hi is being called"); return "hi i'm miya!"; } @RequestMapping("/miya") public String info(){ LOG.log(Level.INFO, "info is being called"); return restTemplate.getForObject("http://localhost:8988/info",String.class); } @Autowired private RestTemplate restTemplate; @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
4.4 啟動工程,演示追蹤
依次啟動上面的三個工程,打開瀏覽器訪問:http://localhost:9411/,會出現(xiàn)以下界面:
訪問:http://localhost:8989/miya,瀏覽器出現(xiàn):i'm service-hi
再打開http://localhost:9411/的界面,點(diǎn)擊Dependencies,可以發(fā)現(xiàn)服務(wù)的依賴關(guān)系:
點(diǎn)擊find traces,可以看到具體服務(wù)相互調(diào)用的數(shù)據(jù):
本文源碼下載: https://github.com/forezp/SpringCloudLearning/tree/master/chapter9
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Security 自定義資源服務(wù)器實(shí)踐過程
這篇文章主要介紹了Spring Security 自定義資源服務(wù)器實(shí)踐,我們通過自己搭建的授權(quán)服務(wù)器和資源服務(wù)器,完整體驗(yàn)了OAuth2流程,需要的朋友可以參考下2022-08-08詳解Spring boot上配置與使用mybatis plus
這篇文章主要介紹了詳解Spring boot上配置與使用mybatis plus,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05基于mybatis-plus-generator實(shí)現(xiàn)代碼自動生成器
這篇文章專門為小白準(zhǔn)備了入門級mybatis-plus-generator代碼自動生成器,可以提高開發(fā)效率。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05Idea+maven搭建SSH(struts2+hibernate5+spring5)環(huán)境的方法步驟
這篇文章主要介紹了Idea+maven搭建SSH(struts2+hibernate5+spring5)環(huán)境的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06JAVA抽象類和抽象方法(abstract)實(shí)例分析
這篇文章主要介紹了JAVA抽象類和抽象方法(abstract),結(jié)合實(shí)例形式分析了java抽象類及抽象方法相關(guān)定義、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-11-11Java實(shí)現(xiàn)批量下載(打包成zip)的實(shí)現(xiàn)
這篇文章主要介紹了Java實(shí)現(xiàn)批量下載(打包成zip)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11SpringBoot配置shiro安全框架的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot配置shiro安全框架的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Java Comparable及Comparator接口區(qū)別詳解
這篇文章主要介紹了Java Comparable及Comparator接口區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07Java中使用增強(qiáng)for循環(huán)的實(shí)例方法
在本篇文章里小編給大家整理是的關(guān)于Java中如何使用增強(qiáng)for循環(huán)的實(shí)例內(nèi)容以及相關(guān)代碼,需要的朋友們可以學(xué)習(xí)下。2019-08-08基于HTML5+js+Java實(shí)現(xiàn)單文件文件上傳到服務(wù)器功能
應(yīng)公司要求,在HTML5頁面上實(shí)現(xiàn)上傳文件到服務(wù)器功能,對于我這樣的菜鳥,真是把我難住了,最后還是請教大神搞定的,下面小編把例子分享到腳本之家平臺,供大家參考2017-08-08