文章出處

業務復雜的微服務架構中,往往服務之間的調用關系比較難梳理,一次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

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()