最近在學習spring cloud構建微服務,研究追蹤微服務rest服務調用鏈路的問題,接觸到zipkin,而spring cloud也提供了spring-cloud-sleuth來方便集成zipkin實現。java
咱們準備了三個必要的程序來作測試,分別是
一、microservice-zipkin-server
負責數據收集以及信息展現功能。
二、microservice-zipkin-client-backend
負責微服務的生產者,對外提供 「http://127.0.0.1:11010/call/1」 的地址來完成一個簡單的功能rest請求。
三、microservice-zipkin-client
負責微服務的調用,對外提供 "http://localhost:11009/call/1" 的rest地址,當訪問此地址時,使用feign方式調用provider的rest服務地址。獲得計算結果後,顯示在界面上。git
三個程序功能很是簡單,接下來咱們看看每一個程序的具體代碼和配置。爲了方便咱們對三個模塊開發,咱們在父POM文件中添加了spring-boot和spring-cloud的依賴,避免子模塊中須要寫版本號github
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
接下來咱們看看三個程序中的相關配置redis
首先,咱們添加maven依賴配置spring
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
在這裏爲了測試方便,咱們能夠將數據保存到內存中,可是生產環境仍是須要將數據持久化中。原生支持了不少產品,例如ES、數據庫等,本例中咱們採用將數據保存到內存中的方式來演示。數據庫
咱們寫一個啓動類ZipkinServer,代碼很是簡單,以下瀏覽器
package com.yangyang.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import zipkin.server.EnableZipkinServer; /** * Created by chenshunyang on 2017/5/24. */ @EnableZipkinServer @SpringBootApplication public class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class,args); } }
接下來咱們配置application.ym配置文件緩存
server: port: 11008 spring: application: name : microservice-zipkin-server
啓動後 無異常輸出,這樣咱們的server程序就OK了app
這兩個程序,與其餘基礎代碼咱們就很少講了(相信學些到這一步的童鞋,都已經對spring cloud建立微服務以上手了),兩個程序在spring-cloud-sleuth相關的配置都是同樣。
首先,咱們要在兩者的POM文件中添加依賴,引入zipkin客戶端自動配置相關依賴異步
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
其次,在配置文件application中,咱們加入zipkin server收集信息的地址
spring: application: name: microservice-zipkin-client-backend zipkin: base-url: http://localhost:11008
這樣咱們的兩個微服務就配置好了(注意這裏咱們並不會再說明如何寫rest接口和使用feign調用rest接口),具體參考代碼
前後啓動咱們的三個程序
先啓動microservice-zipkin-server 而後訪問在瀏覽器中訪問UI界面地址http://127.0.0.1:11008,
能夠看到以下效果
其中Span Name選項爲灰色不可選,說明目前沒有數據,咱們查看數據庫也能夠看到沒有任何數據信息。
接下來咱們訪問consumer提供的訪問地址 http://localhost:11009/call/1
刷新幾回以後,咱們再次刷新咱們的zipkin界面,能夠看到Span Name已經能夠選擇了。
點擊Find Traces按鈕,咱們能夠看到調用的鏈路和耗時狀況,點擊Dependencies,咱們能夠看到provider和consumer的調用圖
OK,咱們的簡單實用spring-cloud-sleuth+zipkin的例子就完成了。
在測試的過程當中咱們會發現,有時候,程序剛剛啓動後,刷新幾回,並不能看到任何數據,緣由就是咱們的spring-cloud-sleuth收集信息是有必定的比率的,默認的採樣率是0.1,配置此值的方式在配置文件中增長spring.sleuth.sampler.percentage參數配置(若是不配置默認0.1),若是咱們調大此值爲1,能夠看到信息收集就更及時。可是當這樣調整後,咱們會發現咱們的rest接口調用速度比0.1的狀況下慢了不少,即時在0.1的採樣率下,咱們屢次刷新consumer的接口,會發現對同一個請求兩次耗時信息相差很是大,若是取消spring-cloud-sleuth後咱們再測試,會發現並無這種狀況,能夠看到這種方式追蹤服務調用鏈路會給咱們業務程序性能帶來必定的影響。
其實,咱們仔細想一想也能夠總結出這種方式的幾種缺陷
缺陷1:zipkin客戶端向zipkin-server程序發送數據使用的是http的方式通訊,每次發送的時候涉及到鏈接和發送過程。
缺陷2:當咱們的zipkin-server程序關閉或者重啓過程當中,由於客戶端收集信息的發送採用http的方式會被丟失。
針對以上兩個明顯的缺陷,改進的辦法是
一、通訊採用socket或者其餘效率更高的通訊方式。
二、客戶端數據的發送儘可能減小業務線程的時間消耗,採用異步等方式發送收集信息。
三、客戶端與zipkin-server之間增長緩存類的中間件,例如redis、MQ等,在zipkin-server程序掛掉或重啓過程當中,客戶端依舊能夠正常的發送本身收集的信息。
相信採用以上三種方式會很大的提升咱們的效率和可靠性。其實spring-cloud以及爲咱們提供採用MQ或redis等其餘的採用socket方式通訊,利用消息中間件或數據庫緩存的實現方式。下一次咱們再來測試spring-cloud-sleuth-zipkin-stream方式的實現。
https://git.oschina.net/shunyang/spring-cloud-microservice-study.git
https://github.com/shunyang/spring-cloud-microservice-study.git
spring cloud 官方文檔:https://github.com/spring-cloud/spring-cloud-sleuth