介绍
Skywalking 是 Apache 基金会下面的一个开源 APM 项目,是一套(APM)分布式追踪系统,提供了很多数据存储列如:Mysql,H2,Elasticsearch7 等。其中APM 全称是应用性能监测软件,主要是用来处理以及追踪分布式系统中的应用程序的性能问题,从而快速找出问题的根源,进而可以让开发者定位问题从而提升应用程序性能。
当我们用很多服务相互调用时,各个服务单调用的顺序\时间性能怎么样?这些问题我们怎么监测,以前的方式是各个系统自己单独做日志,出了问题在一个一个服务的排查,有点刀耕火种的感觉。好在现在有Skywalking链路追踪系统,可以不用写任何代码,就追踪到各个服务间的调用关系和性能状态等。
ASP.NET Core 集成
1、安装Skywalking,搭建好后的Skywalking服务器地址为:127.0.0.1:11800
2、新建项目SkyWalkingA,为项目添加NuGet程序包SkyAPM.Agent.AspNetCore的引用
3、在项目根目录添加skyapm.json文件
{"SkyWalking": {"ServiceName": "WebApplication4","Namespace": "","HeaderVersions": ["sw6"],"Sampling": {"SamplePer3Secs": -1,"Percentage": -1.0},"Logging": {"Level": "Debug","FilePath": "logs/skyapm-{Date}.log"},"Transport": {"Interval": 3000,"ProtocolVersion": "v6","QueueSize": 30000,"BatchSize": 3000,"gRPC": {"Servers": "127.0.0.1:11800","Timeout": 10000,"ConnectTimeout": 10000,"ReportTimeout": 600000}}}
}
4、打开launchSettings.json文件,在其中的环境变量中加入 "SKYWALKING__SERVICENAME"
{"$schema": "http://json.schemastore.org/launchsettings.json","iisSettings": {"windowsAuthentication": false,"anonymousAuthentication": true,"iisExpress": {"applicationUrl": "http://localhost:20467","sslPort": 0}},"profiles": {"IIS Express": {"commandName": "IISExpress","launchBrowser": true,"launchUrl": "api/values","environmentVariables": {"ASPNETCORE_ENVIRONMENT": "Development","SKYWALKING__SERVICENAME": "WebApplication4","ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore"}},"WebApplication4": {"commandName": "Project","launchBrowser": true,"launchUrl": "api/values","applicationUrl": "http://localhost:5000","environmentVariables": {"ASPNETCORE_ENVIRONMENT": "Development","SKYWALKING__SERVICENAME": "WebApplication4","ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore"}}}
}
5、新建项目SkyWalkingB,重复上面步骤2、3、4
在A项目访问B项目
// GET api/values[HttpGet]public async Task<ActionResult<IEnumerable<string>>> Get(){var client = new HttpClient();await client.GetStringAsync("http://localhost:20467/api/values/1");await client.GetStringAsync("http://localhost:20467/api/values");return new string[] { "value1", "value2" };}
跑起来,可以看到对应的链路调用关系
源码地址:
https://github.com/conanl5566/mydemo/tree/master/SkyWalking