Ocelot監控

網關的做用之一,就是有統一的數據出入口,基於這個功能,咱們能夠在網關上配置監控,從而把全部web服務的請求應答基本數據捕獲並展顯出來。
關於web的監控,通常的作法是採集數據並保存,而後經過圖表的方式展現出來,所使用的數據庫通常是時序數據庫Graphite,InfluxDB(https://portal.influxdata.com/downloads),OpenDSDB等,本文使用的是InfluxDB,展現數據通常採用一個圖形化框架,本文用的是Grafana(https://grafana.com/get)
首先按上面連接下載InfluxDB和Grafana
InfluxDB下載後以下圖

關於InfluxDB的操做,有相應的命令,能夠參考官方文檔,這裏不贅述,咱們只在這裏建立一個數據庫MetricsDB便可html

Grafana下載後,在Bin目錄下grafana-server.exe爲啓動程序,啓動便可
在瀏覽器裏輸入http://localhost:3000,用戶名和密碼都是admin(進入後可修改)
添加DataSource

添加Dashboards,能夠使用導入https://grafana.com/dashboards/2125


點擊Import便可進行圖形視圖面板web

咱們使用的是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微服務架構視頻》app

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

相關文章
相關標籤/搜索