totalElapsedTime); 35: Log.HealthCheckProcessingEnd(_logger
可以监视内存、磁盘和其他物理处事器资源的使用情况来了解是否处于正常状态。
运行状况查抄可以测试应用的依赖项(如数据库和外部处事终结点)以确认是否可用和正常事情。
运行状况探测可以由容器业务流程协调措施和负载均衡器用于查抄应用的状态。
源码研究在应用中引入HealthCheck,一般需要配置Startup文件,如下所示:
1: public void ConfigureServices(IServiceCollection services) 2: { 3: services.AddHealthChecks(); 4: } 5: 6: public void Configure(IApplicationBuilder app) 7: { 8: app.UseRouting(); 9: 10: app.UseEndpoints(endpoints => 11: { 12: endpoints.MapHealthChecks("/health"); 13: }); 14: }
此中services.AddHealthChecks();会把我们引入到HealthCheckService的扩展要领中,代码如下:
1: public static class HealthCheckServiceCollectionExtensions 2: { 3: public static IHealthChecksBuilder AddHealthChecks(this IServiceCollection services) 4: { 5: services.TryAddSingleton<HealthCheckService, DefaultHealthCheckService>(); 6: services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, HealthCheckPublisherHostedService>()); 7: return new HealthChecksBuilder(services); 8: } 9: }
该扩展要领会测验考试注册一个HealthCheckService的单例东西。HealthCheckService自己是一个抽象类,它内部含有一个抽象要领,主要用于执行健康查抄并返回健康状态的聚合信息。抽象要领如下所示:
1: public abstract Task<HealthReport> CheckHealthAsync( 2: Func<HealthCheckRegistration, bool> predicate, 3: CancellationToken cancellationToken = default);
HealthCheckService有一个默认派生类,就是DefaultHealthCheckService,在其结构要领中,会去验证是否有反复的健康查抄名称存在,如果有,就会抛出异常。此外名称的查抄是不区分巨细写的。该类所实现的抽象要领作为健康查抄的核心成果,内部实现还是对照庞大的。
首先我们看一下该要领的实现源码:
1: public override async Task<HealthReport> CheckHealthAsync( 2: Func<HealthCheckRegistration, bool> predicate, 3: CancellationToken cancellationToken = default) 4: { 5: var registrations = _options.Value.Registrations; 6: if (predicate != null) 7: { 8: registrations = registrations.Where(predicate).ToArray(); 9: } 10: 11: var totalTime = ValueStopwatch.StartNew(); 12: Log.HealthCheckProcessingBegin(_logger); 13: 14: var tasks = new Task<HealthReportEntry>[registrations.Count]; 15: var index = 0; 16: using (var scope = _scopeFactory.CreateScope()) 17: { 18: foreach (var registration in registrations) 19: { 20: tasks[index++] = Task.Run(() => RunCheckAsync(scope, registration, cancellationToken), cancellationToken); 21: } 22: 23: await Task.WhenAll(tasks).ConfigureAwait(false); 24: } 25: 26: index = 0; 27: var entries = new Dictionary<string, HealthReportEntry>(StringComparer.OrdinalIgnoreCase); 28: foreach (var registration in registrations) 29: { 30: entries[registration.Name] = tasks[index++].Result; 31: } 32: 33: var totalElapsedTime = totalTime.GetElapsedTime(); 34: var report = new HealthReport(entries, totalElapsedTime); 35: Log.HealthCheckProcessingEnd(_logger, report.Status, totalElapsedTime); 36: return report; 37: }
1、其内部有对照完善的监控机制,会在内部维护了一个Log成果,全程监控健康查抄的耗时,该日志所记录的健康查抄不只仅是一个健康查抄调集的耗时,而且也记录了每个Name的耗时。
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32239.html