什么是brave
brave是zipkin官方提供的java版本zipkin-client实现
brave提供的功能
基于brave3.9.0
基于http提供brave源码分析
brave-spancollector-http
提供httpCollector收集器
brave-web-servlet-filter
基于http请求提供过滤器
brave-apache-http-interceptors
基于apache-http-client发起气球提供拦截器
基于springboot启动
step1配置
- 针对collector的SpanCollector
- 针对http请求的filter BraveServletFilter
- 针对数据发送的Brave
- 针对http-client请求的拦截器BraveHttpRequestInterceptor,BraveHttpResponseInterceptor12345678910111213141516171819202122232425262728293031323334353637383940414243@Configurationpublic class ZipkinConfig {//span(一次请求信息或者一次链路调用)信息收集器@Beanpublic SpanCollector spanCollector() {Config config = HttpSpanCollector.Config.builder().compressionEnabled(false)// 默认false,span在transport之前是否会被gzipped.connectTimeout(5000).flushInterval(1).readTimeout(6000).build();return HttpSpanCollector.create("http://localhost:9411", config, new EmptySpanCollectorMetricsHandler());}//作为各调用链路,只需要负责将指定格式的数据发送给zipkin@Beanpublic Brave brave(SpanCollector spanCollector){Builder builder = new Builder("service1");//指定serviceNamebuilder.spanCollector(spanCollector);builder.traceSampler(Sampler.create(1));//采集率return builder.build();}//设置server的(服务端收到请求和服务端完成处理,并将结果发送给客户端)过滤器@Beanpublic BraveServletFilter braveServletFilter(Brave brave) {BraveServletFilter filter = new BraveServletFilter(brave.serverRequestInterceptor(),brave.serverResponseInterceptor(), new DefaultSpanNameProvider());return filter;}//设置client的(发起请求和获取到服务端返回信息)拦截器@Beanpublic CloseableHttpClient okHttpClient(Brave brave){CloseableHttpClient httpclient = HttpClients.custom().addInterceptorFirst(new BraveHttpRequestInterceptor(brave.clientRequestInterceptor(), new DefaultSpanNameProvider())).addInterceptorFirst(new BraveHttpResponseInterceptor(brave.clientResponseInterceptor())).build();return httpclient;}}
基于http发起请求服务端处理
post or get url : http://localhost/service1
相关代码请查看 zipkin简单介绍及环境搭建(一)
流程图
point
基于apache-http发起请求
流程图
如何在代码中添加自己的annotation or binaryAnnotation
直接注入Brave即可 ps(不建议这样做,代码侵入。 zipkin不建议添加大量数据)
ps
- 如果collector要使用kafka直接切换spanController即可,需要server端进行对应配置123456789101112131415client端KafkaSpanCollector.create(KafkaSpanCollector.Config.builder().kafkaProperties(null).build(), new EmptySpanCollectorMetricsHandler());server端需要配置kafka配置final class KafkaZooKeeperSetCondition extends SpringBootCondition {static final String PROPERTY_NAME = "zipkin.collector.kafka.zookeeper";@Overridepublic ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata a) {String kafkaZookeeper = context.getEnvironment().getProperty(PROPERTY_NAME);return kafkaZookeeper == null || kafkaZookeeper.isEmpty() ?ConditionOutcome.noMatch(PROPERTY_NAME + " isn't set") :ConditionOutcome.match();}}