初學zipkin搭建鏈路追蹤服務注意事項


版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接: https://blog.csdn.net/fsy9595887/article/details/84936214
            </div>
                                                <!--一個博主專欄付費入口-->
                      <!--一個博主專欄付費入口結束-->
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-833878f763.css">
                                    <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-833878f763.css">
            <div class="htmledit_views" id="content_views">
                                        <p><strong>1.zipkinserver的搭建</strong></p>

注意:因為關於 Zipkin 的服務端,在Spring Boot 2.x 版本后,官方就不推薦自行定制編譯了(自行搭建方式在本文末補充),反而是直接提供了編譯好的 jar 包來給我們使用(下載zipkin-server-xxx.jar ,使用JAVA命令啟動該JAR,zipkin-server.jar是一個打包好的springBoot應用,springBoot自帶tomcat因此只要啟動JAR包就可以訪問了。 java -jar zipkin-server-xxx.jar 啟動完后訪問localhost:9411可以查看統計界面)。還有其他兩種方式搭建zipkinserver方式,詳細情況參看

2.構件網關工程以及服務提供工程,這兩個工程作為Zipkin 客戶端, 需要將鏈路數據上傳給Zipkin Server,同時它也作為 EurekaClient。

     a.網關pom如下:


    
    
   
  
  
          
  1. <dependencies>
  2.          <dependency>
  3.               <groupId>org.springframework.cloud </groupId>
  4.               <artifactId>spring-cloud-starter-zipkin </artifactId>
  5.               <version>1.2.1.RELEASE </version>
  6.          </dependency>
  7.          <dependency>
  8.              <groupId>org.springframework.cloud </groupId>
  9.              <artifactId>spring-cloud-starter-zuul </artifactId>
  10.              <version>1.4.4.RELEASE </version>
  11.          </dependency>
  12.          <dependency>
  13.               <groupId>org.springframework.boot </groupId>
  14.               <artifactId>spring-boot-starter-web </artifactId>
  15.          </dependency>
  16.          <dependency>
  17.               <groupId>org.springframework.cloud </groupId>
  18.               <artifactId>spring-cloud-starter-eureka </artifactId>
  19.          </dependency>
  20.          <dependency>
  21.               <groupId>org.springframework.boot </groupId>
  22.               <artifactId>spring-boot-starter-test </artifactId>
  23.               <scope>test </scope>
  24.          </dependency>
  25.      </dependencies>

b.網關的配置文件如下:

====================================

server.port=8768

spring.application.name=zipkinzuulclient

#指定 Zipkin Server 地址

spring.zipkin.base-url=http://localhost:9411

#通過配置這個參數來決定了日志記錄發送給采集器的概率,0-1交給使用者自己配置。開發階段和運行初期,

#一般配置成1全量收集日志,在默認情況下,該值為 0.1

spring.sleuth.sampler.percentage=1.0

#以下兩個配置就可以將以"/sayhi/**"開頭的 Url路由指定的url

