Java实战 - SpringBoot整合

一. 添加 Maven 依赖

<!-- zipkin包start -->
<dependency>
    <groupId>io.zipkin.brave</groupId>
    <artifactId>brave-core</artifactId>
    <version>3.10.0</version>
</dependency>
<dependency>
    <groupId>io.zipkin.brave</groupId>
    <artifactId>brave-spancollector-http</artifactId>
    <version>3.10.0</version>
</dependency>
<dependency>
    <groupId>io.zipkin.brave</groupId>
    <artifactId>brave-web-servlet-filter</artifactId>
    <version>3.10.0</version>
</dependency>
<dependency>
    <groupId>io.zipkin.brave</groupId>
    <artifactId>brave-okhttp</artifactId>
    <version>3.10.0</version>
</dependency>
<!-- zipkin包end -->

二. 添加 application.properties 配置

# zipkin 服务名称
zipkin.serviceName=zkDemo
# zipkin 地址
zipkin.url=http://127.0.0.1:9411
# zipkin 链接时间
zipkin.connectTimeout=6000
# zipkin 读取时间
zipkin.readTimeout=6000
# zipkin 每间隔多少秒执行一次Span信息上传
zipkin.flushInterval=1
# zipkin 是否启动压缩
zipkin.compressionEnabled=true

三. 添加 zipkin 的 config 配置

@Data
@Configuration
public class ZipkinConfig {
    /**
     * 服务名称
     */
    @Value("${zipkin.serviceName}")
    private String serviceName;

    /**
     * zipkin地址
     */
    @Value("${zipkin.url}")
    private String url;

    /**
     * 连接时间
     */
    @Value("${zipkin.connectTimeout}")
    private int connectTimeout;

    /**
     * 读取时间
     */
    @Value("${zipkin.readTimeout}")
    private int readTimeout;

    /**
     * 每间隔多少秒执行一次Span信息上传
     */
    @Value("${zipkin.flushInterval}")
    private int flushInterval;

    /**
     * 是否启动压缩
     */
    @Value("${zipkin.compressionEnabled}")
    private boolean compressionEnabled;

    /**
     * @Author zhangbocong
     * @Description span(一次请求信息或者一次链路调用)信息收集器
     * @Date 2021/1/8
     * @Param []
     * @Return com.github.kristofa.brave.SpanCollector SpanCollector 控制器
     */
    @Bean
    public SpanCollector spanCollector() {
        HttpSpanCollector.Config config = HttpSpanCollector.Config.builder()
                // 默认false,span在transport之前是否会被gzipped
                .compressionEnabled(compressionEnabled)
                .connectTimeout(connectTimeout)
                .flushInterval(flushInterval)
                .readTimeout(readTimeout)
                .build();
        return create(url, config, new EmptySpanCollectorMetricsHandler());
    }

    /**
     * @Author zhangbocong
     * @Description 作为各调用链路,只需要负责将指定格式的数据发送给zipkin
     * @Date 2021/1/8
     * @Param [spanCollector]
     * @Return com.github.kristofa.brave.Brave
     */
    @Bean
    public Brave brave(SpanCollector spanCollector) {
        //调用服务的名称
        Builder builder = new Builder(serviceName);
        builder.spanCollector(spanCollector);
        //采集率
        builder.traceSampler(Sampler.ALWAYS_SAMPLE);
        return builder.build();
    }


    /**
     * @Author zhangbocong
     * @Description 设置server的(服务端收到请求和服务端完成处理,并将结果发送给客户端)过滤器
     * @Date 2021/1/8
     * @Param [brave]
     * @Return com.github.kristofa.brave.servlet.BraveServletFilter 过滤器
     */
    @Bean
    public BraveServletFilter braveServletFilter(Brave brave) {
        BraveServletFilter filter = new BraveServletFilter(brave.serverRequestInterceptor(),
                brave.serverResponseInterceptor(), new DefaultSpanNameProvider());
        return filter;
    }

    /**
     * @Author zhangbocong
     * @Description 设置client的(发起请求和获取到服务端返回信息)拦截器
     * @Date 2021/1/8
     * @Param [brave]
     * @Return okhttp3.OkHttpClient 返回请求实例
     */
    @Bean
    public OkHttpClient okHttpClient(Brave brave) {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new BraveOkHttpRequestResponseInterceptor(
                        brave.clientRequestInterceptor(),
                        brave.clientResponseInterceptor(),
                        new DefaultSpanNameProvider())).build();
        return httpClient;
    }
}

四. 测试

  • 使用 postman 对随便什么数据接口进行访问,本例为访问 elasticsearch 某索引下的全部数据

  • 访问本地 ziplin 地址,点击查找全部服务的监控记录


  • 也可以根据服务名等条件进行筛选查找,查找后也查看每条记录的详细信息