亲宝软件园·资讯

展开

SpringBoot2.0 ZipKin SpringBoot2.0 ZipKin代码实例

攻城遭雷劈 人气:1

Zipkin是一种分布式跟踪系统。它有助于收集解决微服务架构中延迟问题所需的时序数据。它管理这些数据的收集和查找。Zipkin的设计基于 Google Dapper论文。

应用程序用于向Zipkin报告时间数据。Zipkin用户界面还提供了一个依赖关系图,显示每个应用程序有多少跟踪请求。如果您正在解决延迟问题或错误问题,则可以根据应用程序,跟踪长度,注释或时间戳过滤或排序所有跟踪。选择跟踪后,您可以看到每个跨度所需的总跟踪时间百分比,从而可以识别问题应用程序。

这是翻译过来的原意,自己在这里想如果有个调用链,我们自己该怎么实现。要去质疑任何代码。

官方流程图:最关键的是Transport这个地方,通过几种方式传输给Conllector。如何在这里支持多种协议,有兴趣的可以进去看看源码。


开始示例,在这里通过一个项目调用不同的方法来进行测试。

先下载Zipkin的web UI,通过java -jar zipkin.jar执行


项目结构:


pom.xml

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency>
<!-- zipkin-->
<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-->

application.properties

server.port=9000

##########请求的项目名##########
server.servlet.context-path=/zipkinTest


##########zipKin################
zipkin.serviceName=zipkin-test
zipkin.url=http://localhost:9411
zipkin.connectTimeout=6000
zipkin.readTimeout=6000
zipkin.flushInterval=1
zipkin.compressionEnabled=true

ZipkinProperties.java

package com.cms.zipkin;

import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.EmptySpanCollectorMetricsHandler;
import com.github.kristofa.brave.Sampler;
import com.github.kristofa.brave.SpanCollector;
import com.github.kristofa.brave.http.DefaultSpanNameProvider;
import static com.github.kristofa.brave.Brave.Builder;
import static com.github.kristofa.brave.http.HttpSpanCollector.Config;
import static com.github.kristofa.brave.http.HttpSpanCollector.create;
import com.github.kristofa.brave.okhttp.BraveOkHttpRequestResponseInterceptor;
import com.github.kristofa.brave.servlet.BraveServletFilter;
import lombok.Data;
import okhttp3.OkHttpClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: zjsz-user
 * @description: Zipkin配置
 * @author: Mr.Yang
 * @create: 2018-07-03 21:58
 **/
@Data
@Configuration
@ConfigurationProperties(prefix = ZipkinProperties.ZIPKIN_PREFIX)
public class ZipkinProperties {

  public static final String ZIPKIN_PREFIX = "zipkin";

  /**
   * 服务名称
   */
  private String serviceName;

  /**
   * zipkin地址
   */
  private String url;

  /**
   * 连接时间
   */
  private int connectTimeout;

  /**
   * 读取时间
   */
  private int readTimeout;

  /**
   * 每间隔多少秒执行一次Span信息上传
   */
  private int flushInterval;

  /**
   * 是否启动压缩
   */
  private boolean compressionEnabled;

  /**
   * @Description: span(一次请求信息或者一次链路调用)信息收集器
   * @Param:
   * @return: SpanCollector 控制器
   * @Author: Mr.Yang
   * @Date: 2018/7/3 0002
   */
  @Bean
  public SpanCollector spanCollector() {
    Config config = Config.builder()
        // 默认false,span在transport之前是否会被gzipped
        .compressionEnabled(compressionEnabled)
        .connectTimeout(connectTimeout)
        .flushInterval(flushInterval)
        .readTimeout(readTimeout)
        .build();
    return create(url, config, new EmptySpanCollectorMetricsHandler());
  }

