在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
ASP.Net Core 中的 注册健康检查服务要注册 public class Startup { // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddHealthChecks(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseHealthChecks("/health"); app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } } 上图的 其他服务的健康检查除了web的活性检查,还可以检查诸如:SQL Server, MySQL, MongoDB, Redis, RabbitMQ, Elasticsearch, Hangfire, Kafka, Oracle, Azure Storage 等一系列服务应用的活性,每一个服务需要引用相关的 nuget 包即可,如下图所示: 然后在 ConfigureServices 中添加相关服务即可,比如下面代码的 public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddHealthChecks().AddSqlServer("server=.;database=PYZ_L;Trusted_Connection=SSPI"); } 自定义健康检查除了上面的一些开源方案,还可以自定义实现 public class MyCustomHealthCheck : IHealthCheck { public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken)) { bool canConnect = IsDBOnline(); if (canConnect) return HealthCheckResult.Healthy(); return HealthCheckResult.Unhealthy(); } } 这里的 IsDBOnline 方法用来判断当前数据库是否是运行状态,实现代码如下: private bool IsDBOnline() { string connectionString = "server=.;database=PYZ_L;Trusted_Connection=SSPI"; try { using (SqlConnection connection = new SqlConnection(connectionString)) { if (connection.State != System.Data.ConnectionState.Open) connection.Open(); } return true; } catch (System.Exception) { return false; } } 然后在 ConfigureServices 方法中进行注入。 public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddHealthChecks().AddCheck<MyCustomHealthCheck>("sqlcheck"); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting().UseEndpoints(config => { config.MapHealthChecks("/health"); }); app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } 接下来可以浏览下 /health 页面,可以看出该端口自动执行了你的 MyCustomHealthCheck 方法,如下图所示: 可视化健康检查上面的检查策略虽然好,但并没有一个好的可视化方案,要想实现可视化的话,还需要单独下载 Nuget 包: Install-Package AspNetCore.HealthChecks.UI Install-Package AspNetCore.HealthChecks.UI.Client Install-Package AspNetCore.HealthChecks.UI.InMemory.Storage 一旦包安装好之后,就可以在 ConfigureServices 和 Configure 方法下做如下配置。 public class Startup { // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddHealthChecks(); services.AddHealthChecksUI().AddInMemoryStorage(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting().UseEndpoints(config => { config.MapHealthChecks("/health", new HealthCheckOptions { Predicate = _ => true, ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); }); app.UseHealthChecksUI(); app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } } 最后还要在 appsettings.json 中配一下 HealthChecks-UI 中的检查项,如下代码所示: { "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "HealthChecks-UI": { "HealthChecks": [ { "Name": "Local", "Uri": "http://localhost:65348/health" } ], "EvaluationTimeOnSeconds": 10, "MinimumSecondsBetweenFailureNotifications": 60 } } 最后在浏览器中输入 使用 ASP.Net Core 的 译文链接:https://www.infoworld.com/article/3379187/how-to-implement-health-checks-in-aspnet-core.html 到此这篇关于详解如何在ASP.Net Core中实现健康检查的文章就介绍到这了,更多相关ASP.Net Core 健康检查内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论