1. 程式人生 > >.Net Core 商城微服務項目系列(三):Ocelot網關接入Grafana監控

.Net Core 商城微服務項目系列(三):Ocelot網關接入Grafana監控

dap 商城 fig 異常類 uri 部分 中標 timeout doc

使用網關之後我們面臨的一個問題就是監控,我們需要知道網關的實時狀態,比如當前的請求吞吐量、請求耗費的時間、請求峰值甚至需要知道具體哪個服務的哪個方法花費了多少時間。網關作為請求的中轉點是監控品牌的要塞。

本篇將針對Ocelot添加Metrics+InfluxDB+Grafana架構的監控。

1.下載安裝InfluxDB和Grafana

在使用前需要先下載InfluxDB和Grafana這兩個工具,下載安裝方式大家可以先看下Edison Zhou大神的這篇:https://www.cnblogs.com/edisonchou/p/integrated_performance_monitoring_foundation.html,博主也是從這裏學習的。

2.使用

安裝好後,我們可以通過127.0.0.1:8083訪問InfluxDB的可視化界面:

技術分享圖片

圖中標出來的地方是InfluxDB提供給我們操作語句模板,通過Create Database創建一個名為“MIMetrics”的數據庫,然後在右上角的設置按鈕裏我們可以設置用戶名和密碼,這裏就不截圖了,大家打開看一下就明白了。

在我們的Ocelot項目中通過NuGet引用下面幾個包:

技術分享圖片

然後需要在Startup.cs啟動類中添加如下配置:

public void ConfigureServices(IServiceCollection services)
        {

            
#region 註冊Metrics bool isOpenMetrics = Convert.ToBoolean(Configuration["AppMetrics:IsOpen"]); if (isOpenMetrics) { string database = Configuration["AppMetrics:DatabaseName"]; string connStr = Configuration["AppMetrics:ConnectionString"]; string app = Configuration["AppMetrics:App"]; string env = Configuration["AppMetrics:Env"]; string username = Configuration["AppMetrics:UserName"]; string password = Configuration["AppMetrics:Password"]; var uri = new Uri(connStr); var metrics = AppMetrics.CreateDefaultBuilder().Configuration.Configure(options => { options.AddAppTag(app); options.AddEnvTag(env); }).Report.ToInfluxDb(options => { options.InfluxDb.BaseUri = uri; options.InfluxDb.Database = database; options.InfluxDb.UserName = username; options.InfluxDb.Password = password; options.HttpPolicy.BackoffPeriod = TimeSpan.FromSeconds(30); options.HttpPolicy.FailuresBeforeBackoff = 5; options.HttpPolicy.Timeout = TimeSpan.FromSeconds(10); options.FlushInterval = TimeSpan.FromSeconds(5); }).Build(); services.AddMetrics(metrics); services.AddMetricsReportScheduler(); services.AddMetricsTrackingMiddleware(); services.AddMetricsEndpoints(); services.AddMetricsTrackingMiddleware(options => options.IgnoredHttpStatusCodes = new[] { 404 }); } #endregion
services.AddOcelot(Configuration) .AddConsul(); services.AddMvc(options => { options.Filters.Add<HttpGlobalExceptionFilter>(); //加入全局異常類 }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime, ILoggerFactory loggerFactory) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //loggerFactory.AddConsole(Configuration.GetSection("Logging")); bool isOpenMetrics = Convert.ToBoolean(Configuration["AppMetrics:IsOpen"]); if(isOpenMetrics) { app.UseMetricsAllMiddleware(); app.UseMetricsAllEndpoints(); } app.UseOcelot(); app.UseMvc(); }

藍色部分的代碼是我們這次需要的,通過註冊Metrics中間件的形式使用InfluxDB,然後下面是配置文件中需要添加的:

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "AppMetrics": {
    "IsOpen": true,
    "DatabaseName": "MIMetrics",
    "ConnectionString": "http://127.0.0.1:8086",
    "UserName": "admin",
    "Password": "tangjinghe",
    "App": "Ocelot",
    "Env": "Development"
  }
}

這樣,配置就算完成了,然後重新運行起網關,發送幾個請求後查看http://localhost:3000的Grafana的可視化界面:

技術分享圖片

.Net Core 商城微服務項目系列(三):Ocelot網關接入Grafana監控