問(wèn)題背景
上一篇文章使用了zipkin的追蹤鏈路使用的是http的方式后豫,是一種同步的方式悉尾,其中有一個(gè)微服務(wù)一直沒(méi)有返回,鏈路會(huì)一直卡住挫酿,并且默認(rèn)使用內(nèi)存記錄的构眯,一旦zipkin重啟之后,之前的鏈路就消失了早龟,所這個(gè)篇章介紹使用mysql進(jìn)行持久化惫霸,并且使用rabbitmq進(jìn)行異步消費(fèi)猫缭,提高效率
注意事項(xiàng):
- 默認(rèn)已安裝JDK
- 默認(rèn)安裝mysql,可以根據(jù)這篇文章進(jìn)行安裝msql
- nacos服務(wù)端可以參照前幾篇文章進(jìn)行安裝
- zipkin源碼官網(wǎng)文檔
- 可以根據(jù)文章代碼自己創(chuàng)建工程壹店,也可以直接下載測(cè)試源碼
- rabbitmq下載地址
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ì)列鏈路追蹤(四)
rabbitmq安裝部署
1 解壓壓縮包之后,可以看見(jiàn)以下幾個(gè)文件
2 通過(guò)xftp把文件放入centos通過(guò)指令進(jìn)行安裝老赤,注意安裝順序需要安裝下面來(lái)安裝
rpm -ivh erlang-21.3.8.16-1.el7.x86_64.rpm
rpm -ivh socat-1.7.3.2-5.el7.lux.x86_64.rpm
rpm -ivh rabbitmq-server-3.8.6-1.el7.noarch.rpm
3 啟動(dòng)rabbitmq
systemctl start rabbitmq-server.service
4 查看進(jìn)程號(hào)
ps -ef | grep rabbitmq
5 關(guān)閉防火墻
systemctl stop firewalld
6 瀏覽器輸入http://10.10.195.199:15672,輸入你所安裝的IP
7 默認(rèn)的賬號(hào)guest制市,密碼guest抬旺,在rabbitmq3.0版本之后不允許遠(yuǎn)程訪(fǎng)問(wèn)了,User can only log in via localhost祥楣,可以創(chuàng)建遠(yuǎn)程訪(fǎng)問(wèn)的用戶(hù)
8 創(chuàng)建遠(yuǎn)程用戶(hù)开财,賬號(hào):root 密碼:123456
rabbitmqctl add_user root 123456
9 設(shè)置root用戶(hù)角色
rabbitmqctl set_user_tags root administrator
10 設(shè)置root用戶(hù)權(quán)限
rabbitmqctl set_permissions -p "/" root ".*" ".*" ".*"
11 查看當(dāng)前用戶(hù)和角色
rabbitmqctl list_users
12 驗(yàn)證用戶(hù)鑒權(quán)
rabbitmqctl authenticate_user 'root' '123456'
13 登錄http://10.10.195.199:15672,輸入root误褪,密碼:123456
項(xiàng)目搭建
1 gateway微服務(wù)
1.1 通過(guò)前幾篇文章的代碼進(jìn)行更改责鳍,引入pom依賴(lài),新添加rabbitmq依賴(lài)
<?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>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</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>
1.2 更改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è)名稱(chēng) lb://service1 http://localhost:3901
predicates:
- Path=/three/** #路徑匹配規(guī)則兽间,微服務(wù)必須有一個(gè)統(tǒng)一的入口历葛,不然網(wǎng)關(guān)不能訪(fǎng)問(wèn),@RequestMapping("/three")
- id: service1 #設(shè)置路由id(理論上是可以隨便寫(xiě)的)
uri: lb://service1 #設(shè)置路由的url lb://nacos服務(wù)注冊(cè)名稱(chēng) lb://service1 http://localhost:3901
predicates:
- Path=/four/** #路徑匹配規(guī)則嘀略,@RequestMapping("/four")
zipkin:
base-url: http://10.10.195.199:9411/
sender:
type: rabbit # web
rabbitmq:
queue: zipkin # 隊(duì)列名稱(chēng)
locator:
discovery:
enabled: true
sleuth:
sampler:
probability: 1.0
rabbitmq:
host: 10.10.195.199
port: 5672 # 單機(jī)端口
username: root # 賬號(hào)
password: 123456 # 密碼
virtual-host: / # 虛擬主機(jī)地址
listener:
direct:
retry:
enabled: true # 開(kāi)啟發(fā)布重試
max-attempts: 5 #重試次數(shù)
initial-interval: 5000 # 重試間隔
simple:
retry:
enabled: true #開(kāi)啟消費(fèi)重試
max-attempts: 5 # 重試次數(shù)
initial-interval: 5000 #重試間隔
2 service微服務(wù)
2.1 更改service公共pom文件恤溶,service1和service2單獨(dú)的pom文件沒(méi)有變化
<?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>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</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>
2.2 更改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
rabbitmq:
host: 10.10.195.199
port: 5672 # 單機(jī)端口
username: root # 賬號(hào)
password: 123456 # 密碼
virtual-host: / # 虛擬主機(jī)地址
listener:
direct:
retry:
enabled: true # 開(kāi)啟發(fā)布重試
max-attempts: 5 #重試次數(shù)
initial-interval: 5000 # 重試間隔
simple:
retry:
enabled: true #開(kāi)啟消費(fèi)重試
max-attempts: 5 # 重試次數(shù)
initial-interval: 5000 #重試間隔
2.3 更改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
rabbitmq:
host: 10.10.195.199
port: 5672 # 單機(jī)端口
username: root # 賬號(hào)
password: 123456 # 密碼
virtual-host: / # 虛擬主機(jī)地址
listener:
direct:
retry:
enabled: true # 開(kāi)啟發(fā)布重試
max-attempts: 5 #重試次數(shù)
initial-interval: 5000 # 重試間隔
simple:
retry:
enabled: true #開(kāi)啟消費(fèi)重試
max-attempts: 5 # 重試次數(shù)
initial-interval: 5000 #重試間隔
測(cè)試步驟
1 啟動(dòng)nacos服務(wù)端,zipkin服務(wù)端帜羊,gateway咒程,service1,service2
- 啟動(dòng)zipkin的參數(shù)變了讼育,由于是zipkin和rabbitmq在一個(gè)服務(wù)器帐姻,所以地址使用的是127.0.0.1,沒(méi)有使用這個(gè)地址會(huì)報(bào)這個(gè)錯(cuò)誤:PLAIN login refused: user 'root' - invalid credentials
- 在服務(wù)器下面就可以使用guest賬號(hào)和密碼了
- --RABBIT_QUEUE=zipkin奶段,如果沒(méi)有zipkin這個(gè)隊(duì)列饥瓷,會(huì)自動(dòng)創(chuàng)建
nohup java -jar zipkin-server-2.23.16-exec.jar --STORAGE_TYPE=mysql --MYSQL_HOST=localhost --MYSQL_TCP_PORT=3306 --MYSQL_USER=root --MYSQL_PASS=123456 --MYSQL_DB=zipkin --RABBIT_ADDRESSES=127.0.0.1:5672 --RABBIT_USER=guest --RABBIT_PASSWORD=guest --RABBIT_VIRTUAL_HOST=/ --RABBIT_QUEUE=zipkin >> zipkin.log &
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 查看rabbitmq隊(duì)列扛伍,業(yè)務(wù)發(fā)布消息,由于zipkin馬上就消費(fèi)了词裤,所以看不到ready的數(shù)量刺洒,但可以把zipkin先關(guān)閉鳖宾,查看后再啟動(dòng)
5 查看zipkin,點(diǎn)擊RUN QUERY逆航,顯示調(diào)用鏈路
6 點(diǎn)擊紅框鼎文,查看依賴(lài)鏈路
7 通過(guò)注冊(cè)中心使用微服務(wù)名顯示鏈路關(guān)系
心得
- 做的這套組件,中間調(diào)試的版本兼容bug太多了因俐,中間件都要自己安裝拇惋,比較麻煩,有時(shí)間講解一下docker吧抹剩,測(cè)試方便
作為程序員第 21 篇文章撑帖,每次寫(xiě)一句歌詞記錄一下,看看人生有幾首歌的時(shí)間澳眷,wahahaha ...