日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

引入Jaeger——使用

發布時間:2023/12/4 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 引入Jaeger——使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上一篇定義了兩種使用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" });});//添加OpenTracingservices.AddOpenTracing();//注入Jaegerif (Convert.ToBoolean(Configuration.GetSection("OpenTracing:Enable")?.Value)){var agentHost = Configuration.GetSection("OpenTracing:Agent").GetValue<string>("Host");var agentPort = Configuration.GetSection("OpenTracing:Agent").GetValue<int>("Port");var agentMaxPacketSize = Configuration.GetSection("OpenTracing:Agent").GetValue<int>("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<HomeController> _logger;public HomeController(ILogger<HomeController> logger, IHttpClientFactory clientFactory){_clientFactory = clientFactory;_logger = logger;}[HttpGet]public async Task<string> Get(){_logger.LogInformation("WebAPI01中請求WebAPI02");var result = await GetWebAPI02();return $"WebAPI01請求WebAPI02返回值 :{ result}";}async Task<string> 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結果如下,鏈路清晰

總結

以上是生活随笔為你收集整理的引入Jaeger——使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。