介绍
Zipkin是一个分布式跟踪系统,用来收集微服务架构中的时序数据以用于调用链分析。Zipkin提供了In-Memory
、MySql
、Elasticsearch
等存储方式。
安装配置
服务端安装
开发环境中,我们可以安装一个In-Memory
的Zipkin
docker run -d -p 9411:9411 openzipkin/zipkin
安装完成后打开:http://localhost:9411/zipkin/可见:
更多Docker部署方式见:zipkin-docker
.NET下的使用
Install-Package zipkin4net -Version 1.3.0
Install-Package zipkin4net.middleware.aspnetcore -Version 1.3.0
在ServiceA中添加注册代码:
public void RegisterZipkinTrace(IApplicationBuilder app, ILoggerFactory loggerFactory, IHostApplicationLifetime lifetime)
{
lifetime.ApplicationStarted.Register(() =>
{
TraceManager.SamplingRate = 1.0f;
var logger = new TracingLogger(loggerFactory, "zipkin4net");
var httpSender = new HttpZipkinSender("http://localhost:9411", "application/json");
var tracer = new ZipkinTracer(httpSender, new JSONSpanSerializer(), new Statistics());
var consoleTracer = new zipkin4net.Tracers.ConsoleTracer();
TraceManager.RegisterTracer(tracer);
TraceManager.RegisterTracer(consoleTracer);
TraceManager.Start(logger);
});
lifetime.ApplicationStopped.Register(() => TraceManager.Stop());
app.UseTracing("service_a");
}
配置到管道中
public void Configure(IApplicationBuilder app,ILoggerFactory loggerFactory, IHostApplicationLifetime lifetime)
{
RegisterZipkinTrace(app, loggerFactory, lifetime);
}
ServiceB也是同样的操作
我们请求一次获取文章列表接口
查看Zpkin调用链
另外zipkin还为我们生产了依赖关系图
来源:oschina
链接:https://my.oschina.net/u/4256405/blog/3282878