這篇文章咱們解決上篇鏈路跟蹤的遺留問題html
默認狀況下zipkin將收集到的數據存放在內存中(In-Memeroy),可是不可避免帶來了幾個問題:java
一般作法是與mysql或者ElasticSearch結合使用,那麼咱們先把收集到的數據先存到Mysql數據庫中mysql
gradle配置:spring
dependencies { compile('org.springframework.cloud:spring-cloud-starter-eureka') compile('org.springframework.cloud:spring-cloud-starter-config') // compile('io.zipkin.java:zipkin-server') compile 'org.springframework.cloud:spring-cloud-starter-sleuth' compile('io.zipkin.java:zipkin-autoconfigure-ui') runtime('mysql:mysql-connector-java') compile('org.springframework.boot:spring-boot-starter-jdbc') compile('org.springframework.cloud:spring-cloud-sleuth-zipkin-stream') compile('org.springframework.cloud:spring-cloud-stream') compile('org.springframework.cloud:spring-cloud-stream-binder-kafka') }
這裏將原先的 io.zipkin.java:zipkin-server 替換爲 spring-cloud-sleuth-zipkin-stream 該依賴項包含了對mysql存儲的支持,同時添加spring-boot-starter-jdbc與mysql的依賴,順便把kafka的支持也加進來sql
注意:此處腳本最好在數據庫中執行一下,固然咱們也能夠在下面的配置文件中作初始化的相關配置數據庫
spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/myschool?characterEncoding=utf-8&useSSL=false initialize: true continue-on-error: true kafka: bootstrap-servers: localhost:9092 server: port: 9000 zipkin: storage: type: mysql
注意zipkin.storage.type 指定爲mysqlbootstrap
package com.hzgj.lyrk.zipkin.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.sleuth.zipkin.stream.EnableZipkinStreamServer; @EnableZipkinStreamServer @SpringBootApplication public class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); } }
這裏注意將@EnableZipkinServer改爲@EnableZipkinStreamServer異步
這步改造主要用以提升性能與穩定性,服務將收集到的span無腦的往消息中間件上丟就能夠了,不用管zipkin的地址在哪裏。ide
gradle:spring-boot
compile('org.springframework.cloud:spring-cloud-starter-eureka-server') // compile('org.springframework.cloud:spring-cloud-sleuth-zipkin') compile 'org.springframework.cloud:spring-cloud-starter-sleuth' compile 'org.springframework.cloud:spring-cloud-sleuth-stream' compile('org.springframework.cloud:spring-cloud-starter-config') compile('org.springframework.cloud:spring-cloud-stream') compile('org.springframework.cloud:spring-cloud-stream-binder-kafka') compile('org.springframework.kafka:spring-kafka') compile('org.springframework.cloud:spring-cloud-starter-bus-kafka')
這裏把原先的spring-cloud-sleuth-zipkin改爲spring-cloud-sleuth-stream,不用猜裏面必定是基於spring-cloud-stream實現的
server: port: 8100 logging: level: org.springframework.cloud.sleuth: DEBUG spring: sleuth: sampler: percentage: 1.0
注意:這裏設置低採樣率會致使span的丟棄。咱們同時設置sleuth的日誌輸出爲debug
數據庫裏的相關數據: