全鏈路spring cloud sleuth+zipkin
http://blog.csdn.net/qq_15138455/article/details/72956232
版權聲明:@入江之鯨
一、About ZipKin
please google
二、 Demo Scene
三、 Result Display
四、Prepare
1、soft version
kafka:2.10-0.10.2.0zokeeper:3.4.10
elasticsearch:5.2.2
jdk:1.8
spring boot:1.5.3.RELEASE
sprign cloud:Dalston.RELEASE
rabbit mq:3.6.9
2、install
kafka+zookeeperelasticsearch
rabbit mq
mysql
3、create four spring cloud project
web-api、user-api、order-api、zipkinps:
why i will create zipkin project use spring boot by myself not use zipkin.jar from http://zipkin.io/,actually,zipkin.jar is a spring boot project,check it‘s dependency lib you will find it din‘t use spring-cloud-sleuth-stream,but i will send trace info to kafka for zipkin server collector ,so i must use spring-cloud-sleuth-stream in my service
and the message send to kafka is a sleuth.span object and use kafka default serialized,but zipkin.jar only receive zipkin.span and json or thrift encode,so it‘s not matching,That‘s the reason i create zipkin server
but if you use rabbit mq,that‘s no problem.
4、configuration
4.1、the service web-api、user-api、order-api config part like:[html] view plain copy
- <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-kafka</artifactId>
- </dependency>
[html] view plain copy
- spring.sleuth.sampler.percentage=1.0
- spring.cloud.stream.kafka.binder.brokers=10.20.1.11:9092,10.20.1.12:9092
- spring.cloud.stream.kafka.binder.zkNodes=10.20.1.11:2181,10.20.1.12:2181
pom.xml
[html] view plain copy
- <!-- the first one dependency below,In principle, there is no need,beause sleuth-zipkin-stream 1.5.3 will Introduce zipkin version1.19 Automaticly,but 1.19 only support elasticsearch version 2.X -->
- <dependency>
- <groupId>io.zipkin.java</groupId>
- <artifactId>zipkin</artifactId>
- <version>1.24.0</version>
- </dependency>
- <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-kafka</artifactId>
- </dependency>
- <dependency>
- <groupId>io.zipkin.java</groupId>
- <artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>
- <version>1.24.0</version>
- <optional>true</optional>
- </dependency>
[html] view plain copy
- #kafka config
- spring.sleuth.enabled=false
- spring.sleuth.sampler.percentage=1.0
- spring.cloud.stream.kafka.binder.brokers=10.20.1.11:9092,10.20.1.12:9092
- spring.cloud.stream.kafka.binder.zkNodes=10.20.1.11:2181,10.20.1.12:2181
- #elasticsearch config
- zipkin.storage.type=elasticsearch
- zipkin.storage.elasticsearch.hosts=10.20.1.11:9200,10.20.1.12:9200
- zipkin.storage.elasticsearch.cluster=elasticsearch
- zipkin.storage.elasticsearch.index=zipkin
- zipkin.storage.elasticsearch.index-shards=5
- zipkin.storage.elasticsearch.index-replicas=1
[java] view plain copy
- <span style="font-size:14px;">@SpringBootApplication
- //@EnableZipkinServer //this is used by interface receive trace info
- @EnableZipkinStreamServer //can be used kafka,rabbit
- public class ZkingApplication {
- public static void main(String[] args) {
- SpringApplication.run(ZkingApplication.class, args);
- }
- }</span>
五、Demo DownLoad
click me
by the way,spring cloud is a pretty boy,i like its combination of terseness and elegance
六、補充
如果kafka沒有啟動,spring boot會啟動失敗,這個異常處理設計的真是缺德
/**
* 1、修改背景
* 因kafka節點沒有啟動 在spring boot啟動時初始化outputBindingLifecycle、inputBindingLifecycle
* 兩個bean時候連接kafka失敗,向外拋出了異常直到EmbeddedWebApplicationContext類
* 捕獲處理,處理方式為:stopAndReleaseEmbeddedServletContainer()導致整個應用停止啟動
* 2、修改方案
* 幹預上面兩個bean的初始化,在連接kafka異常時,將異常處理掉,不向上層拋出
* 3、修改步驟
* 3.1、使用自定義MyBindingLifecycle的bean將BindingServiceConfiguration中的兩個bean初始化替換掉
* 3.2、在自定bean中啟動線程MyBindingThread來控制兩個bean的初始化
* 4、解決啟動問題之後,實際上kafka還是沒有連接的,此時向kafka發送span時會失敗,默認的處理方案是捕獲到異常之後使用
* handleError處理,再次發送新的span,這就導致循環發送
* 參見:ErrorHandlingTaskExecutor中的execute方法
* catch (Throwable t)
* {
* ErrorHandlingTaskExecutor.this.errorHandler.handleError(t);
* }
* 5、解決方案
* 重寫ErrorHandler的handleError方法
* 6、跟蹤代碼發現
* 跟蹤發現ErrorHandler對線對象是在SourcePollingChannelAdapterFactoryBean初始化時候設置的
* spca.setErrorHandler(this.pollerMetadata.getErrorHandler());
* 進一步發現是在pollerMetadata對象中,所以需要在pollerMetadata對象初始化時候做修改
* 7、修改步驟
* 自定義MyPollerMetadata且需要@Configuration,重寫handleError方法如下
* @author zhangdingxin、yangxi
*/
全鏈路spring cloud sleuth+zipkin