ASP.NET Core整合Zipkin链路跟踪的实现要领(2)

个中-f是指定文件名称,假如是docker-compose.yml则可以直接忽略文件名称,当shell中呈现如下界面

ASP.NET Core整合Zipkin链路跟踪的实现方式

而且在欣赏器中输入:9411/zipkin/呈现如图所示,则说明Zikpin启动乐成

ASP.NET Core整合Zipkin链路跟踪的实现方式

整合ASP.NET Core

ZipKin启动乐成之后,我们就可以将措施中的数据收罗到Zipkin中去了,我新建了两个ASP.NET Core的措施,一个是OrderApi,另一个是ProductApi利便能浮现出挪用链路,个中OrderApi挪用ProductApi接口,在两个项目中别离引入Zipkin依赖包

<PackageReference Include="zipkin4net" Version="1.5.0" /> <PackageReference Include="zipkin4net.middleware.aspnetcore" Version="1.5.0" />

个中zipkin4net为焦点包,zipkin4net.middleware.aspnetcore是集成ASP.NET Core的措施包。然后我们在Startup文件中添加如下要领

public void RegisterZipkinTrace(IApplicationBuilder app, ILoggerFactory loggerFactory, IHostApplicationLifetime lifetime) { lifetime.ApplicationStarted.Register(() => { //记录数据密度,1.0代表全部记录 TraceManager.SamplingRate = 1.0f; //链路日志 var logger = new TracingLogger(loggerFactory, "zipkin4net"); //zipkin处事地点和内容范例 var httpSender = new HttpZipkinSender("http://localhost:9411/", "application/json"); var tracer = new ZipkinTracer(httpSender, new JSONSpanSerializer(), new Statistics()); var consoleTracer = new zipkin4net.Tracers.ConsoleTracer(); TraceManager.RegisterTracer(tracer); TraceManager.RegisterTracer(consoleTracer); TraceManager.Start(logger); }); //措施遏制时遏制链路跟踪 lifetime.ApplicationStopped.Register(() => TraceManager.Stop()); //引入zipkin中间件,用于跟踪处事请求,这边的名字可自界说代表当前处事名称 app.UseTracing(Configuration["nacos:ServiceName"]); }

然后我们在Configure要领中挪用RegisterZipkinTrace要领即可。由于我们要在OrderApi项目中回收HttpClient的方法挪用ProductAPI,默认zipkin4net是支持收罗HttpClient发出请求的链路数据(由于在ProductApi中我们并不发送Http请求,所以可以不消集成一下操纵),详细集成形式如下,假如利用的是HttpClientFactory的方法,在ConfigureServices中设置如下

public void ConfigureServices(IServiceCollection services) { //由于我利用了Nacos作为处事注册中心 services.AddNacosAspNetCore(Configuration); services.AddScoped<NacosDiscoveryDelegatingHandler>(); services.AddHttpClient(ServiceName.ProductService,client=> { client.BaseAddress = new Uri($"http://{ServiceName.ProductService}"); }) .AddHttpMessageHandler<NacosDiscoveryDelegatingHandler>() //引入zipkin trace跟踪httpclient请求,名称设置当前处事名称即可 .AddHttpMessageHandler(provider =>TracingHandler.WithoutInnerHandler(Configuration["nacos:ServiceName"])); services.AddControllers(); }

假如是直接是利用HttpClient的形式挪用则可以回收以下方法

using (HttpClient client = new HttpClient(new TracingHandler("OrderApi"))) { }

然后我们在OrderApi中写一段挪用ProductApi的代码

[Route("orderapi/[controller]")] public class OrderController : ControllerBase { private List<OrderDto> orderDtos = new List<OrderDto>(); private readonly IHttpClientFactory _clientFactory; public OrderController(IHttpClientFactory clientFactory) { orderDtos.Add(new OrderDto { Id = 1, TotalMoney=222,Address="北京市",Addressee="me",From="淘宝",SendAddress="武汉" }); _clientFactory = clientFactory; } /// <summary> /// 获取订单详情接口 /// </summary> /// <param>订单id</param> /// <returns></returns> [HttpGet("getdetails/{id}")] public async Task<OrderDto> GetOrderDetailsAsync(long id) { OrderDto orderDto = orderDtos.FirstOrDefault(i => i.Id == id); if (orderDto != null) { OrderDetailDto orderDetailDto = new OrderDetailDto { Id = orderDto.Id, TotalMoney = orderDto.TotalMoney, Address = orderDto.Address, Addressee = orderDto.Addressee, From = orderDto.From, SendAddress = orderDto.SendAddress }; //挪用ProductApi处事接口 var client = _clientFactory.CreateClient(ServiceName.ProductService); var response = await client.GetAsync($"/productapi/product/getall"); var result = await response.Content.ReadAsStringAsync(); orderDetailDto.Products = JsonConvert.DeserializeObject<List<OrderProductDto>>(result); return orderDetailDto; } return orderDto; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsdffd.html