分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

.Net Core 商城微服务项目系列(三):Ocelot网关接入Grafana监控

发布时间:2023-09-06 02:19责任编辑:白小东关键词:暂无标签

使用网关之后我们面临的一个问题就是监控,我们需要知道网关的实时状态,比如当前的请求吞吐量、请求耗费的时间、请求峰值甚至需要知道具体哪个服务的哪个方法花费了多少时间。网关作为请求的中转点是监控品牌的要塞。

本篇将针对Ocelot添加Metrics+InfluxDB+Grafana架构的监控。

1.下载安装InfluxDB和Grafana

在使用前需要先下载InfluxDB和Grafana这两个工具,下载安装方式大家可以先看下Edison Zhou大神的这篇:https://www.cnblogs.com/edisonchou/p/integrated_performance_monitoring_foundation.html,博主也是从这里学习的。

2.使用

安装好后,我们可以通过127.0.0.1:8083访问InfluxDB的可视化界面:

图中标出来的地方是InfluxDB提供给我们操作语句模板,通过Create Database创建一个名为“MIMetrics”的数据库,然后在右上角的设置按钮里我们可以设置用户名和密码,这里就不截图了,大家打开看一下就明白了。

在我们的Ocelot项目中通过NuGet引用下面几个包:

然后需要在Startup.cs启动类中添加如下配置:

public void ConfigureServices(IServiceCollection services) ???????{ ???????????#region 注册Metrics ???????????bool isOpenMetrics = Convert.ToBoolean(Configuration["AppMetrics:IsOpen"]); ???????????if (isOpenMetrics) ???????????{ ???????????????string database = Configuration["AppMetrics:DatabaseName"]; ???????????????string connStr = Configuration["AppMetrics:ConnectionString"]; ???????????????string app = Configuration["AppMetrics:App"]; ???????????????string env = Configuration["AppMetrics:Env"]; ???????????????string username = Configuration["AppMetrics:UserName"]; ???????????????string password = Configuration["AppMetrics:Password"]; ???????????????var uri = new Uri(connStr); ???????????????var metrics = AppMetrics.CreateDefaultBuilder().Configuration.Configure(options => ???????????????{ ???????????????????options.AddAppTag(app); ???????????????????options.AddEnvTag(env); ???????????????}).Report.ToInfluxDb(options => ???????????????{ ???????????????????options.InfluxDb.BaseUri = uri; ???????????????????options.InfluxDb.Database = database; ???????????????????options.InfluxDb.UserName = username; ???????????????????options.InfluxDb.Password = password; ???????????????????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(); ???????????????services.AddMetricsTrackingMiddleware(options => options.IgnoredHttpStatusCodes = new[] { 404 }); ???????????} ???????????#endregion ???????????services.AddOcelot(Configuration) ???????????????.AddConsul(); ???????????services.AddMvc(options => ???????????{ ???????????????options.Filters.Add<HttpGlobalExceptionFilter>(); //加入全局异常类 ???????????}); ???????} ???????// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. ???????public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime, ILoggerFactory loggerFactory) ???????{ ???????????if (env.IsDevelopment()) ???????????{ ???????????????app.UseDeveloperExceptionPage(); ???????????} ???????????//loggerFactory.AddConsole(Configuration.GetSection("Logging")); ???????????bool isOpenMetrics = Convert.ToBoolean(Configuration["AppMetrics:IsOpen"]); ???????????if(isOpenMetrics) ???????????{ ???????????????app.UseMetricsAllMiddleware(); ???????????????app.UseMetricsAllEndpoints(); ???????????} ???????????app.UseOcelot(); ???????????app.UseMvc(); ???????}

蓝色部分的代码是我们这次需要的,通过注册Metrics中间件的形式使用InfluxDB,然后下面是配置文件中需要添加的:

{ ?"Logging": { ???"IncludeScopes": false, ???"Debug": { ?????"LogLevel": { ???????"Default": "Warning" ?????} ???}, ???"Console": { ?????"LogLevel": { ???????"Default": "Warning" ?????} ???} ?}, ?"AppMetrics": { ???"IsOpen": true, ???"DatabaseName": "MIMetrics", ???"ConnectionString": "http://127.0.0.1:8086", ???"UserName": "admin", ???"Password": "tangjinghe", ???"App": "Ocelot", ???"Env": "Development" ?}}

这样,配置就算完成了,然后重新运行起网关,发送几个请求后查看http://localhost:3000的Grafana的可视化界面:

.Net Core 商城微服务项目系列(三):Ocelot网关接入Grafana监控

原文地址:https://www.cnblogs.com/weiBlog/p/9861311.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved