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

释放双眼,带上耳机,听听看~!

一、关于Spring Cloud Sleuth与Zipkin

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

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

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

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

二、快速构建Zipkin Server

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

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1    <dependencies>
2        <dependency>
3            <groupId>org.springframework.boot</groupId>
4            <artifactId>spring-boot-starter-web</artifactId>
5        </dependency>
6
7        <!-- 热启动,热部署依赖包,为了调试方便,加入此包 -->
8        <dependency>
9            <groupId>org.springframework.boot</groupId>
10            <artifactId>spring-boot-devtools</artifactId>
11            <optional>true</optional>
12        </dependency>
13
14        <!-- zipkin -->
15        <dependency>
16            <groupId>io.zipkin.java</groupId>
17            <artifactId>zipkin-autoconfigure-ui</artifactId>
18        </dependency>
19        <dependency>
20            <groupId>io.zipkin.java</groupId>
21            <artifactId>zipkin-server</artifactId>
22        </dependency>
23    </dependencies>
24
25    <!-- spring cloud dependencies -->
26    <dependencyManagement>
27        <dependencies>
28            <dependency>
29                <groupId>org.springframework.cloud</groupId>
30                <artifactId>spring-cloud-dependencies</artifactId>
31                <version>Edgware.SR3</version>
32                <type>pom</type>
33                <scope>import</scope>
34            </dependency>
35        </dependencies>
36    </dependencyManagement>
37

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


1
2
3
4
5
6
7
8
1@SpringBootApplication
2@EnableZipkinServer
3public class ZipkinServiceApplication {
4    public static void main(String[] args) {
5        SpringApplication.run(ZipkinServiceApplication.class, args);
6    }
7}
8

(3)配置文件


1
2
3
4
5
6
7
1server:
2  port: 9411
3
4spring:
5  application:
6    name: zipkin-service
7

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

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

三、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之后的服务列表:

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

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1    public class Program
2    {
3        ......
4
5        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
6            WebHost.CreateDefaultBuilder(args)
7                .UseUrls("http://*:8030")
8                .UseStartup<Startup>()
9                .ConfigureLogging((builderContext, loggingBuilder) =>
10                {
11                    loggingBuilder.AddConfiguration(builderContext.Configuration.GetSection("Logging"));
12                    // Add Steeltoe Dynamic Logging Provider
13                    loggingBuilder.AddDynamicConsole();
14                });
15    }
16

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

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1    public class Startup
2    {
3        ......
4
5        public void ConfigureServices(IServiceCollection services)
6        {
7            ......
8            // Add Steeltoe Distributed Tracing
9            services.AddDistributedTracing(Configuration);
10            // Export traces to Zipkin
11            services.AddZipkinExporter(Configuration);
12
13            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
14
15            // Add Hystrix Metrics to container
16            services.AddHystrixMetricsStream(Configuration);
17        }
18
19        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
20        {
21            ......
22
23            app.UseMvc();
24            
25            // Start Hystrix metrics stream service
26            app.UseHystrixMetricsStream();
27            // Start up trace exporter
28            app.UseTracingExporter();
29        }
30    }
31

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


1
2
3
4
5
6
7
8
9
10
11
12
13
1"management": {
2    "tracing": {
3      "alwaysSample": true,
4      "egressIgnorePattern": "/api/v2/spans|/v2/apps/.*/permissions|/eureka/.*|/oauth/.*",
5      "exporter": {
6        "zipkin": {
7          "endpoint": "http://localhost:9411/api/v2/spans",
8          "validateCertificates": false
9        }
10      }
11    }
12  }
13

四、快速验证测试

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

(2)通过Zuul调用API 

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

(3)通过Zipkin UI查看Trace

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

点击具体的Trace查看Details

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

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

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

五、小结

本文简单地介绍了一下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篇》

给TA打赏
共{{data.count}}人
人已打赏
安全经验

图解教程:Google Adsense和百度联…

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索