  /**
   * @Description: 作为各调用链路,只需要负责将指定格式的数据发送给zipkin
   * @Param:
   * @return:
   * @Author: Mr.Yang
   * @Date: 2018/7/3 0002
   */
  @Bean
  public Brave brave(SpanCollector spanCollector) {
    //调用服务的名称
    Builder builder = new Builder(serviceName);
    builder.spanCollector(spanCollector);
    //采集率
    builder.traceSampler(Sampler.ALWAYS_SAMPLE);
    return builder.build();
  }


  /**
   * @Description: 设置server的(服务端收到请求和服务端完成处理,并将结果发送给客户端)过滤器
   * @Param:
   * @return: 过滤器
   * @Author: Mr.Yang
   * @Date: 2018/7/3 0002
   */
  @Bean
  public BraveServletFilter braveServletFilter(Brave brave) {
    BraveServletFilter filter = new BraveServletFilter(brave.serverRequestInterceptor(),
        brave.serverResponseInterceptor(), new DefaultSpanNameProvider());
    return filter;
  }

  /**
   * @Description: 设置client的(发起请求和获取到服务端返回信息)拦截器
   * @Param:
   * @return: OkHttpClient 返回请求实例
   * @Author: Mr.Yang
   * @Date: 2018/7/3 0002
   */
  @Bean
  public OkHttpClient okHttpClient(Brave brave) {
    OkHttpClient httpClient = new OkHttpClient.Builder()
        .addInterceptor(new BraveOkHttpRequestResponseInterceptor(
            brave.clientRequestInterceptor(),
            brave.clientResponseInterceptor(),
            new DefaultSpanNameProvider())).build();
    return httpClient;
  }
}

ZipkinBraveController1 

package com.cms.contorller;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: zjsz-user
 * @description: 服务一
 * @author: Mr.Yang
 * @create: 2018-07-03 21:58
 **/
@RestController
@RequestMapping("server1")
public class ZipkinBraveController1 {


  @Autowired
  private OkHttpClient client;

  /**
  * @Description: 第一步调用
  * @Param:
  * @return: 字符串
  * @Author: Mr.Yang
  * @Date: 2018/7/3
  */
  @RequestMapping("/zipkin")
  public String service1() throws Exception {
    Thread.sleep(100);
    Request request = new Request.Builder().url("http://localhost:9000/zipkinTest/server2/zipkin").build();
    Response response = client.newCall(request).execute();
    return response.body().string();
  }

}

ZipkinBraveController2

package com.cms.contorller;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: zjsz-user
 * @description: 服务二
 * @author: Mr.Yang
 * @create: 2018-07-03 21:58
 **/
@RestController
@RequestMapping("server2")
public class ZipkinBraveController2 {

  @Autowired
  private OkHttpClient client;

  /**
   * @Description: 第二步调用
   * @Param:
   * @return: 字符串
   * @Author: Mr.Yang
   * @Date: 2018/7/3
   */
  @RequestMapping("/zipkin")
  public String service1() throws Exception {
    Thread.sleep(100);
    Request request = new Request.Builder().url("http://localhost:9000/zipkinTest/server3/zipkin").build();
    Response response = client.newCall(request).execute();
    return response.body().string();
  }

}

ZipkinBraveController3

package com.cms.contorller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @program: zjsz-user
 * @description: 服务三
 * @author: Mr.Yang
 * @create: 2018-07-03 21:58
 **/
@RestController
@RequestMapping("server3")
public class ZipkinBraveController3 {

  /**
   * @Description: 第三步调用
   * @Param:
   * @return: 字符串
   * @Author: Mr.Yang
   * @Date: 2018/7/3
   */
  @RequestMapping("/zipkin")
  public String service1() throws Exception {
    Thread.sleep(200);

    return "你好,欢迎进入Zipkin的世界";
  }

}

项目启动后,访问http://localhost:9000/zipkinTest/server1/zipkin 就可以看到

你好,欢迎进入Zipkin的世界

我们通过http://localhost:9411/zipkin 查看zipkin的web UI


查看每条调用链的详情


后面还会讲关于zipkin将数据整合到Mysql、Elasticsearch中去。

加载全部内容

相关教程
猜你喜欢
用户评论