zuul.routes.sayhi.path=/sayhi/**

zuul.routes.sayhi.url=http://localhost:1000

===================================

c.服務提供工程的pom如下:


    
    
   
  
  
          
  1. <dependencies>
  2.          <dependency>
  3.               <groupId>org.springframework.cloud </groupId>
  4.               <artifactId>spring-cloud-starter-zipkin </artifactId>
  5.               <version>1.2.1.RELEASE </version>
  6.          </dependency>
  7.          <dependency>
  8.               <groupId>org.springframework.boot </groupId>
  9.               <artifactId>spring-boot-starter-web </artifactId>
  10.          </dependency>
  11.          <dependency>
  12.               <groupId>org.springframework.cloud </groupId>
  13.               <artifactId>spring-cloud-starter-eureka </artifactId>
  14.          </dependency>
  15.          <dependency>
  16.               <groupId>org.springframework.boot </groupId>
  17.               <artifactId>spring-boot-starter-test </artifactId>
  18.               <scope>test </scope>
  19.          </dependency>
  20.      </dependencies>

d.服務提供工程的配置

=====================================

spring.application.name=zipkinclient

server.port=1000

#設置服務注冊中心的URL,本服務要向該服務注冊中心注冊自己

eureka.client.serviceUrl.defaultZone=http://ipa:8761/eureka

#指定 Zipkin Server 地址

spring.zipkin.base-url=http://localhost:9411

#通過配置這個參數來決定了日志記錄發送給采集器的概率,0-1交給使用者自己配置。開發階段和運行初期,

#一般配置成1全量收集日志,在默認情況下,該值 為 0.1

spring.sleuth.sampler.percentage=1.0

============================================

3.完整的項目搭建完畢,依次啟動 eurekaserverzipkinserverzipkinzuulclient zipkinclient 在瀏覽器上訪問http://localhost:8768/sayhi/GetTest/getTest 瀏覽器顯示:I  am from :1000    再訪問 http://localhost:9411 ,即訪問 Zipkin 的展示界面,點擊find traces可以看到對應的鏈路數據,例如請求的調用時間、消耗時間,以及請求調用的鏈路情況。(注意:需要先訪問服務產生鏈路記錄,才能在zipkin界面查到相應的servicenamespanname

 

4.可以在鏈路中添加自定義數據,本案例在 zipkinzuulclient服務中新建一個過濾器,它的類型為 post 類型, order0,開啟攔截。在過濾器的攔截邏輯方法里, 通過 Tracer addTag 方法在本案例中加上了鏈路的操作人,過濾器中部分代碼如下:


    
    
   
  
  
          
  1. @Autowired
  2. Tracer tracer; //首先注入Tracer對象
  3.     @Override
  4.     public Object run() {
  5. //在鏈路中添加鏈路操作人員記錄
  6.        tracer.addTag( "user", "lucus");    
  7. return null;
  8.     }

 

補充:在低版本中也可以自行創建zipkinserver工程,搭建zipkinserver服務,但是官方在2017年6月已經停止使用這種方式,並且要求鏈路追蹤案例中所有案例工程(網關工程、服務提供工程)的搭建都是在低版本中進行,不能使用springboot 2.x,如:

springboot 1.5.9、

spring-cloud.version Dalston.SR1、

spring-cloud-starter-zipkin 1.2.1.RELEASE、

zipkin-server 1.27.0、

zipkin-autoconfigure-ui 1.27.0 等。

a.zipkinserver中引入pom依賴如下:


    
    
   
  
  
          
  1. <dependencies>
  2.          <dependency>
  3.              <groupId>io.zipkin.java </groupId>
  4.              <artifactId>zipkin-server </artifactId>
  5.              <version>1.27.0 </version>
  6.          </dependency>
  7.          <dependency>
  8.              <groupId>io.zipkin.java </groupId>
  9.              <artifactId>zipkin-autoconfigure-ui </artifactId>
  10.              <version>1.27.0 </version>
  11.          </dependency>
  12.          <dependency>
  13.              <groupId>org.springframework.boot </groupId>
  14.              <artifactId>spring-boot-starter </artifactId>
  15.          </dependency>
  16.          <dependency>
  17.              <groupId>org.springframework.boot </groupId>
  18.              <artifactId>spring-boot-starter-web </artifactId>
  19.          </dependency>
  20.          <dependency>
  21.              <groupId>org.springframework.cloud </groupId>
  22.              <artifactId>spring-cloud-starter-eureka </artifactId>
  23.              <version>1.4.6.RELEASE </version>
  24.          </dependency>
  25.          <dependency>
  26.              <groupId>org.springframework.boot </groupId>
  27.              <artifactId>spring-boot-starter-test </artifactId>
  28.              <scope>test </scope>
  29.          </dependency>
  30.      </dependencies>

b.低版本中的zipkinserver的配置文件配置內容如下:

====================

#自定義端口號

server.port=9000

#設置服務注冊中心的URL,本服務要向該服務注冊中心注冊自己

eureka.client.serviceUrl.defaultZone=http://ipa:8761/eureka

spring.application.name=zipkinserver

====================

c.啟動類添加注釋如下:


    
    
   
  
  
          
  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  4. import zipkin.server.EnableZipkinServer;
  5. @EnableEurekaClient
  6. @EnableZipkinServer
  7. @SpringBootApplication
  8. public class ZipkinServerApplication {
  9.      public static void main(String[] args) {
  10.         SpringApplication.run(ZipkinServerApplication.class, args);
  11.     }
  12. }

原文地址:https://blog.csdn.net/fsy9595887/article/details/84936214


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM