康健查抄 常用于判定一个应用措施可否对 request 请求举办响应,ASP.Net Core 2.2 中引入了 康健查抄 中间件用于陈诉应用措施的康健状态。
ASP.Net Core 中的 康健查抄 落地做法是袒露一个可设置的 Http 端口,你可以利用 康健查抄 去做一个最简朴的活性检测,好比说:查抄网络和系统的资源可用性,数据库资源是否可用,应用措施依赖的动静中间件可能 Azure cloud service 的可用性 等等,这篇文章我们就来接头如何利用这个 康健查抄中间件。
注册康健检点处事要注册 康健查抄 处事,需要在 Startup.ConfigureServices 下挪用 AddHealthChecks 要领,然后利用 UseHealthChecks 将其注入到 Request Pipeline 管道中,如下代码所示:
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?}"); }); } }
上图的 /health 就是一个可供查抄此 web 是否存活的袒露端口。
其他处事的康健查抄除了web的活性查抄,还可以查抄诸如:SQL Server, MySQL, MongoDB, Redis, RabbitMQ, Elasticsearch, Hangfire, Kafka, Oracle, Azure Storage 等一系列处事应用的活性,每一个处事需要引用相关的 nuget 包即可,如下图所示:
然后在 ConfigureServices 中添加相关处事即可,好比下面代码的 AddSqlServer。
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddHealthChecks().AddSqlServer("server=.;database=PYZ_L;Trusted_Connection=SSPI"); }
自界说康健查抄除了上面的一些开源方案,还可以自界说实现 康健查抄 类,好比自界说方法来检测 数据库 或 外部处事 的可用性,那怎么实现呢? 只需要实现系统内置的 IHealthCheck 接口并实现 CheckHealthAsync() 即可,如下代码所示:
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 包: AspNetCore.HealthChecks.UI , HealthChecks.UI.Client 和 AspNetCore.HealthChecks.UI.InMemory.Storage,呼吁如下:
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 中的查抄项,如下代码所示: