javascript
Spring cloud(Finchley)微服务框架,sleuth整合zipkin链路追踪失效的问题
一、首先說問題:
- 1、springCloud在使用鏈路追蹤組件sleuth整合zipkin的過程中鏈路追蹤信息切都是正常;
- 2、微服務(wù)太多需要使用組件Config對每個(gè)微服務(wù)的的配置文件進(jìn)行統(tǒng)一管理;
- 3、config對微服務(wù)進(jìn)行管理,但是如果某一個(gè)配置發(fā)生改變,如果每個(gè)微服務(wù)都要重啟才能獲取最新的配置文件就不合理,那么springCloud提供了spring cloud Bus組件來刷新配置,使用消息中間件(官方推薦RabbitMQ)不需要重啟微服務(wù)就可以獲取最新配置。
- 4、當(dāng)spring cloud Bus 配置完成之后zipkin完全收集不到鏈路追蹤信息了。。。
二、尋找解決方案
最開始遇到這個(gè)問題時(shí),認(rèn)為是自己配置在哪里有問題,反復(fù)確認(rèn)沒有問題。然后開始狂論壇,問大牛。得到的答案是springcloud不成熟,可能存在bug,既然有bug(我也不知道該怎么解決),但是如果鏈路追蹤不能使用,對整個(gè)微服務(wù)的調(diào)用依賴,每個(gè)微服務(wù)的調(diào)用請求耗時(shí)就沒有辦法定位,對后期的維護(hù)是不利的。只能放棄分布式配置,但是也不好,如果修改了一個(gè)配置文件,每個(gè)微服務(wù)都要重啟,對于后期的維護(hù)也是不利的。只能繼續(xù)尋找解決方案。
三、問題分析
- 1、出現(xiàn)這個(gè)問題,首先想到的是要去官方文檔上尋找答案;丟一個(gè)springcloud文檔:springcloud Finchley SR2。
- 2、使用springcloud Bus需要使用rabbitMQ,但是zipkin收集信息是http(文檔)
同時(shí)zipkin也支持使用RabbitMQ來收集鏈路信息,猜想是不是在配置中使用了RabbitMQ收集,而HTTP的方式失效了,那么就有兩個(gè)解決方案:其中一就是只是用HTTP的方式,不是用RabbitMQ;二:就是將zipkin收集鏈路信息的方式修改為RabbitMQ。方式一簡單有效,但是沒有找到相應(yīng)的配置,那么就使用方案二,在官方文檔中也有描述將zipkin的獲取方式修改為rabbitMq或者kafka:
If you want to use RabbitMQ or Kafka instead of HTTP, add the spring-rabbit or spring-kafka dependency. The default destination name is zipkin.If using Kafka, you must set the property spring.zipkin.sender.type property accordingly:spring.zipkin.sender.type: kafka [Caution] Caution spring-cloud-sleuth-stream is deprecated and incompatible with these destinations.If you want Sleuth over RabbitMQ, add the spring-cloud-starter-zipkin and spring-rabbit dependencies.The following example shows how to do so for Gradle:Maven. <dependencyManagement> 1<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${release.train.version}</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement><dependency> 2<groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> 3<groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId> </dependency> 1、We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.2、Add the dependency to spring-cloud-starter-zipkin. That way, all nested dependencies get downloaded.3、To automatically configure RabbitMQ, add the spring-rabbit dependency.通過官方文檔的說明按照上述的三個(gè)步驟就能實(shí)現(xiàn),但是按照上述的步驟依舊是沒有追蹤到鏈路信息。
上述的配置說明微服務(wù)的客戶端應(yīng)該是沒有問題的,那么問題可能出現(xiàn)在zipkin服務(wù)器上。
四、zipkin服務(wù)器的問題
從spring boot 2.0開始官方就官方不再支持使用自建Zipkin Server的方式進(jìn)行服務(wù)鏈路追蹤,而是直接提供了編譯好的 jar 包來給我們使用。
但是從1.0時(shí)代的配置中應(yīng)該是可以看出問題,在1.0的Zipkin Service中需要加入sleuth和rabbit的依賴,同時(shí)需要配置rabbitMQ消息的地址,但是使用jar包直接啟動(dòng),并沒有指定rabbit的地址。
查詢spring-cloud-sleuth在github上的源碼,在源碼中會(huì)有一些人遇到一些問題,會(huì)不會(huì)有同樣的問題,解決方案。其中有一個(gè)問題:
有一個(gè)回答:
I also have the same problem and the workaround provided in Gitter has helped me to solve it until the permanent solution is in place. For the benefit of others here is the workaround.Explicitly configure spring.rabbitmq.addresses property to point to the credentials of the bounded RabbitMQ service. For example, if the bounded service is called rabbitmq the following configuration will work:spring.rabbitmq.addresses=${vcap.services.rabbitmq.credentials.uri}也就是說在啟動(dòng)zipkin的時(shí)候指定一下rabbitMq的地址。問題
五、解決方案
1、在需要收集鏈路追蹤的客戶端添加依賴:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zipkin</artifactId></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId></dependency>2、下載zipk的jar包這里使用了zipkin-server-2.11.1-exec
在啟動(dòng)zipkin的之后設(shè)置參數(shù)
或者使用docker
添加參數(shù)
在docker啟動(dòng)后還是有問題,最后發(fā)現(xiàn)是localhost的問題,將localhost修改成rabbit對應(yīng)的服務(wù)器IP地址就好了。
啟動(dòng)之后在RabbitMq中會(huì)出現(xiàn)一個(gè)zipkin的隊(duì)列。
另一種方式,使用http就比較簡單,在項(xiàng)目中加入
zipkin:base-url: http://ip:9411# 使用http的方式收集鏈路追蹤信息,默認(rèn)是使用rabbitMQ,這樣在使用了spring cloud Bus之后鏈路追蹤就不會(huì)失效了sender:type: web但是推薦使用rabbit。
最后在zipkin的頁面上就可以看到鏈路追蹤的信息了
總結(jié)
以上是生活随笔為你收集整理的Spring cloud(Finchley)微服务框架,sleuth整合zipkin链路追踪失效的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 会员钻是什么
- 下一篇: java json的使用方法_JAVA编