spring-cloud-sleuth+zipkin追蹤服務實現(三)


1.前言

在上一篇spring-cloud-sleuth+zipkin追蹤服務實現(二)中我們講述了利用mq的方式發送數據,存儲在mysql,實際生產過程中調用數據量非常的大,mysql存儲並不是很好的選擇,這時我們可以采用elasticsearch進行存儲。
我們還是使用之前上一節中的三個程序做修改,方便大家看到對比不同點。這里每個項目名都加了一個es,用來表示區別。

2.使用前提

這里選用elasticsearch 2.x版本。
安裝elasticsearch的方法詳見本人的文章elk搭建實戰中elasticsearch部分。

3、microservice-zipkin-stream-server-es

要使用elasticsearch的話,必須在pom.xml中聲明相關的依賴。同時不使用mysql,那么去掉mysql相關的依賴。

全部maven依賴如下:

```
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!--zipkin依賴-->
    <!--此依賴會自動引入spring-cloud-sleuth-stream並且引入zipkin的依賴包-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    </dependency>

    <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-autoconfigure-ui</artifactId>
        <scope>runtime</scope>
    </dependency>


    <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin</artifactId>
        <version>1.24.0</version>
    </dependency>

    <!--保存到數據庫需要如下依賴-->
    <!-- 添加 spring-data-elasticsearch的依賴 -->
    <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>
        <version>1.24.0</version>
        <optional>true</optional>
    </dependency>     
```

由於使用了消息中間件rabbit mq以及elasticsearch,所以我們還需要在配置文件application.properties加入相關的配置:

 server.port=11030
spring.application.name=microservice-zipkin-stream-server-es
#zipkin數據保存到數據庫中需要進行如下配置
#表示當前程序不使用sleuth
spring.sleuth.enabled=false
#表示zipkin數據存儲方式是elasticsearch
zipkin.storage.StorageComponent = elasticsearch
zipkin.storage.type=elasticsearch


zipkin.storage.elasticsearch.cluster=elasticsearch-zipkin-cluster
zipkin.storage.elasticsearch.hosts=127.0.0.1:9300
# zipkin.storage.elasticsearch.pipeline=
zipkin.storage.elasticsearch.max-requests=64
zipkin.storage.elasticsearch.index=zipkin
zipkin.storage.elasticsearch.index-shards=5
zipkin.storage.elasticsearch.index-replicas=1



#rabbitmq配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

為了避免http通信的干擾,我們將原來的監聽端口有11020更改為11030,啟動程序,未報錯且能夠看到rabbit連接日志,說明程序啟動成功。

4.microservice-zipkin-stream-client-es、microservice-zipkin-client-stream-backend-es

與上一節中的代碼保持一致,當然為了以示區別,端口也做了相應的調整

5.測試

按照上一節的方式訪問:http://localhost:11021/call/1,
我們可以server的ui上有相關的數據,同時打開es的管理頁面:http://localhost:9200/_plugin/head/,
點擊數據瀏覽,就可以看到以zipkin開頭,以日期結尾的index,說明數據成功的寫到es了。

6.項目源碼:

https://git.oschina.net/shunyang/spring-cloud-microservice-study.git
https://github.com/shunyang/spring-cloud-microservice-study.git

6.參考文檔:

spring cloud 官方文檔:https://github.com/spring-cloud/spring-cloud-sleuth


免責聲明!

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



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