首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring cloud 学习(8) - sleuth & zipkin 调用链跟踪

spring cloud 学习(8) - sleuth & zipkin 调用链跟踪

作者头像
菩提树下的杨过
发布2018-01-18 16:43:41
1.5K0
发布2018-01-18 16:43:41
举报

业务复杂的微服务架构中,往往服务之间的调用关系比较难梳理,一次http请求中,可能涉及到多个服务的调用(eg: service A -> service B -> service C...),如果想分析各服务间的调用关系,以及各服务的响应耗时,找出有性能瓶颈的服务,这时zipkin就派上用场,它是Twitter公司开源的一个tracing系统,官网地址为: http://zipkin.io/ , spring cloud可以跟它无疑集成。

使用步骤:

一、微服务方

1.1 添加依赖jar包

    compile 'org.springframework.cloud:spring-cloud-starter-bus-kafka'
    compile 'org.springframework.cloud:spring-cloud-starter-sleuth'
    compile 'org.springframework.cloud:spring-cloud-sleuth-stream'  

注:为了实现tracing数据埋点与采集的解耦,spring cloud引入了message bus(消息总线)的概念,微服务无需关心tracing系统在哪,长什么样,只要向bus总线上扔消息就行,所以引入了bus-kafka以及sleuth-stream。

1.2 application.yml配置

spring:
  ...
  cloud:
    bus:
      enabled: true
    stream:
      default-binder: kafka
      kafka:
        binder:
          brokers: 10.0.1.2,10.0.1.3,10.0.1.4 //kafaka的服务器集群列表
          zkNodes: 10.0.1.5,10.0.1.6,10.0.1.7 //zk的服务器集群列表
          defaultZkPort: 2181 //zk的端口
          defaultBrokerPort: 9092 //kafka的broker端口
  ...
  sleuth:
    sampler:
      percentage: 0.2 //采样率 0.2为20%  

上面2项配置好就行了,代码不用任何修改,真正的代码零侵入

二、zipkin-server

zipkin从kafka上接收过来数据后,有4种保存方式:in-memory(保存在内存中)、mysql、cassandra、elasticsearch

个人开发调试的话,推荐用in-memory模式,其它环境不要使用!(注:因为随着收集的数据越来越多,都放在内存中 很容易造成OOM)

2.1 mysql 存储

2.1.1 主要jar包依赖

dependencies {
    ... 关键是下面几个
    compile 'org.springframework.cloud:spring-cloud-starter-sleuth'
    compile 'org.springframework.cloud:spring-cloud-sleuth-zipkin-stream'
    compile 'org.springframework.cloud:spring-cloud-starter-bus-kafka'
    compile 'io.zipkin.java:zipkin-server'
    compile 'io.zipkin.java:zipkin-autoconfigure-ui'
    compile 'io.zipkin.java:zipkin-autoconfigure-storage-mysql' #mysql的存储

    ... 下面几个是spring-boot/cloud的常规项
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-security'
    compile 'log4j:log4j:1.2.17' //zipkin的storage jar包,依赖低版本的log4j
    compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.8.2'
    compile 'mysql:mysql-connector-java:6.0.5'   
}

2.1.2 application.yml配置

spring:
  application:
    name: zipkin-server
  datasource: //指定mysql数据源
    schema: classpath:/mysql.sql
    url: jdbc:mysql://192.168.1.2:3306/zipkin?autoReconnect=true&useSSL=false
    username: root
    password: ***
    driver-class-name: com.mysql.cj.jdbc.Driver
    initialize: true
    continue-on-error: true
  sleuth:
    enabled: false
  cloud:
    bus:
      enabled: true
    ...
    stream:
      default-binder: kafka
      kafka:
        binder:
          brokers: ${kafka.brokers}
          zkNodes: ${kafka.zkNodes}
          defaultZkPort: ${kafka.zkPort}
          defaultBrokerPort: ${kafka.brokerPort}

zipkin:
  storage:
    type: mysql //配置成mysql存储

2.1.3 main入口代码

@SpringBootApplication(exclude = { 
        MybatisAutoConfiguration.class,
        RedisAutoConfiguration.class,
        RedisRepositoriesAutoConfiguration.class})
@EnableZipkinStreamServer
public class ZipkinServer {

    public static void main(String[] args) {
        SpringApplication.run(ZipkinServer.class, args);
    }
}

 注:如果你的项目中依赖了redis,mybatis等其它包,可以参考上面的写法,排除掉这些自动配置,否则的话,不用加那一堆exclude。

2.2 cassandra

2.2.1 依赖jar包

注:cassandra和elasticsearch下,可能会遇到zipkin中的dependencies面板无数据,详情见github上的讨论:https://github.com/openzipkin/zipkin-dependencies/issues/22

    compile 'org.springframework.boot:spring-boot-starter-data-cassandra'
    compile('io.zipkin.java:zipkin-autoconfigure-storage-cassandra3:1.29.3') {
        exclude group: "com.datastax.cassandra", module: "cassandra-driver-core"
    }
    compile 'com.datastax.cassandra:cassandra-driver-core:3.1.1'
    compile 'com.datastax.cassandra:cassandra-driver-mapping:3.1.1'

2.2.2 application.yml

spring:
  data:
    cassandra:
      contact-points: localhost
      port: 9042
      keyspace-name: zipkin3
  ...

zipkin:
  storage:
    type: cassandra3

2.3 elasticsearch

2.3.1 依赖jar包

compile 'io.zipkin.dependencies:zipkin-dependencies-elasticsearch:1.7.2'
compile 'io.zipkin.java:zipkin-autoconfigure-storage-elasticsearch-http:1.29.2'

2.3.2 application.yml

zipkin:
  storage:
    type: elasticsearch
    elasticsearch:
      cluster: elasticsearch
      hosts: http://localhost:9200
      index: zipkin
      index-shards: 5
      index-replicas: 1
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档