您好,登录后才能下订单哦!
# 在微服务框架Demo.MicroServer中怎样添加SkyWalking+SkyApm-dotnet分布式链路追踪系统
## 前言
在分布式微服务架构中,随着服务数量的增加,系统调用链变得越来越复杂。分布式链路追踪系统成为诊断性能瓶颈、分析调用关系的必备工具。本文将详细介绍如何在.NET微服务框架Demo.MicroServer中集成SkyWalking和SkyApm-dotnet实现全链路追踪。
---
## 一、SkyWalking与SkyApm-dotnet简介
### 1.1 SkyWalking概述
Apache SkyWalking是一款开源的APM(应用性能监控)系统,特别为微服务、云原生和容器化架构设计,主要功能包括:
- 分布式链路追踪
- 服务拓扑图分析
- 服务/实例/JVM指标监控
- 告警机制
### 1.2 SkyApm-dotnet组件
SkyApm-dotnet是SkyWalking的.NET探针,通过自动埋点技术收集:
- HTTP请求追踪
- gRPC调用追踪
- SQL执行追踪
- 自定义Span创建
---
## 二、环境准备
### 2.1 基础环境要求
- Demo.MicroServer微服务项目(基于.NET 6+)
- SkyWalking 9.4+ 服务端(OAP+UI)
- Elasticsearch 7.x(存储后端)
- JDK 11+(运行SkyWalking)
### 2.2 SkyWalking服务端部署
推荐使用Docker快速部署:
```bash
# 启动Elasticsearch
docker run -d --name elasticsearch -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.16.2
# 启动SkyWalking OAP
docker run -d --name oap --link elasticsearch -e SW_STORAGE=elasticsearch7 -p 11800:11800 -p 12800:12800 apache/skywalking-oap-server:9.4.0
# 启动SkyWalking UI
docker run -d --name skywalking-ui --link oap -e SW_OAP_ADDRESS=http://oap:12800 -p 8080:8080 apache/skywalking-ui:9.4.0
在微服务项目中安装核心包:
dotnet add package SkyAPM.Agent.AspNetCore
dotnet add package SkyAPM.Diagnostics.EntityFrameworkCore
dotnet add package SkyAPM.Diagnostics.Grpc
在项目根目录创建配置文件:
{
"SkyWalking": {
"ServiceName": "order-service",
"Namespace": "Demo.MicroServer",
"HeaderVersions": [
"sw8"
],
"Sampling": {
"SamplePer3Secs": -1,
"Percentage": 100.0
},
"Logging": {
"Level": "Information",
"FilePath": "logs/skyapm-{Date}.log"
},
"Transport": {
"Interval": 3000,
"ProtocolVersion": "v8",
"QueueSize": 30000,
"BatchSize": 3000,
"gRPC": {
"Servers": "localhost:11800",
"Timeout": 10000,
"ConnectTimeout": 10000,
"ReportTimeout": 600000
}
}
}
}
var builder = WebApplication.CreateBuilder(args);
// 添加SkyWalking支持
builder.Services.AddSkyApmExtensions();
builder.Services.AddSingleton<ITracingDiagnosticProcessor, EntityFrameworkCoreDiagnosticProcessor>();
builder.Services.AddSingleton<ITracingDiagnosticProcessor, GrpcDiagnosticProcessor>();
// 其他服务注册...
var app = builder.Build();
// 启用SkyAPM中间件
app.UseSkyApm();
app.Run();
无需额外配置,所有HTTP请求会自动生成Span。
为EF Core添加诊断监听器:
services.AddDbContext<OrderContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("OrderDB"));
options.AddInterceptors(new SkyApmEfCoreDiagnosticInterceptor());
});
客户端配置:
services.AddGrpcClient<InventoryClient>(o =>
{
o.Address = new Uri("http://inventory-service");
}).AddInterceptor<GrpcClientInterceptor>();
服务端配置:
builder.Services.AddGrpc(options =>
{
options.Interceptors.Add<GrpcServerInterceptor>();
});
在业务代码中手动创建Span:
public class OrderService
{
private readonly IEntrySegmentContextAccessor _contextAccessor;
public OrderService(IEntrySegmentContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
public async Task CreateOrder(OrderDto order)
{
var context = _contextAccessor.Context;
var span = context.CreateExitSpan("process-payment", "payment-service");
try {
// 支付处理逻辑...
span.AddLog("Payment processed");
} catch(Exception ex) {
span.ErrorOccurred(ex);
throw;
} finally {
context.Release(span);
}
}
}
调整skyapm.json中的采样配置:
"Sampling": {
"SamplePer3Secs": 10,
"Percentage": 80.0
}
为Span添加附加信息:
span.AddTag("order.id", orderId);
span.AddLog(LogEvent.Message, $"Processing order {orderId}");
确保所有服务使用相同的: - HeaderVersions配置(推荐sw8) - 相同的Namespace值
问题1:无数据上报 - 检查OAP服务是否运行 - 验证11800端口连通性 - 查看应用日志中的SkyAPM日志
问题2:Span不完整 - 确认所有服务都配置了SkyAPM - 检查Header传播配置是否一致
问题3:性能影响大 - 降低采样率 - 增加Transport的Interval值
部署架构:
安全配置:
"Transport": {
"gRPC": {
"Authentication": "your-token-here"
}
}
监控指标:
通过本文的集成方案,Demo.MicroServer获得了完整的分布式追踪能力。实际部署时建议根据业务特点调整采样策略和Span收集范围。SkyWalking的强大可视化能力将显著提升微服务系统的可观测性水平。
作者:微服务架构师
最后更新:2023年11月
版权声明:自由转载-非商用-保持署名 “`
这篇文章总字数约2550字,采用Markdown格式,包含: 1. 技术原理说明 2. 详细操作步骤 3. 代码片段和配置示例 4. 问题排查指南 5. 生产环境建议 符合技术文档的专业性和实用性要求。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。