纵有疾风起
人生不言弃

.NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪

Tip: 此篇已加入.NET Core微服务基础系列文章索引

=>  Steeltoe目录快速导航

1. 基于Steeltoe使用Spring Cloud Eureka

2. 基于Steeltoe使用Spring Cloud Zuul

3. 基于Steeltoe使用Spring Cloud Hystrix

4. 基于Steeltoe使用Spring Cloud Config

5. 基于Steeltoe使用Zipkin

一、关于Spring Cloud Sleuth与Zipkin

  在 SpringCloud 之中提供的 Sleuth 技术可以实现微服务的调用跟踪,也就是说它可以自动的形成一个调用连接线,通过这个连接线使得开发者可以轻松的找到所有微服务间关系,同时也可以获取微服务所耗费的时间, 这样就可以进行微服务调用状态的监控以及相应的数据分析。

  .NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪插图

  Zipkin是一个分布式追踪系统,它有助于收集解决微服务架构中延迟问题所需的时序数据。它管理这些数据的收集和查找。

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

二、快速构建Zipkin Server

  示例版本:Spring Boot 1.5.15.RELEASE,Spring Cloud Edgware.SR3

  (1)pom.xml 添加相关依赖包

    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <!-- 热启动,热部署依赖包,为了调试方便,加入此包 -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>            <optional>true</optional>        </dependency>        <!-- zipkin -->        <dependency>            <groupId>io.zipkin.java</groupId>            <artifactId>zipkin-autoconfigure-ui</artifactId>        </dependency>        <dependency>            <groupId>io.zipkin.java</groupId>            <artifactId>zipkin-server</artifactId>        </dependency>    </dependencies>    <!-- spring cloud dependencies -->    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>Edgware.SR3</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>

  (2)启动类添加相关注解

@SpringBootApplication@EnableZipkinServerpublic class ZipkinServiceApplication {    public static void main(String[] args) {        SpringApplication.run(ZipkinServiceApplication.class, args);    }}

  (3)配置文件

server:  port: 9411spring:  application:    name: zipkin-service

  最终启动后,访问zipkin主界面:

.NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪插图1

三、ASP.NET Core集成Zipkin

3.1 示例环境准备

  这里仍然基于第一篇的示例进行修改,各个项目的角色如下表所示:

微服务项目名称 项目微服务中的角色
eureka-service   服务发现&注册(Spring Boot)
zuul-service   API网关 (Spring Boot)  
zipkin-service   分布式追踪服务 (Spring Boot)  
agent-service   服务提供者 (ASP.NET Core)
client-service   服务提供者 (ASP.NET Core)
premium-service   服务提供者&服务消费者 (ASP.NET Core)

  所有相关服务(除zipkin-service外)注册到Eureka之后的服务列表:

  .NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪插图2

3.2 想要测试的服务调用链路

  浏览器通过API网关(Zuul)调用Premium-Service的API,在这个API中会调用Client-Service的API,当然,会通过服务发现(Eureka)来获取Client-Service的URL。

3.3 以PremiumService为例添加相关配置

  这里以PremiumService为例,其他几个Service参照下面的步骤依次添加配置即可。

  (1)添加相关NuGet包

PM> Install-Package Steeltoe.Extensions.Logging.DynamicLogger 

PM> Install-Package Steeltoe.Management.ExporterCore     

PM> Install-Package Steeltoe.Management.TracingCore

  (2)Program类添加动态日志Provider

    public class Program    {        ......        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>            WebHost.CreateDefaultBuilder(args)                .UseUrls("http://*:8030")                .UseStartup<Startup>()                .ConfigureLogging((builderContext, loggingBuilder) =>                {                    loggingBuilder.AddConfiguration(builderContext.Configuration.GetSection("Logging"));                    // Add Steeltoe Dynamic Logging Provider                    loggingBuilder.AddDynamicConsole();                });    }

  Steeltoe的日志提供器是对ASP.NET Core自身日志器的进一步封装,其在原始数据基础上增加了如Spring Cloud Sleuth中一样的额外信息。

  (3)Starup启动类中添加相关配置

    public class Startup    {        ......        public void ConfigureServices(IServiceCollection services)        {            ......            // Add Steeltoe Distributed Tracing            services.AddDistributedTracing(Configuration);            // Export traces to Zipkin            services.AddZipkinExporter(Configuration);            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);            // Add Hystrix Metrics to container            services.AddHystrixMetricsStream(Configuration);        }        public void Configure(IApplicationBuilder app, IHostingEnvironment env)        {            ......            app.UseMvc();                        // Start Hystrix metrics stream service            app.UseHystrixMetricsStream();            // Start up trace exporter            app.UseTracingExporter();        }    }

  (4)appSettings添加相关配置 => 主要是zipkin server的相关信息

"management": {    "tracing": {      "alwaysSample": true,      "egressIgnorePattern": "/api/v2/spans|/v2/apps/.*/permissions|/eureka/.*|/oauth/.*",      "exporter": {        "zipkin": {          "endpoint": "http://localhost:9411/api/v2/spans",          "validateCertificates": false        }      }    }  }

四、快速验证测试

  (1)启动Eureka, Zuul, Zipkin以及Premium-Service和Client-Service

  (2)通过Zuul调用API 

  .NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪插图3

  (3)通过Zipkin UI查看Trace

  .NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪插图4

  点击具体的Trace查看Details

  .NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪插图5

  (4)点击“依赖分析”按钮查看依赖图

  .NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪插图6

五、小结

  本文简单地介绍了一下Spring Cloud Seluth与Zipkin,然后通过Java快速地构建了一个Zipkin Server,通过在ASP.NET Core中集成Zipkin并做了一个基本的微服务调用追踪Demo。本示例的Zipkin Server的追踪数据是基于内存,实际中应该集成ELK进行持久化。当然,我们也可以直接通过Zipkin的.NET客户端来做。

示例代码

  GitHub => https://github.com/EdisonChou/Microservice.PoC.Steeltoe/tree/master/src/Chapter4-ServiceTracing

参考资料

Steeltoe官方文档:《Steeltoe Doc

Steeltoe官方示例:https://github.com/SteeltoeOSS/Samples

周立,《Spring Cloud与Docker 微服务架构实战

小不点啊,《SpringCloud系列十二:SpringCloudSleuth(SpringCloudSleuth 简介、SpringCloudSleuth 基本配置、数据采集)

Ken.W,《Steeltoe之Distributed Tracing篇

 

文章转载于:https://www.cnblogs.com/edisonchou/p/dotnet_core_microservice_integrate_with_zipkin.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » .NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录