Fork me on GitHub

.NetCore 使用Zipkin 分布式服务追踪监控服务性能

参考资料

https://zipkin.io/

https://github.com/openzipkin/zipkin/

https://github.com/openzipkin/zipkin4net

搭建环境

CentOS 7.X

Java 8 SDK

zipkin官网有三种安装方式可以参考

Docker

docker run -d -p 9411:9411 openzipkin/zipkin

Java

如果你已经拥有Java8的环境可以如下安装

curl -sSL https://zipkin.io/quickstart.sh | bash -s
java -jar zipkin.jar

源码安装

git clone https://github.com/openzipkin/zipkin
cd zipkin
./mvnw -DskipTests --also-make -pl zipkin-server clean install
java -jar ./zipkin-server/target/zipkin-server-*exec.jar

安装好之后访问下地址看下Zipkin效果

.NetCore中怎么来使用Zipkin

首先添加如下2个nuget包

为了不使Configure方法太过臃肿,我这里扩展下需要用到的IApplicationBuilder接口

在Configure中注入IApplicationLifetime 、ILoggerFactory两个对象

 

  app.UserZipkinCore(applicationLifetime, loggerFactory);
public static void UserZipkinCore(this IApplicationBuilder app,IApplicationLifetime applicationLifetime,ILoggerFactory loggerFactory)
        {
            applicationLifetime.ApplicationStarted.Register(() =>
            {
                TraceManager.SamplingRate = 1.0f;
                var logger = new TracingLogger(loggerFactory, "zipkin4net");
                var httpsender = new HttpZipkinSender("http://ipaddress:9411", "application/json");
                var tracer = new ZipkinTracer(httpsender, new JSONSpanSerializer());
                TraceManager.RegisterTracer(tracer);
                TraceManager.Start(logger);
            });
            applicationLifetime.ApplicationStopped.Register(() => TraceManager.Stop());
            app.UseTracing("IdentityServer4"); 
        }
SamplingRate:跟踪日志的颗粒度
UseTracing:在Zipkin上新建的服务名称

启动引用程序查看Zipkin中的记录

关于Zipkin的界面功能就不做特别介绍一看都懂

 
 

 

posted @ 2018-06-26 11:34  龙码精神  阅读(694)  评论(2编辑  收藏  举报