【微服务】zipkin 链路追踪

核能气质少年 提交于 2020-08-07 06:20:23

1. zipkin基础介绍: 分布式追踪系统

概念:

  • tranceID: 请求全局处理标识
  • spanID: 请求在单个服务流转处理标识
  • ParentID: 子服务中显示父服务ID(spanID),主要用于构建服务调用链路

数据传输到Zipkin方式:

  • http
  • kafka
  • Scribe

Zipkin构成组件:

  • collector: zipkin校验、保存、索引接收的追踪数据
  • storage: zipkin 数据存储(Cassandra、mysql、ES、内存等,支持扩展)
  • search: zipkin 数据检索服务(WEB UI)
  • WEB UI: 数据追踪展示UI,默认没有认证

2. 环境部署

参考官网下载:zipkin quickstart

3. 使用案例

3.1 maven 依赖引入

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>
</dependencies>

3.2 测试服务

服务调用流程:zipkin_service_one -> zipkin_service_two -> zipkin_service_three

zipkin_service_one

ServiceController:
@RestController
public class ServiceController {

    @Autowired
    private FeignService feignService;

    @Value("${spring.application.name}")
    private String application;

    @GetMapping("/hello")
    public String hello() {
        return application;
    }

    @GetMapping("/trace")
    public String trace() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(application).append(" ==> ");
        return stringBuilder.append(feignService.hello()).toString();
    }
}

FeignService:
@FeignClient("zipkin-service-two")
public interface FeignService {

    @GetMapping("/hello")
    String hello();
}

zipkin_service_two

ServiceController:
@RestController
@Slf4j
public class ServiceController {

    @Autowired
    private FeignService feignService;

    @Value("${spring.application.name}")
    private String application;

    @GetMapping("/hello")
    public String hello(HttpServletRequest httpServletRequest) {
        String AUTHORIZATION = "AUTHORIZATION";
        log.info("AUTHORIZATION ==> {}", httpServletRequest.getHeader(AUTHORIZATION));
        StringBuilder stringBuilder = new StringBuilder(application).append(" ==> ");
        return stringBuilder.append(feignService.hello()).toString();
    }

    @GetMapping("/trace")
    public String trace() {
        return feignService.hello();
    }
}

FeignService:
@FeignClient("zipkin-service-three")
public interface FeignService {

    @GetMapping("/hello")
    String hello();
}

zipkin_service_three

ServiceController:
@RestController
@Slf4j
public class ServiceController {

    @Value("${spring.application.name}")
    private String application;

    @GetMapping("/hello")
    public String hello(HttpServletRequest httpServletRequest) {
        String AUTHORIZATION = "AUTHORIZATION";
        log.info("AUTHORIZATION ==> {}", httpServletRequest.getHeader(AUTHORIZATION));
        return application;
    }
}

3.3 效果

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!