一、为什么要用zipkin,它解决了什么问题
1、什么是zipkin
zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),其主要功能是聚集来自各个异构系统的实时监控数据。
它的原理是基于2010年谷歌发表了内部使用的分布式跟踪系统Dapper的论文。讲述了Dapper在谷歌内部两年的演变和设计、运维经验,Twitter也根据该论文开发了自己的分布式跟踪系统zipkin,并将其开源。
其实还有很多的分布式跟踪系统,比如阿里的鹰眼Tracing、Apache的HTrace、京东的Hydra、新浪的Watchman等。
2、zipkin和ELK的区别
ELK提供的是日志的管理,包含了收集、存储、搜索等功能,但是它缺乏实时服务链路跟踪。而zipkin刚好弥补了它这个缺陷。
二、搭建zipkin-server
1、zipkin和springboot版本关联性很大
新建或复制一个maven模块,名称为:mycloud-zipkin-server
2、pom文件添加
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>2.12.9</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.12.9</version>
</dependency>
3、配置文件
spring:
application:
name: zipkin-server
main:
#让应用以非web服务方式启动server.port配置将会失效
web-application-type: none
server:
#官方默认端口9411
port: 9411
compression:
# 是否开启压缩 默认false
enabled: true
4、启动类
package com.example.mycloud.run;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import zipkin2.server.internal.EnableZipkinServer;
@EnableZipkinServer
@SpringBootApplication
@ComponentScan("com.example.mycloud")
public class ZipkinServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinServerApplication.class, args);
}
}
5、问题
启动后端口并不是9411,而是8080
2022-06-19 15:50:03.401 INFO 20768 --- [oss-http-*:8080] com.linecorp.armeria.server.Server : Serving HTTP at /0:0:0:0:0:0:0:0:8080 - http://127.0.0.1:8080/
2022-06-19 15:50:03.401 INFO 20768 --- [ main] c.l.a.spring.ArmeriaAutoConfiguration : Armeria server started at ports: {/0:0:0:0:0:0:0:0:8080=ServerPort(/0:0:0:0:0:0:0:0:8080, [http])}
2022-06-19 15:50:03.558 INFO 20768 --- [ main] c.e.mycloud.run.ZipkinServerApplication : Started ZipkinServerApplication in 5.472 seconds (JVM running for 5.805)
三、客户端修改
1、pom文件添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
2、配置文件添加
spring:
zipkin:
base-url: http://127.0.0.1:8080
四、zipkin原理
[
{
"traceId": "83864d1517b989f5", //全局ID
"parentId": "83864d1517b989f5", //全局ID
"id": "a42c22e446fc2583", //span的id
"kind": "CLIENT",
"name": "get",
"timestamp": 1655626568600214, //span创建时的时间戳,用来记录采集的时刻
"duration": 9248, //span的创建到span完成最终的采集所经历的时间,该时间段可以理解成对于该跟踪埋点来说,服务调用的总耗时
"localEndpoint": {
"serviceName": "zuul",
"ipv4": "192.168.214.1"
},
"tags": {
"http.method": "GET",
"http.path": "/InstanceInfo"
}
},
{
"traceId": "83864d1517b989f5",
"id": "83864d1517b989f5",
"kind": "SERVER",
"name": "get",
"timestamp": 1655626568597043,
"duration": 18548,
"localEndpoint": {
"serviceName": "zuul",
"ipv4": "192.168.214.1"
},
"remoteEndpoint": {
"ipv4": "127.0.0.1",
"port": 62900
},
"tags": {
"http.method": "GET",
"http.path": "/api/sp1/InstanceInfo"
}
},
{
"traceId": "83864d1517b989f5",
"parentId": "83864d1517b989f5",
"id": "a42c22e446fc2583",
"kind": "SERVER",
"name": "get /instanceinfo",
"timestamp": 1655626568604990,
"duration": 3625,
"localEndpoint": {
"serviceName": "service-provider1",
"ipv4": "192.168.214.1"
},
"remoteEndpoint": {
"ipv4": "127.0.0.1"
},
"tags": {
"http.method": "GET",
"http.path": "/InstanceInfo",
"mvc.controller.class": "TestResource",
"mvc.controller.method": "Info"
},
"shared": true
}
]
参考资料:
https://blog.csdn.net/chenglu6516/article/details/100698050
https://blog.csdn.net/setlilei/article/details/102961605
https://zipkin.io/