Steeltoe里的分布式追踪功能与Spring Cloud Sleuth一样,支持在日志中记录追踪数据,或者上传到远端的服务,比如Zipkin。

Logging

在Steeltoe中使用日志时需要引入其特有的日志包Steeltoe.Extensions.Logging.DynamicLogger

之后还需在应用程序启动时加入日志提供器。

WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().ConfigureLogging((builderContext, loggingBuilder) =>
{
loggingBuilder.AddConfiguration(builderContext.Configuration.GetSection("Logging")); // Add Steeltoe Dynamic Logging provider
loggingBuilder.AddDynamicConsole();
});

接下来,引入追踪包Steeltoe.Management.TracingCore

然后在Startup类中加入追踪服务。

public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedTracing(Configuration); services.AddMvc();
}

最后在Action方法里添加日志锚点。

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly ILogger _logger;
public ValuesController(ILogger<ValuesController> logger)
{
_logger = logger;
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
_logger.LogWarning("Hello log");
return new string[] { "value1", "value2" };
}
}

并在appsettings.json文件确认如下配置:

"Logging": {
"LogLevel": {
"Default": "Warning"
}
}

这样启动程序后,可以在输出窗口内看到两条日志,这是因为Steeltoe的日志提供器是对ASP.NET Core自身日志器的进一步封装,其在原始数据基础上增加了如Spring Cloud Sleuth中一样的额外信息。

Exporting

如果想要把追踪数据发送到Zipkin服务中,还需额外引入新的包Steeltoe.Management.ExporterCore

并在Startup类中增加新的服务。

public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedTracing(Configuration);
services.AddZipkinExporter(Configuration); services.AddMvc();
} public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc(); app.UseTracingExporter();
}

appsettings.json文件里加上上文中Zipkin的服务地址。

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

再次启动程序,首先可以看到exportable字段的值已从false变为了true。

然后,再到Zipkin服务中查看,追踪数据确实已经传入到其中。

Steeltoe之Distributed Tracing篇的更多相关文章

  1. Sentry 监控 - Distributed Tracing 分布式跟踪

    系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...

  2. Knowing how all your components work together: distributed tracing with Zipkin

    转自: http://aredko.blogspot.com/2014/02/knowing-how-all-your-components-work.html In today's post we ...

  3. Steeltoe之Circuit Breaker篇

    在分布式系统中,服务发生异常是很正常的现象.为了处理这类"例外",可以采取不同的应对策略,断路器模式即是其中一种方法.这个模式的主要特点是其可以阻断失败的级联影响,不会因为一个服务 ...

  4. Steeltoe之Service Discovery篇

    在前文一窥Spring Cloud Eureka中,已经构建了基于Eureka的服务端与客户端,可用于实现服务注册与发现功能.而借助Steeltoe的类库,可以在.NET生态系统中使用Spring C ...

  5. Steeltoe之Config客户端篇

    Steeltoe是一款开源项目,其目标是选取源自Netflix及其它公司的工具,使它们能够运用于.NET社区.它不仅可以在.NET Core上,也可以在.NET Framework 4.X以上使用.此 ...

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

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 =>  Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...

  7. OneAPM大讲堂 | Metrics, Tracing 和 Logging 的关系

    [编者按]这是在 OpenTracing 和分布式追踪领域内广受欢迎的一片博客文章.在构建监控系统时,大家往往在这几个名词和方式之间纠结. 通过这篇文章,作者很好的阐述了分布式追踪.统计指标与日志之间 ...

  8. Scalable, Distributed Systems Using Akka, Spring Boot, DDD, and Java--转

    原文地址:https://dzone.com/articles/scalable-distributed-systems-using-akka-spring-boot-ddd-and-java Whe ...

  9. Build Telemetry for Distributed Services之OpenCensus:C#

    OpenCensus Easily collect telemetry like metrics and distributed traces from your services OpenCensu ...

随机推荐

  1. 032 Java再次总结

    1.大纲 多线程怎么用的,线程池,事务,传播机制与隔离级别,反射,泛型,数据库引擎的区别,数据库merge,窗口函数,fastJson,JVM调优,GC钩子,Linux的awk,shell,HashM ...

  2. 微软官网tools

    DHCP/AD域插件: 远程管理工具(含DHCP/AD域) 安装网址: https://www.microsoft.com/zh-cn/download/details.aspx?id=7887 程序 ...

  3. java int数组任何数之间间隔不能对于指定数,内付极速排序

    public static void main(String[] args) { int []arr = {300,310, 210,313,334,360,255,233,275,274,410,5 ...

  4. freebsd 时间校准

    修改 /etc/rc.conf ntpdate_enable='YES'ntpd_enable='YES' 如果这里不指定ntpdate_hosts=参数的话,ntpdate会读取/etc/ntp.c ...

  5. LDAP & Implementation

    LDAP & Implementation 一.什么是LDAP? (一)在介绍什么是LDAP之前,我们先来复习一个东西:“什么是目录服务?” 1. 目录服务是一个特殊的数据库,用来保存描述性的 ...

  6. iOS 开发中keyChain的使用

    我们开发中很多数据都是直接存储到本地沙盒中的,这样当应用程序被卸载后,本地的数据都会被删除.如果我们不想让数据在卸载程序的时候丢失,我们可以用KeyChain来存储我们想要的数据.苹果提供了原生的一套 ...

  7. Lua学习----零碎知识点

    Jit(just in time) 动态即时编译,边运行时边编译---->lua (主要是面向进程) Aot(ahead of time) 静态提前编译,运行前编译---->C#(主要是面 ...

  8. Unity进阶----AssetBundle_03(2018/11/07)

    1. 为啥有AB包? 因为资源需要更新, 避免更新一次打包一次 动态修改. 2. AB包注意啥? 依赖关系 找依赖关系应该找到对应的平台!!! 3. 打包策略是分场景打包 若文件被文件夹包含打包出来的 ...

  9. return和throw某些特性相似

    拷贝构造函数的调用拷贝构造函数会在以下三中情况下被调用(1)当类的一个对象去初始化该类的另一个对象时 int main(){ Point a(1,2); Point b(a);//用对象a初始化对象b ...

  10. placeholde属性在IE10以下浏览器上的兼容方案

    首先,判断浏览器是否支持placeholder属性:目前经验来看placeholder属性在     IE10及以上   才能正常显示,而我们实际项目中往往要求兼容到   IE8 var input ...