博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
引入Jaeger——使用
阅读量:4035 次
发布时间:2019-05-24

本文共 4414 字,大约阅读时间需要 14 分钟。

上一篇定义了两种使用Jaeger的方式:中间件和action过滤器,下面这个例子定义了两个服务 WebAPI01,请求WebAPI02,采用的是中间件的请求方式。

引入JaegerSharp包(或发布到自己的Nuget库里引用)

WebAPI01的Startup

using JaegerSharp;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Hosting;using Microsoft.OpenApi.Models;using System;namespace WebAPI01{    public class Startup    {        public Startup(IConfiguration configuration)        {            Configuration = configuration;        }        public IConfiguration Configuration { get; }        public void ConfigureServices(IServiceCollection services)        {            //命名客户端            services.AddHttpClient("WebAPI02", client =>            {                client.BaseAddress = new Uri(Configuration.GetSection("DownStreamUrl").Value);            });            services.AddControllers();            services.AddSwaggerGen(c =>            {                c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPI01", Version = "v1" });            });            //添加OpenTracing            services.AddOpenTracing();            //注入Jaeger            if (Convert.ToBoolean(Configuration.GetSection("OpenTracing:Enable")?.Value))            {                var agentHost = Configuration.GetSection("OpenTracing:Agent").GetValue
("Host"); var agentPort = Configuration.GetSection("OpenTracing:Agent").GetValue
("Port"); var agentMaxPacketSize = Configuration.GetSection("OpenTracing:Agent").GetValue
("MaxPacketSize"); services.AddJaegerSharp(agentHost, agentPort, agentMaxPacketSize); } } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI01 v1")); } app.UseHttpsRedirection(); if (Convert.ToBoolean(Configuration.GetSection("OpenTracing:Enable")?.Value)) { app.UseJaegerSharp(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }}

WebAPI01的appsettings.json

{  "Logging": {    "LogLevel": {      "Default": "Information",      "Microsoft": "Warning",      "Microsoft.Hosting.Lifetime": "Information"    }  },  "OpenTracing": {    "Enable": true,    "Agent": {      "Host": "localhost",      "Port": 6831,      "MaxPacketSize": 0    }  },  "DownStreamUrl": "https://localhost:6001"}

调用下游服务 WebAPI02,下游服务 WebAPI02与WebAPI01类似,需要引入JaegerSharp,添加配置文件,指向同一个Jaeger的host,如果WebAPI02有下游API调用,继续配置调用。

using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Logging;using System.Net.Http;using System.Threading.Tasks;namespace WebAPI01.Controllers{    [ApiController]    [Route("[controller]")]    public class HomeController : ControllerBase    {        private readonly IHttpClientFactory _clientFactory;        private readonly ILogger
_logger; public HomeController(ILogger
logger, IHttpClientFactory clientFactory) { _clientFactory = clientFactory; _logger = logger; } [HttpGet] public async Task
Get() { _logger.LogInformation("WebAPI01中请求WebAPI02"); var result = await GetWebAPI02(); return $"WebAPI01请求WebAPI02返回值 :{ result}"; } async Task
GetWebAPI02() { using var client = _clientFactory.CreateClient("WebAPI02"); var request = new HttpRequestMessage(HttpMethod.Get, "/home"); using var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); return result; } else { return "error"; } } }}

下载Jaeger(https://www.jaegertracing.io/download/),我用的是1.21.0的Windows版本,因为我的开发环境是Windows,运行jaeger-all-in-one.exe

,再跑自己的应用,访问完链路后,打开localhost:16686,查看结果如下:

查询WebAPI01结果

点击WebAPI01:HTTP GET 7151a0a结果如下,链路清晰

转载地址:http://mkudi.baihongyu.com/

你可能感兴趣的文章
busybox passwd修改密码
查看>>
wpa_supplicant控制脚本
查看>>
rfkill: WLAN hard blocked
查看>>
gstreamer相关工具集合
查看>>
arm 自动升级脚本
查看>>
RS232 四入四出模块控制代码
查看>>
gstreamer插件之 videotestsrc
查看>>
autoupdate script
查看>>
linux 驱动开发 头文件
查看>>
/etc/resolv.conf
查看>>
container_of()传入结构体中的成员,返回该结构体的首地址
查看>>
linux sfdisk partition
查看>>
ipconfig,ifconfig,iwconfig
查看>>
opensuse12.2 PL2303 minicom
查看>>
电平触发方式和边沿触发的区别
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
移植QT
查看>>
如此调用
查看>>
计算机的发展史
查看>>