码迷,mamicode.com
首页 > Windows程序 > 详细

asp.net core api网关 实时性能监控

时间:2018-08-08 18:18:52      阅读:1223      评论:0      收藏:0      [点我收藏+]

标签:time   service   static   factor   ISE   middle   ONBUILD   pen   builder   

asp.net core api网关 实时性能监控

使用InfluxDB、Grafana

Dockerfile 运行 InfluxDB、Grafana

influxdb:
image: influxdb
  ports:
    - "8086:8086"
    - "8083:8083"
  environment:
    - INFLUXDB_DB=TogetherAppMetricsDB
    - INFLUXDB_ADMIN_ENABLED=true
    - INFLUXDB_ADMIN_USER=admin
    - INFLUXDB_ADMIN_PASSWORD=admin
grafana:
  image: grafana/grafana
  ports:
    - "3000:3000"

配置 Grafana

  1. 浏览器打开 <本地ip>:3000,使用默认账号登录
  2. 添加数据源
    在Configuration中点击Add data source按钮,输入你安装的InfluxDB数据库信息
  3. 点击仪表板设置模板

在API网关中App.Metrics

  1. 安装必要的nuget包
> App.Metrics
> App.Metrics.AspNetCore.Endpoints
> App.Metrics.AspNetCore.Reporting
> App.Metrics.AspNetCore.Tracking
> App.Metrics.Reporting.InfluxDB
  1. ConfigureServices 配置

StartUp.cs

public void ConfigureServices(IServiceCollection services)
{
      ...

      services.AddOptions();
      services.Configure<AppMetricsOptions>(Configuration.GetSection("AppMetrics"));

      services.AddAppMetrics(Configuration);
      services.AddOcelot(Configuration);
}

 public void Configure(ILoggerFactory loggerFactory, IApplicationBuilder app, IHostingEnvironment env)
 {

      app.UseMetricsAllEndpoints();
      app.UseMetricsAllMiddleware();

      app.UseOcelot();
 }

ServiceCollectionExtensions.cs

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddAppMetrics(this IServiceCollection services, IConfiguration configuration)
    {
        var options = services.BuildServiceProvider()
            .GetRequiredService<IOptions<AppMetricsOptions>>()?.Value;
        if (options?.IsOpen == true)
        {
            var uri = new Uri(options.ConnectionString);
            var metrics = AppMetrics.CreateDefaultBuilder().Configuration.Configure(opt =>
            {
                opt.AddAppTag(options.App);
                opt.AddEnvTag(options.Env);
            }).Report.ToInfluxDb(opt =>
            {
                opt.InfluxDb.BaseUri = uri;
                opt.InfluxDb.Database = options.DatabaseName;
                opt.InfluxDb.UserName = options.UserName;
                opt.InfluxDb.Password = options.Password;
                opt.HttpPolicy.BackoffPeriod = TimeSpan.FromSeconds(30);
                opt.HttpPolicy.FailuresBeforeBackoff = 5;
                opt.HttpPolicy.Timeout = TimeSpan.FromSeconds(10);
                opt.FlushInterval = TimeSpan.FromSeconds(5);
            }).Build();

            services.AddMetrics(metrics);
            services.AddMetricsReportScheduler();
            services.AddMetricsTrackingMiddleware();
            services.AddMetricsEndpoints();
        }
        return services;
    }
}

public class AppMetricsOptions
{
    public bool IsOpen { get; set; }
    public string DatabaseName { get; set; }
    public string ConnectionString { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string App { get; set; }
    public string Env { get; set; }
}

最终效果

技术分享图片

参考文章

  1. .NET Core微服务之基于App.Metrics+InfluxDB+Grafana实现统一性能监控
  2. ASP.NET Core Real-time Performance Monitoring
  3. influxdb docker 文档
  4. grafana 官方文档

asp.net core api网关 实时性能监控

标签:time   service   static   factor   ISE   middle   ONBUILD   pen   builder   

原文地址:https://www.cnblogs.com/zengande/p/9444284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!