問(wèn)題背景
根據(jù)上一篇文章完成了 Gateway+FeignClient+Nacos 的初步搭建竹椒,這篇添加 Sleuth 鏈路追蹤
注意事項(xiàng):
- 可以根據(jù)文中的代碼自己創(chuàng)建工程呻畸,也可以直接下載源碼進(jìn)行學(xué)習(xí)
- 默認(rèn)已安裝JDK
- 可以根據(jù)文章自檢創(chuàng)建工程写穴,也可以直接下載源碼進(jìn)行參考
Gateway+Nacos+Sleuth+Zipkin網(wǎng)關(guān)鏈路追蹤(測(cè)試及源碼)共苛,Gateway+FeignClient+Nacos通過(guò)網(wǎng)關(guān)遠(yuǎn)程調(diào)用微服務(wù)(一)
Gateway+Nacos+Sleuth+Zipkin網(wǎng)關(guān)鏈路追蹤(測(cè)試及源碼),Gateway+Nacos+Sleuth鏈路追蹤(二)
Gateway+Nacos+Sleuth+Zipkin網(wǎng)關(guān)鏈路追蹤(測(cè)試及源碼)仪吧,Gateway+Nacos+Zipkin安裝部署可視化http方式鏈路追蹤(三)
Gateway+Nacos+Sleuth+Zipkin網(wǎng)關(guān)鏈路追蹤(測(cè)試及源碼)庄新,Gateway+Nacos+Zipkin持久化mysql存儲(chǔ)rabbitmq消息隊(duì)列鏈路追蹤(四)
項(xiàng)目搭建
1 sleuth鏈路追蹤可以直接根據(jù)上一篇文章的代碼,再添加一個(gè)sleuth依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
整個(gè)pom如下薯鼠,service公共pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yg</groupId>
<artifactId>sleuthTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
<module>service1</module>
<module>service2</module>
</modules>
<name>sleuthTest</name>
<description>sleuthTest</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!-- <version>2.1.0.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--Spring Cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
gateway服務(wù)的pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yg</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>gateway</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<!-- <version>2.1.0.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- <scope>test</scope>-->
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!-- <version>2.1.0.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2 其他東西都不用改择诈,只需要引入依賴,現(xiàn)在新的版本logback都不用添加出皇,默認(rèn)會(huì)打印traceID和spanID
項(xiàng)目測(cè)試
1 啟動(dòng)nacos服務(wù)端羞芍,gateway,service1郊艘,service2
2 查看注冊(cè)中心http://localhost:8848/nacos荷科,密碼和賬號(hào)都為:nacos
3 使用網(wǎng)關(guān)路由調(diào)用service1微服務(wù)API唯咬,service1微服務(wù)調(diào)用service2
4 查看gateway的日志,可以看到紅框步做,gateway是微服務(wù)名副渴,后面接著是traceID和spanID
5 查看service1微服務(wù)的日志,service1微服務(wù)名全度,后面接著是traceID和spanID
6 可以從上兩張圖看到service1的traceID和gateway的traceID和spanID相同,gateway是起始調(diào)用斥滤,所以traceID和spanID相同
7 如果微服務(wù)過(guò)多将鸵,這種方式查看及其不方便,所以下一章介紹zipkin
心得
- sleuth添加非常簡(jiǎn)單佑颇,添加即用顶掉,但查看不方便,引出zipkin帶有web界面可視化
作為程序員第 19 篇文章挑胸,每次寫(xiě)一句歌詞記錄一下痒筒,看看人生有幾首歌的時(shí)間,wahahaha ...