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

文章目录

    • [1. 探针集成实现高可用](#1. 探针集成实现高可用)
      • [1.1 LivenessProbe](#1.1 LivenessProbe)
      • [1.2 ReadinessProbe](#1.2 ReadinessProbe)
      • [1.3 StartupProbe](#1.3 StartupProbe)
    • [2. 健康检查看板](#2. 健康检查看板)
      • [2.1 组件包](#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"
    }
    ],

相关推荐
William_cl2 分钟前
ASP.NET路由长度约束精讲:[HttpGet (“{name:minlength (3)}“)] 字符长度限制吃透,附避坑指南 + 实战代码
后端·asp.net
我命由我1234511 分钟前
Java 泛型 - Java 泛型通配符(上界通配符、下界通配符、无界通配符、PECS 原则)
java·开发语言·后端·java-ee·intellij-idea·idea·intellij idea
szhf7811 分钟前
SpringBoot Test详解
spring boot·后端·log4j
无尽的沉默12 分钟前
SpringBoot整合Redis
spring boot·redis·后端
摸鱼的春哥19 分钟前
春哥的Agent通关秘籍07:5分钟实现文件归类助手【实战】
前端·javascript·后端
瑶山27 分钟前
Spring Cloud微服务搭建五、集成负载均衡,远程调用,熔断降级
spring cloud·微服务·负载均衡·远程调用·熔断降级
Victor35636 分钟前
MongoDB(2)MongoDB与传统关系型数据库的主要区别是什么?
后端
JaguarJack36 分钟前
PHP 应用遭遇 DDoS 攻击时会发生什么 从入门到进阶的防护指南
后端·php·服务端
BingoGo37 分钟前
PHP 应用遭遇 DDoS 攻击时会发生什么 从入门到进阶的防护指南
后端
Victor35638 分钟前
MongoDB(3)什么是文档(Document)?
后端