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

业务复杂的微服务架构中,往往服务之间的调用关系比较难梳理,一次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
时间: 2024-08-19 07:19:12

spring cloud 学习(8) - sleuth & zipkin 调用链跟踪的相关文章

spring cloud 学习(4) - hystrix 服务熔断处理

hystrix 是一个专用于服务熔断处理的开源项目,当依赖的服务方出现故障不可用时,hystrix有一个所谓的断路器,一但打开,就会直接拦截掉对故障服务的调用,从而防止故障进一步扩大(类似中电路中的跳闸,保护家用电器). 使用步骤:(仍然在之前的示例代码上加以改造) 一.添加hystrix依赖 compile 'org.springframework.cloud:spring-cloud-starter-hystrix' 二.在需要熔断的方法上添加注解 package com.cnblogs.y

Spring Cloud 学习——5.使用 feign 的 hystrix 支持

1.前言 hystrix 是一个微服务系统的断路器组件,上文介绍了 spring cloud 通过 netfix hystrix 提供对 hystrix 的支持.同时 spring cloud 也提供了 openfeign 的支持, 而 openfeign 本身就已经内置了 hystrix 支持.所以本文介绍一个使用 openfeign 内置 hystrix 的简单示例. 前文链接: Spring Cloud 学习——3.openfeign实现声明式服务调用 Spring Cloud 学习——4

Spring Cloud学习--配置中心(Config)

Spring Cloud学习--配置中心(Config) 一 Spring Cloud Config简介 二 编写 Config Server 三 编写Config Client 四 使用refresh端点手动刷新配置 五 Spring Config Server与Eurelka配合使用 六 Config Server的高可用 一. Spring Cloud Config简介 微服务要实现集中管理微服务配置.不同环境不同配置.运行期间也可动态调整.配置修改后可以自动更新的需求,Spring Cl

dubbo+zipkin调用链监控(二)

*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } a { color: #4183C4; } a.absent { color: #cc0000; } a.anchor { display: block; padding-left: 30px; margin-left: -30px; cursor: pointer; position: absolute

dubbo+zipkin调用链监控

图片描述(最多50字)收集器抽象 由于zipkin支持http以及kafka两种方式上报数据,所以在配置上需要做下抽象. AbstractZipkinCollectorConfiguration 主要是针对下面两种收集方式的一些配置上的定义,最核心的是Sender接口的定义,http与kafka是两类完全不同的实现. public abstract Sender getSender();其次是协助性的构造函数,主要是配合构建收集器所需要的一些参数. zipkinUrl如果是http收集,那么对应

Spring Cloud 学习——6.zuul实现路由、负载均衡、安全验证

1.前言 在一个大微服务架构的系统中,可能存在着很多服务,如果将这些服务全部对外暴露,会带来很多问题.比如安全问题,有些核心服务直接对外暴露很容易被攻击:比如身份验证问题,有些接口服务是要求登录的,如果各种服务各自对外暴露,那么这些要求登录的请求第一个触达的服务模块都要向“用户服务模块”查询鉴权结果,这样既对“用户服务模块”造成额外压力,也增加了这些其它服务模块的开发成本,所以应该考虑将身份验证的事情交到网关模块中直接完成:比如运维难度和成本问题,如果每一种服务都各自对外暴露,那么整个系统就需要

使用docker-compose 一键部署你的分布式调用链跟踪框架skywalking

原文:使用docker-compose 一键部署你的分布式调用链跟踪框架skywalking 一旦你的程序docker化之后,你会遇到各种问题,比如原来采用的本地记日志的方式就不再方便了,虽然你可以挂载到宿主机,但你使用 --scale 的话,会导致 记录日志异常,所以最好的方式还是要做日志中心化,另一个问题,原来一个请求在一个进程中的痉挛失败,你可以在日志中巡查出调用堆栈,但是docker化之后, 原来一个进程的东西会拆成几个微服务,这时候最好就要有一个分布式的调用链跟踪,类似于wcf中的sv

Spring Cloud学习(一)

Spring Cloud是什么? Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均衡.断路器.数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署.Spring并没有重复制造轮子,它只是将目前各家公司开发的比较成熟.经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂.易部署和易

Spring Cloud学习笔记-012

分布式服务跟踪:Spring Cloud Sleuth 随着业务的发展,系统规模也会变得越来越大,各微服务间的调用关系也变得越来越错综复杂.通常一个由客户端发起的请求在后端系统中会经过多个不同的微服务调用来协同产生最后的请求结果,在复杂的微服务架构系统中,几乎每一个前端请求都会形成一条复杂的分布式服务调用链路,在每条链路中任何一个依赖服务出现延迟高或错误的时候都有可能引起请求最后的失败.这时候,对于每个请求,全链路调用的跟踪就变得越来越重要,通过实现对请求调用的跟踪可以帮助我们快速发现错误根源以