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

 1 using Microsoft.AspNetCore.Builder;
 2 using Microsoft.AspNetCore.Hosting;
 3 using Microsoft.Extensions.Configuration;
 4 using Microsoft.Extensions.DependencyInjection;
 5 using Ocelot.DependencyInjection;
 6 using Ocelot.Middleware;
 7 using Ocelot.JWTAuthorizePolicy;
 8 using App.Metrics;
 9 using System;
10 
11 namespace OcelotGateway
12 {
13 public class Startup
14 {
15 public Startup(IConfiguration configuration)
16 {
17 Configuration = configuration;
18 }
19 public IConfiguration Configuration { get; }
20 public void ConfigureServices(IServiceCollection services)
21 {
22 #region 注放Metrics 
23 var metrics = AppMetrics.CreateDefaultBuilder()
24 .Configuration.Configure(
25 options =>
26 {
27 options.AddAppTag("RepairApp");
28 options.AddEnvTag("stage");
29 })
30 .Report.ToInfluxDb(
31 options =>
32 {
33 options.InfluxDb.BaseUri = new Uri("http://127.0.0.1:8086");
34 options.InfluxDb.Database = "AppMetricsDemo";
35 options.InfluxDb.UserName = "admin";
36 options.InfluxDb.Password = "123456";
37 options.HttpPolicy.BackoffPeriod = TimeSpan.FromSeconds(30);
38 options.HttpPolicy.FailuresBeforeBackoff = 5;
39 options.HttpPolicy.Timeout = TimeSpan.FromSeconds(10);
40 options.FlushInterval = TimeSpan.FromSeconds(5);
41 })
42 .Build();
43 services.AddMetrics(metrics);
44 services.AddMetricsReportScheduler();
45 services.AddMetricsTrackingMiddleware();
46 services.AddMetricsEndpoints();
47 #endregion
48 
49 #region 注放JWT
50 var audienceConfig = Configuration.GetSection("Audience");
51 //注入OcelotJwtBearer
52 services.AddOcelotJwtBearer(audienceConfig["Issuer"], audienceConfig["Issuer"], audienceConfig["Secret"], "GSWBearer");
53 #endregion
54 //注入配置文件,AddOcelot要求參數是IConfigurationRoot類型,所以要作個轉換
55 services.AddOcelot(Configuration as ConfigurationRoot);
56 }
57 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
58 {
59 #region Metrics中間件
60 app.UseMetricsAllMiddleware();
61 app.UseMetricsAllEndpoints();
62 #endregion
63 app.UseOcelot().Wait();
64 }
65 }
66 }
View Code

接下來啟動AuthenticationAPI,DemoAAPI,DemoBAPI,OcelotGateway,TestClient,請求幾次后,查看localhost:3000的監控頁面如下:

 

《基於.net core微服務架構視頻》

 http://edu.51cto.com/course/13342.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM