当前位置: 首页 > news >正文

【微服务部署】03-健康检查

文章目录

    • 1. 探针集成实现高可用
      • 1.1 LivenessProbe
      • 1.2 ReadinessProbe
      • 1.3 StartupProbe
    • 2. 健康检查看板
      • 2.1 组件包

1. 探针集成实现高可用

  • Liveness
  • Readiness
  • Startup

1.1 LivenessProbe

  • 判断服务是否存活
  • 结束“非存活”状态服务
  • 根据重启策略决定是否重启服务

1.2 ReadinessProbe

  • 判断服务是否“就绪”
  • “就绪”状态的服务可以接收请求
  • 非“就绪”状态的服务将会被从流量负载中摘除

1.3 StartupProbe

  • 检测应用程序是否启动成功
  • 启动探针执行成功后,将不再执行,除非应用重启
  • 当启动探针检测成功后,其它探针才开始工作
  • 适合启动较慢的应用配置
  • Kuberbetes 1.16以后的支持
// startup
internal static bool Ready { get; set; } = true;
internal static bool Live { get; set; } = true;public void ConfigureServices(IServiceCollection services)
{...// 注册健康检查services.AddHealthChecks().AddMySql(Configuration.GetValue<string>("Mysql"), "mysql", tags: new string[] { "mysql", "live", "all" })// tags标签分组.AddRabbitMQ(s =>{var connectionFactory = new RabbitMQ.Client.ConnectionFactory();Configuration.GetSection("RabbitMQ").Bind(connectionFactory);return connectionFactory;}, "rabbitmq", tags: new string[] { "rabbitmq", "live", "all" }).AddCheck("liveChecker", () =>{return Live ? HealthCheckResult.Healthy() : HealthCheckResult.Unhealthy();}, new string[] { "live", "all" }).AddCheck("readyChecker", () =>{return Ready ? HealthCheckResult.Healthy() : HealthCheckResult.Unhealthy();}, new string[] { "ready", "all" });
}// public void Configure(IApplicationBuilder app, IWebHostEnvironment env){...app.UseEndpoints(endpoints =>{endpoints.MapMetrics();endpoints.MapHealthChecks("/live", new HealthCheckOptions { Predicate = registration => registration.Tags.Contains("live") });// 检查标记了live的检查项endpoints.MapHealthChecks("/ready", new HealthCheckOptions { Predicate = registration => registration.Tags.Contains("ready") });endpoints.MapHealthChecks("/hc", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions{ResponseWriter = HealthChecks.UI.Client.UIResponseWriter.WriteHealthCheckUIResponse});// 没有传检查项,检查所有endpoints.MapControllers();endpoints.MapGrpcService<GeekTime.Ordering.API.Grpc.OrderServiceImpl>();});}// Controller调用// 设置Ready的值[HttpGet]public IActionResult SetReady([FromQuery]bool ready){Startup.Ready = ready;if (!ready){Task.Run(async () =>{await Task.Delay(60000);Startup.Ready = true;});}return Content($"{Environment.MachineName} : Ready={Startup.Ready}");}// 设置live
[HttpGet]
public IActionResult SetLive([FromQuery]bool live){Startup.Live = live;return Content($"{Environment.MachineName} : Live={Startup.Live}");}// 程序退出
[HttpGet]
public IActionResult Exit([FromServices]IHostApplicationLifetime application){Task.Run(async () =>{await Task.Delay(3000);application.StopApplication();});return Content($"{Environment.MachineName} : Stopping");}

2. 健康检查看板

AspNetCore.Diagnostics.HealthCheck项目,开源社区项目插件

2.1 组件包

  • 探针检测端:AspNetCore.HealthChecks.UI
  • 应用端输出:AspNetCore.HealthChecks.UI.Client
  • 应用端检查项:AspNetCore.HealthChecks.Xxx
// startup
public void ConfigureServices(IServiceCollection services)
{services.AddMvc();services.AddHealthChecks();services.AddHealthChecksUI();}public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseEndpoints(endpoints =>{endpoints.MapHealthChecks("/live");endpoints.MapHealthChecks("/ready");endpoints.MapHealthChecks("/hc");endpoints.MapMetrics();endpoints.MapHealthChecksUI();// 使得dashboard生效endpoints.MapControllers();});}// 配置文件配置需要检查的项目
"HealthChecksUI": {"HealthChecks": [{"Name": "geektime-ordering-api","Uri": "http://geektime-ordering-api/hc"},{"Name": "geektime-mobile-apiaggregator","Uri": "http://geektime-mobile-apiaggregator/hc"},{"Name": "geektime-mobile-gateway","Uri": "http://geektime-mobile-gateway/hc"}],
http://www.lryc.cn/news/145045.html

相关文章:

  • SQL注入之报错注入
  • 基于大数据+django+mysql的银行信用卡用户的数仓系统
  • 【网络】多路转接——五种IO模型 | select
  • sql顺序倒序查询
  • java和sql生成时间维度数据
  • HUT23级训练赛
  • sm4 加解密算法工具类( Java 版 )
  • Redis项目实战——商户查询缓存
  • 重磅OpenAI发布ChatGPT企业版本
  • # Go学习-Day7
  • uniapp-form表单
  • 漏洞挖掘-利用
  • React钩子函数之useDeferredValue的基本使用
  • lodash常用方法
  • QByteArray与结构体之间相互转换
  • npm如何安装淘宝镜像
  • 从项目中突显技能:在面试中讲述你的编程故事
  • python的观察者模式案例
  • C语言——类型转换
  • jmeter性能测试入门完整版
  • 报错sql_mode=only_full_group_by
  • 伪造 IP 地址的原理和防范措施
  • Linux通过libudev获取挂载路径、监控U盘热拔插事件、U盘文件系统类型
  • 【会议征稿】2023智能通信与网络国际学术会议(ICN 2023)
  • Android投屏总结
  • vue2 组件组成部分,组件通信,进阶语法
  • 信看课堂笔记—LDO和DC-DC电路打PK
  • C++ Day6
  • 分布式系统与微服务的区别是什么?
  • python:用python构建一个物联网平台