SpringCloud 2.x之Spring Cloud 中整合Zipkin進行服務跟蹤zipkin

     上一篇簡介了ZipkinServer的搭建,可是從Spring boot2.x版本後,Zipkin官網已經再也不推薦本身搭建定製Zipkin,而是直接提供了編譯好的jar包。詳情能夠查看官網:web

          https://zipkin.io/pages/quickstart.htmlspring

圖片


        有了Zipkin Server還不能對微服務的調用鏈路進行人禍監控,Zipkin Server能夠被認爲是一個數據處理和展現中心,那它的數據哪裏來呢?須要Zipkin Client做爲代理鏈接到Zipkin Server源源不斷的上送過來。今天講解一下如何在微服務中引入Zipkin Client,而後結合Zipkin Server監控各微服務間的調用鏈路。總體調用鏈路圖以下:apache


涉及的項目:app

      註冊中心:sc-eureka-servermaven

      Zipkinserversc-zipkin-serveride

      微服務:sc-zipkin-client-websc-zipkin-client-servicespring-boot

 

一、        新建項目sc-zipkin-client-service,對應的pom.xml文件以下

<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">


   <modelVersion>4.0.0</modelVersion>



   <groupId>spring-cloud</groupId>

   <artifactId>sc-zipkin-client-service</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <packaging>jar</packaging>



   <name>sc-zipkin-client-service</name>

   <url>http://maven.apache.org</url>

   <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>2.0.4.RELEASE</version>

   </parent>



   <dependencyManagement>

      <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Finchley.RELEASE</version>

           <type>pom</type>

           <scope>import</scope>

        </dependency>



      </dependencies>

   </dependencyManagement>



   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>

      <maven.compiler.target>1.8</maven.compiler.target>

   </properties>



   <dependencies>

      <dependency>

        <groupId>org.springframework.cloud</groupId>

         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

      </dependency>



      <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-zipkin</artifactId>

      </dependency>

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

      </dependency>

   </dependencies>

</project> 

備註:主要引入了spring-cloud-starter-zipkin,說明這是一個zipkin client

 

二、        新建配置文件application.yml

 

eureka:

  client:

    serviceUrl:

      defaultZone:http://localhost:5001/eureka/  

server:

  port: 9201

spring:

  application:

    name: sc-zipkin-client-service

  zipkin:

base-url:http://localhost:9000

`


 

圖片

三、        sc-zipkin-client-service(普通的微服務)項目其餘項目文件以下圖

 

圖片 

四、        新建項目sc-zipkin-client-web,對應的pom.xml文件以下

 

<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">


   <modelVersion>4.0.0</modelVersion>



   <groupId>spring-cloud</groupId>

   <artifactId>sc-zipkin-client-web</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <packaging>jar</packaging>



   <name>sc-zipkin-client-web</name>

   <url>http://maven.apache.org</url>



   <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>2.0.4.RELEASE</version>

   </parent>



   <dependencyManagement>

      <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Finchley.RELEASE</version>

           <type>pom</type>

           <scope>import</scope>

        </dependency>



      </dependencies>

   </dependencyManagement>



   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>

      <maven.compiler.target>1.8</maven.compiler.target>

   </properties>



   <dependencies>

      <dependency>

        <groupId>org.springframework.cloud</groupId>

         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

      </dependency>



      <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-zipkin</artifactId>

      </dependency>



      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

      </dependency>



      <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-openfeign</artifactId>

      </dependency>

   </dependencies>

</project>

`備註:一樣引入了spring-cloud-starter-zipkin,說明是一個zipkin client


五、        新建配置文件application.yml

 

eureka:

  client:

    serviceUrl:

      defaultZone:http://localhost:5001/eureka/



server:

  port: 9202

spring:

  application:

    name: sc-zipkin-client-web

  zipkin:

    base-url: http://localhost:9000

圖片

六、        sc-zipkin-client-web(普通的微服務)項目其餘項目文件以下圖

 

圖片

 

七、        驗證

項目啓動順序:

   sc-eureka-server

sc-zipkin-server

sc-zipkin-client-service

sc-zipkin-client-web

訪問註冊中心:http://127.0.0.1:5001/

服務都已經註冊成功


 

圖片

訪問Zinkin Serverhttp://localhost:9000/zipkin/ 

圖片


目前zipkin server沒有記錄任何的微服務調用鏈路數據。

分別訪問接口:

http://127.0.0.1:9202/user/listUser

圖片


http://127.0.0.1:9202/user/getUser/1

圖片


再次查看Zipkin Server(若是沒有出現能夠多訪問幾回接口,Zipkin須要更多的監控數據)


 

圖片


圖片

源碼:

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-zipkin-client-web
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-zipkin-client-service

圖片

相關文章
相關標籤/搜索