Ocelot监控
網關的作用之一,就是有統一的數據出入口,基于這個功能,我們可以在網關上配置監控,從而把所有web服務的請求應答基本數據捕獲并展顯出來。
關于web的監控,一般的做法是采集數據并保存,然后通過圖表的方式展示出來,所使用的數據庫一般是時序數據庫Graphite,InfluxDB(https://portal.influxdata.com/downloads),OpenDSDB等,本文使用的是InfluxDB,展示數據一般采用一個圖形化框架,本文用的是Grafana(https://grafana.com/get)
首先按上面鏈接下載InfluxDB和Grafana
InfluxDB下載后如下圖
關于InfluxDB的操作,有相應的命令,可以參考官方文檔,這里不贅述,我們只在這里創建一個數據庫MetricsDB即可
Grafana下載后,在Bin目錄下grafana-server.exe為啟動程序,啟動即可
在瀏覽器里輸入http://localhost:3000,用戶名和密碼都是admin(進入后可修改)
添加DataSource
添加Dashboards,可以使用導入https://grafana.com/dashboards/2125
點擊Import即可進行圖形視圖面板
我們使用的是App.Metrics(https://www.app-metrics.io)的包來實現監控
在OcelotGateway項目中,添加引用下面五個Nuget包
App.Metrics主包
App.Metrics.AspNetCore.Endpoints
App.Metrics.AspNetCore.Reporting
App.Metrics.AspNetCore.Tracking
App.Metrics.Reporting.InfluxDB
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.JWTAuthorizePolicy;
using App.Metrics;
using System;
namespace OcelotGateway
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
#region 注放Metrics?
var metrics = AppMetrics.CreateDefaultBuilder()
.Configuration.Configure(
options =>
{
options.AddAppTag("RepairApp");
options.AddEnvTag("stage");
})
.Report.ToInfluxDb(
options =>
{
options.InfluxDb.BaseUri = new Uri("http://127.0.0.1:8086");
options.InfluxDb.Database = "AppMetricsDemo";
options.InfluxDb.UserName = "admin";
options.InfluxDb.Password = "123456";
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();
#endregion
#region 注放JWT
var audienceConfig = Configuration.GetSection("Audience");
//注入OcelotJwtBearer
services.AddOcelotJwtBearer(audienceConfig["Issuer"], audienceConfig["Issuer"], audienceConfig["Secret"], "GSWBearer");
#endregion
//注入配置文件,AddOcelot要求參數是IConfigurationRoot類型,所以要作個轉換
services.AddOcelot(Configuration as ConfigurationRoot);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
#region Metrics中間件
app.UseMetricsAllMiddleware();
app.UseMetricsAllEndpoints();
#endregion
app.UseOcelot().Wait();
}
}
}
接下來啟動AuthenticationAPI,DemoAAPI,DemoBAPI,OcelotGateway,TestClient,請求幾次后,查看localhost:3000的監控頁面如下:
相關文章:
-
Ocelot——初識基于.Net Core的API網關
-
Ocelot API網關的實現剖析
-
微服務網關Ocelot
-
API網關Ocelot 使用Polly 處理部分失敗問題
-
談談微服務中的 API 網關(API Gateway)
-
Ocelot網關
-
Ocelot統一權限驗證
-
應用監控怎么做?
-
ASP.NET Core之跨平臺的實時性能監控
-
.Net Core 2.0+ InfluxDB+Grafana+App Metrics 實現跨平臺的實時性能監控
-
應用程序的8個關鍵性能指標以及測量方法
-
使用Metrics監控應用程序的性能
-
下一個計劃 : .NET/.NET Core應用性能管理
原文:http://www.cnblogs.com/axzxs2001/p/8005101.html
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結
- 上一篇: 用于.NET Core的ORM
- 下一篇: Visual Studio 2017的第