网关的作用之一,就是有统一的数据出入口,基于这个功能,我们可以在网关上配置监控,从而把所有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
class="code_img_closed" src="/Upload/Images/2017120815/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('e618039f-9db4-44d3-926f-f9e135e6172b',event)" src="/Upload/Images/2017120815/2B1B950FA3DF188F.gif" alt="" />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的监控页面如下: