SpringCloud_Sleuth分布式鏈路請求跟蹤的示例代碼
Spring Cloud Sleuth是一款針對Spring Cloud的分布式跟蹤工具。它借鑒了Dapper、Zipkin和HTrace。
特點
- 將trace Id和 span Id 添加到Slf4J MDC,這樣就可以在日志聚合器中從給定的trace或span提取所有日志。
- 提供通用分布式跟蹤數(shù)據(jù)模型的抽象。
spring-cloud-sleuth-zipkin
可收集到zipkin server。
代碼鏈接https://github.com/lidonglin-bit/cloud
一、概述
1.為什么會出現(xiàn)這個技術(shù)?需要解決哪些問題?
問題
在微服務(wù)框架中,一個由客戶端發(fā)起的請求在后端系統(tǒng)中會經(jīng)過多個不同的服務(wù)節(jié)點調(diào)用來協(xié)同產(chǎn)生最后的請求結(jié)果,每一個前端請求都會形成一個復(fù)雜的分布式服務(wù)調(diào)用鏈路,鏈路中的任何一環(huán)出現(xiàn)高延時或錯誤都會引起整個請求最后的失敗。
2.是什么
Spring Cloud Sleuth提供了一套完整的服務(wù)跟蹤的解決方案
在分布式系統(tǒng)中提供追蹤解決方案并且兼容支持了zipkin(負(fù)責(zé)展現(xiàn))
https://github.com/spring-cloud/spring-cloud-sleuth
https://cloud.spring.io/spring-cloud-sleuth/reference/html/
3.解決
二、搭建鏈路監(jiān)控步驟
1.zipkin
下載
SpringCloud從F版起已不需要自己構(gòu)建Zipkin server了,只需要調(diào)用jar包即可
zipkin-server-2.12.9.exec.jar
運行jar
java -jar zipkin-server-2.12.9-exec.jar
運行控制臺
http://localhost:9411/zipkin/
術(shù)語
完整的調(diào)用鏈路
表示一請求鏈路,一條鏈路通過Trace Id唯一標(biāo)識,Span標(biāo)識發(fā)起的請求信息,各span通過parent id 關(guān)聯(lián)起來。
上圖what
一條鏈路通過Trace Id唯一標(biāo)識,Span標(biāo)識發(fā)起的請求信息,各span通過parent id 關(guān)聯(lián)起來。
整個鏈路的依賴關(guān)系如下:
名詞解釋
Trace:類似于樹結(jié)構(gòu)的Span集合,表示一條調(diào)用鏈路,存在唯一標(biāo)識
span:表示調(diào)用鏈路來源,通俗的理解span就是一次請求信息
2.服務(wù)提供者
1.修改:cloud-provider-payment8001
2.POM
<!--包含了sleuth+zipkin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
3.YML
server: port: 8001 spring: application: name: cloud-payment-service zipkin: base-url: http://localhost:9411 sleuth: sampler: #采樣率值介于0~1之間,1表示全部采樣 probability: 1 datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/cloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root mybatis: mapperLocations: classpath:/mapper/*.xml type-aliases-package: com.donglin.springcloud.entities eureka: client: register-with-eureka: true fetchRegistry: true service-url: defaultZone: http://localhost:7001/eureka
4.業(yè)務(wù)類PaymentController
@GetMapping("/payment/zipkin") public String paymentZipkin(){ return "hi ,i'am paymentzipkin server,welcome to donglin,O(∩_∩)O哈哈~"; }
3.服務(wù)消費者(調(diào)用方)
1.修改:cloud-consumer-order80
2.POM
<!--包含了sleuth+zipkin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
3.YML
server: port: 80 spring: application: name: cloud-order-service zipkin: base-url: http://localhost:9411 sleuth: sampler: probability: 1 eureka: client: #表示是否將自己注冊進(jìn)EurekaServer默認(rèn)為true。 register-with-eureka: false #是否從EurekaServer抓取已有的注冊信息,默認(rèn)為true。單節(jié)點無所謂,集群必須設(shè)置為true才能配合ribbon使用負(fù)載均衡 fetchRegistry: true service-url: #單機 defaultZone: http://localhost:7001/eureka #集群 #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
4.業(yè)務(wù)類PaymentController
//==> zipkin+sleuth @GetMapping("/consumer/payment/zipkin") public String paymentZipkin(){ String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class); return result; }
4.依次啟動eureka7001/8001/80
80調(diào)用8001幾次測試下
5.打開瀏覽器訪問: http://localhost:9411
會出現(xiàn)以下界面
查看
查看依賴關(guān)系
到此這篇關(guān)于SpringCloud_Sleuth分布式鏈路請求跟蹤的文章就介紹到這了,更多相關(guān)SpringCloud_Sleuth分布式鏈路跟蹤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇上
Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。國內(nèi)又以Mybatis用的多,基于mybatis上的增強框架,又有mybatis plus和TK mybatis等。今天我們介紹一個新的mybatis增強框架 fluent mybatis2021-10-10教你如何用Eclipse創(chuàng)建一個Maven項目
這篇文章主要介紹了教你如何用Eclipse創(chuàng)建一個Maven項目,文中有非常詳細(xì)的代碼示例,對正在入門Java的小伙伴們是非常有幫助的喲,需要的朋友可以參考下2021-05-05使用springboot整合websocket實現(xiàn)群聊教程
websocket怎么說呢,就是服務(wù)器可以主動向客戶端發(fā)起對話,下面就是springboot整合websocket實現(xiàn)群聊的操作代碼,一起來看一下get新技能吧2021-08-08