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

posted @ 2018-10-26 08:25  门罗的魔术师  阅读(207)  评论(0编辑  收藏  举报