問(wèn)題背景
上一篇文章使用了sleuth日志形式的鏈路追蹤决左,但微服務(wù)過(guò)多的時(shí)候,這種模式顯得非常臃腫無(wú)力,因此這個(gè)篇章使用zipkin可視化界面來(lái)查看追蹤鏈路
- 默認(rèn)已安裝JDK
- 可以根據(jù)前幾篇的文章和這篇這篇文章自己創(chuàng)建工程佛猛,也可以直接使用測(cè)試源碼
- zipkin服務(wù)端安裝包下載
- zipkin依賴是包含sleuth依賴的
- zipkin源碼官網(wǎng)文檔
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ì)列鏈路追蹤(四)
zipkin的下載安裝部署
1 前面幾篇文章講了nacos服務(wù)端的安裝,這篇文章講解一下zipkin的安裝码荔,zipkin有官方提供的jar包可以直接時(shí)候漩勤,本文作為測(cè)試工程就直接下載,沒(méi)有自己創(chuàng)建了
2 下載zipkin服務(wù)端缩搅,講一個(gè)通用的方法越败,以后大家也可以以這種方式下載
3 打開(kāi) https://search.maven.org/ 網(wǎng)站
4 在紅框中輸入需要查找的jar包,這里輸入zipkin-server
5 選擇紅框44所有版本
6 我選擇的是最新版本硼瓣,2.23.16版究飞,下載exec.jar版本才可以執(zhí)行,官網(wǎng)下載慢堂鲤,也可以下載我上傳的亿傅,在上文注意事項(xiàng)中
7 把java放入linux運(yùn)行,可以在本地運(yùn)行瘟栖,但前提都要有JDK環(huán)境葵擎,我個(gè)人把日志打印出來(lái)了
nohup java -jar zipkin-server-2.23.16-exec.jar >> zipkin.log &
8 默認(rèn)端口為9411,登錄http://127.0.0.1:9411/zipkin半哟,這里使用自己的IP即可
項(xiàng)目搭建
1 根據(jù)上一篇sleuth文章酬滤,更改sleuth的依賴為zipkin,zipkin依賴是包含sleuth依賴的
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
2 gateway的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-zipkin</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>
3 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-zipkin</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>
4 gateway添加application配置
server:
port: 3900
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: service2 #設(shè)置路由id(理論上是可以隨便寫(xiě)的)
uri: lb://service2 #設(shè)置路由的url lb://nacos服務(wù)注冊(cè)名稱 lb://service1 http://localhost:3901
predicates:
- Path=/three/** #路徑匹配規(guī)則寓涨,微服務(wù)必須有一個(gè)統(tǒng)一的入口盯串,不然網(wǎng)關(guān)不能訪問(wèn),@RequestMapping("/three")
- id: service1 #設(shè)置路由id(理論上是可以隨便寫(xiě)的)
uri: lb://service1 #設(shè)置路由的url lb://nacos服務(wù)注冊(cè)名稱 lb://service1 http://localhost:3901
predicates:
- Path=/four/** #路徑匹配規(guī)則戒良,@RequestMapping("/four")
zipkin:
base-url: http://10.10.195.199:9411/
sender:
type: web
locator:
discovery:
enabled: true
sleuth:
sampler:
probability: 1.0
5 service1添加application配置
server:
port: 3901
spring:
application:
name: service1
cloud:
nacos:
discovery:
server-addr: localhost:8848
zipkin:
base-url: http://10.10.195.199:9411/
sender:
type: web
locator:
discovery:
enabled: true
sleuth:
sampler:
probability: 1.0
6 service2添加application配置
server:
port: 3902
spring:
application:
name: service2
cloud:
nacos:
discovery:
server-addr: localhost:8848
zipkin:
base-url: http://10.10.195.199:9411/
sender:
type: web
locator:
discovery:
enabled: true
sleuth:
sampler:
probability: 1.0
7 其他不用更改
測(cè)試步驟
1 啟動(dòng)nacos服務(wù)端体捏,zipkin服務(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 查看zipkin奏司,點(diǎn)擊RUN QUERY,顯示調(diào)用鏈路
5 點(diǎn)擊紅框樟插,查看依賴鏈路
6 通過(guò)注冊(cè)中心使用微服務(wù)名顯示鏈路關(guān)系
心得
- 通過(guò)zipkin可視化界面看起來(lái)更清晰
- 這個(gè)篇章講的是http鏈路關(guān)系韵洋,此方式是同步的方式竿刁,微服務(wù)過(guò)多會(huì)產(chǎn)生一定延時(shí),下個(gè)篇章介紹使用rabbitMQ的方式
作為程序員第 20 篇文章搪缨,每次寫(xiě)一句歌詞記錄一下食拜,看看人生有幾首歌的時(shí)間,wahahaha ...