Silky微服务框架在线文档Silky微服务框架在线文档
首页
文档
配置
源码解析
博文
github
gitee
首页
文档
配置
源码解析
博文
github
gitee
  • 简介

    • silky 框架介绍
  • 入门

    • 名词解释
    • 快速开始
    • 脚手架
    • 微服务模块化架构的最佳实践 & 约定
    • 示例
  • 主机与模块

    • 主机
    • 网关
    • 模块
    • 插件
  • 网关与 HTTP

    • Swagger 文档
    • 性能分析(MiniProfiler)
    • 跨域(CORS)
    • 审计日志
  • 服务与 RPC

    • 应用服务和服务条目
    • rpc通信
    • websocket通信
    •  服务注册中心
    • 服务治理
  • 数据与缓存

    • EFCore 数据访问
    • 缓存
    • 分布式锁
  • 安全与认证

    • 身份认证与授权
    • 分布式事务
  • 基础设施

    • 依赖注入
    • 对象到对象的映射
    • 参数验证
    • 链路跟踪
    • 日志(Serilog)
    • 健康检查
    • 消息总线(MassTransit)
    • 单元测试与集成测试

概述

silky 框架通过 Silky.HealthChecks.Rpc 模块提供了对 RPC 服务的健康检查能力。在 Kubernetes、Docker Swarm 等容器编排环境下,健康检查是服务存活与就绪探测的重要手段;在微服务架构中,健康检查也是服务治理(故障摘除、流量调度)的基础。

Silky.HealthChecks.Rpc 集成了 AspNetCore.HealthChecks.UI 组件,提供两种形式的健康检查端点:

  • UI 格式(/healthz 端点):返回标准 HealthChecks.UI JSON 格式,可直接接入 HealthChecks UI 面板
  • API 格式(适用于 RPC 微服务):通过 UseSilkyRpcHealthCheckApi 或 MapSilkyRpcHealthChecks 暴露,返回 silky 自定义的简洁 JSON 格式,适合程序自动判断

安装

<PackageReference Include="Silky.HealthChecks.Rpc" Version="3.9.1" />

RPC 微服务健康检查

注册服务

在微服务的 Startup.cs 中注册健康检查服务:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHealthChecks()
        .AddSilkyRpc();             // 注册 RPC 健康检查

    services.AddSilkyServerHealthCheck();   // 可选:启用服务端主动健康检查上报
}

中间件模式(简单场景)

适合直接暴露健康检查端点,无需路由中间件:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // 返回 HealthChecks.UI JSON 格式(适合接入 UI 面板)
    app.UseSilkyRpcHealthCheck("/silkyrpc/healthz");

    // 或者返回 Silky API JSON 格式(适合程序判断)
    app.UseSilkyRpcHealthCheckApi("/api/silkyrpc/healthz");

    app.UseRouting();
    app.UseEndpoints(endpoints => { endpoints.MapSilkyRpcServices(); });
}

端点路由模式(含认证)

配合 ASP.NET Core 端点路由,支持为健康检查端点配置认证策略:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapSilkyRpcServices();

        // 带认证的健康检查端点
        endpoints.MapSilkyRpcHealthChecks("/api/silkyrpc/health");
    });
}

网关健康检查

注册服务

在网关的 Startup.cs 中注册网关健康检查服务:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHealthChecks()
        .AddSilkyGateway();         // 注册网关健康检查
}

中间件模式

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // 返回 HealthChecks.UI JSON 格式
    app.UseSilkyGatewayHealthCheck("/silkygateway/healthz");

    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapSilkyRpcServices();
    });
}

端点路由模式

app.UseEndpoints(endpoints =>
{
    endpoints.MapSilkyRpcServices();
    endpoints.MapSilkyGatewayHealthChecks("/api/silkygateway/health");
});

完整示例

以下是网关的完整健康检查配置示例:

// 网关 Startup.cs
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSilkyHttpCore()
            .AddSwaggerDocuments()
            .AddRouting();

        services.AddHealthChecks()
            .AddSilkyGateway();     // 注册网关健康检查
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRouting();
        app.UseCorsAccessor();

        // 提供轻量健康探测端点(供 Kubernetes liveness/readiness 使用)
        app.UseSilkyGatewayHealthCheck("/healthz");

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapSilkyRpcServices();
            // 提供带认证的详细健康信息(供运维平台查询)
            endpoints.MapSilkyGatewayHealthChecks("/api/health");
        });
    }
}

Kubernetes 探针配置

将健康检查端点配置为 Kubernetes 的存活探针和就绪探针:

# kubernetes deployment.yaml
livenessProbe:
  httpGet:
    path: /healthz
    port: 80
  initialDelaySeconds: 30
  periodSeconds: 10
  timeoutSeconds: 5
  failureThreshold: 3

readinessProbe:
  httpGet:
    path: /healthz
    port: 80
  initialDelaySeconds: 15
  periodSeconds: 5
  timeoutSeconds: 3
  failureThreshold: 2

健康检查响应格式

UI 格式(/healthz,供 HealthChecks.UI 使用):

{
  "status": "Healthy",
  "totalDuration": "00:00:00.0032541",
  "entries": {
    "silky-rpc": {
      "status": "Healthy",
      "duration": "00:00:00.0028647",
      "description": "RPC 服务正常",
      "tags": []
    }
  }
}

API 格式(/api/healthz,silky 自定义格式):

{
  "status": "Healthy",
  "description": "RPC 服务正常"
}

返回的 HTTP 状态码:

  • 健康:200 OK
  • 降级(Degraded):200 OK
  • 不健康:503 Service Unavailable

集成 HealthChecks UI

安装 AspNetCore.HealthChecks.UI 和 AspNetCore.HealthChecks.UI.InMemory.Storage 后,可以搭建可视化健康检查面板:

services.AddHealthChecksUI(opt =>
{
    opt.SetEvaluationTimeInSeconds(30);     // 每 30 秒检查一次
    opt.AddHealthCheckEndpoint("Gateway", "/healthz");
}).AddInMemoryStorage();
app.UseHealthChecksUI(config =>
{
    config.UIPath = "/health-ui";   // 面板路径:http://host/health-ui
});
编辑当前页
Prev
日志(Serilog)
Next
消息总线(MassTransit)