1. 程式人生 > >Spring Cloud(十)Sleuth+ZipKin 實現服務追蹤(續)

Spring Cloud(十)Sleuth+ZipKin 實現服務追蹤(續)

注:本文Spring Cloud 版本 Finchley.SR1 本節是對上一篇的延續,沒有檢視上一篇的請先檢視 Spring Cloud(九)Sleuth+ZipKin 實現服務追蹤 相信細心的網友也發現了,以上的配置方式使用的是 http 傳送鏈路資料,並且儲存在快取中,所以也只能當個 demo,自己玩玩,不適合應用在生產環境中。所以我們的測試環境、生產環境專案需要在之前的基礎上升級。通過上一步的構建我們應該清楚,在生產中使用,我們需要考慮的是鏈路請求資料的傳輸(如何傳送到 zipkin server )以及 zipkin server 將資料的儲存在哪裡。解決了這兩個問題我們就可以放心的在生產環境中使用該功能。

鏈路資料的傳輸及儲存方式: 其實我們檢視 zipkin-server 的依賴就可以知道, zipkin 對於資料傳輸,支援使用訊息佇列 kafka 以及 rabbitmq,資料儲存支援 cassandra、elasticsearch、mysql

接下來我們對之前的專案進行改造,使用kafka來傳輸鏈路資料,ElasticSearch 來儲存資料,當然我們也可以選擇 mysql,但是在使用一段時間後,隨著資料積累,mysql 訪問速度會大大降低,因此推薦使用 ElasticSearch。

升級 zipkin-server

1、升級依賴

各位可以點進 zipkin-server 的pom裡,看看官方都提供了什麼依賴 在這裡,我們需要引入如下依賴:

<!-- 使用 kafka 傳輸鏈路資料-->
<dependency>
     <groupId>io.zipkin.java</groupId>
     <artifactId>zipkin-autoconfigure-collector-kafka</artifactId>
     <version>${zipkin.version}</version>
</dependency>

<!-- 使用 ElasticSearch 儲存資料-->
<dependency
>
<groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-storage-elasticsearch</artifactId> <version>${zipkin.version}</version> </dependency>

2、升級配置檔案

新增如下配置:

zipkin:
    collector:
        kafka:
            bootstrap-servers: ip:port
            groupId: zipkin
            topic: zipkin
    storage:
        StorageComponent: elasticsearch
        elasticsearch:
            cluster: elasticsearch
            hosts: 192.168.0.222:9200
            index: zipkin
            index-replicas: 1
            index-shards: 5
        type: elasticsearch

升級 client

1、POM 依賴

新增 kafka 依賴

<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

2、修改配置檔案

配置鏈路資料傳輸方式為 kafka, 以及 kafka 的配置

spring.zipkin.sender.type=kafka
spring.sleuth.sampler.probability=1.0
spring.kafka.bootstrap-servers=ip:port
spring.zipkin.kafka.topic=zipkin
spring.zipkin.kafka.groupId=zipkin

#刪除之前配置的 spring.zipkin.base-url=http://localhost:9411

測試

1、分別啟動註冊中心、Zuul 閘道器、ZipkinServer、以及上面的 client one 、client two

2、分別請求上面的三個介面產生鏈路資料

完結撒花!!!!