gRPC 自定义健康检查

gRPC 专栏收录该内容
28 篇文章 0 订阅

gRPC 自定义健康检查

在 gRPC 中自定义健康检查逻辑,用于检查特定的组件(如检查 Redis、MQ 等),或者结合其他的服务组件一起使用(如使用 Spring Boot 的健康检查)

实现

gRPC 的健康检查服务是通过 health.proto定义的

  • health.proto
syntax = "proto3";

package grpc.health.v1;

option csharp_namespace = "Grpc.Health.V1";
option go_package = "google.golang.org/grpc/health/grpc_health_v1";
option java_multiple_files = true;
option java_outer_classname = "HealthProto";
option java_package = "io.grpc.health.v1";

message HealthCheckRequest {
  string service = 1;
}
message HealthCheckResponse {
  enum ServingStatus {
    UNKNOWN = 0;
    SERVING = 1;
    NOT_SERVING = 2;
    SERVICE_UNKNOWN = 3;
  }
  ServingStatus status = 1;
}

service Health {
  // 单次健康检查
  rpc Check(HealthCheckRequest) returns (HealthCheckResponse);

  // 流式健康检查
  rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse);
}

里面定义了两个方法,一个是用于单次检查的 Check方法,一个是用于流式请求的 Watch方法

自定义检查组件

  • CustomHealthCheckImpl.java

自定义健康检查逻辑,通过不同的组件名称返回相应的状态信息

public class CustomHealthCheckImpl extends HealthGrpc.HealthImplBase {
    @Override
    public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
        System.out.println("健康检查:" + request.getService());

        HealthCheckResponse.ServingStatus servingStatus = getServingStatus(request);

        HealthCheckResponse response = HealthCheckResponse.newBuilder()
                                                          .setStatus(servingStatus)
                                                          .build();

        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    @Override
    public void watch(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
        System.out.println("健康检查 Stream:" + request.getService());

        HealthCheckResponse.ServingStatus servingStatus = getServingStatus(request);

        HealthCheckResponse response = HealthCheckResponse.newBuilder()
                                                          .setStatus(servingStatus)
                                                          .build();
        responseObserver.onNext(response);
    }

    private HealthCheckResponse.ServingStatus getServingStatus(HealthCheckRequest request) {
        HealthCheckResponse.ServingStatus servingStatus;
        String service = request.getService();

        switch (service) {
            case "":
                servingStatus = HealthCheckResponse.ServingStatus.SERVING;
                break;
            case "mysql":
                servingStatus = checkMySQL();
                break;
            case "redis":
                servingStatus = checkRedis();
                break;
            default:
                servingStatus = HealthCheckResponse.ServingStatus.UNKNOWN;
                break;
        }
        return servingStatus;
    }
}

使用

在 Server 端添加自定义的健康检查服务

Server server = ServerBuilder.forPort(1234)
                             .addService(new UserInfoServiceImpl()) 
                             .addService(new HelloServiceImpl())
                             .addService(new CustomHealthCheckImpl())
                             .build();

测试

grpc-health-probe -addr localhost:1234 -service mysql
status: SERVING

grpc-health-probe -addr localhost:1234 -service redis
service unhealthy (responded with "NOT_SERVING")

使用 Spring Boot 健康检查

通过 Spring Boot 的 HealthIndicator 作为 gRPC 的健康检查,需要将相应的组件状态转为 gRPC 的健康检查状态

public class CustomHealthCheckImpl extends HealthGrpc.HealthImplBase {

    private final ObjectProvider<HealthIndicator> healthIndicatorObjectProvider;

    public CustomHealthCheckImpl(ObjectProvider<HealthIndicator> healthIndicatorObjectProvider) {
        this.healthIndicatorObjectProvider = healthIndicatorObjectProvider;
    }

    @Override
    public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
        HealthCheckResponse.ServingStatus servingStatus = getServingStatus(request);

        HealthCheckResponse response = HealthCheckResponse.newBuilder()
                                                          .setStatus(servingStatus)
                                                          .build();

        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    @Override
    public void watch(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
        HealthCheckResponse.ServingStatus servingStatus = getServingStatus(request);

        HealthCheckResponse response = HealthCheckResponse.newBuilder()
                                                          .setStatus(servingStatus)
                                                          .build();
        responseObserver.onNext(response);
    }

    private HealthCheckResponse.ServingStatus getServingStatus(HealthCheckRequest request) {
        HealthCheckResponse.ServingStatus servingStatus;
        String service = request.getService();

        servingStatus = healthIndicatorObjectProvider.stream()
                                                     .peek(h -> {
                                                         System.out.println(h.getClass().getSimpleName());
                                                     })
                                                     .filter(h -> h.getClass().getSimpleName().toLowerCase().contains(service.toLowerCase()))
                                                     .map(HealthIndicator::health)
                                                     .map(Health::getStatus)
                                                     .map(this::toGrpcHealthStatus)
                                                     .findFirst()
                                                     .orElse(HealthCheckResponse.ServingStatus.UNKNOWN);

        return servingStatus;
    }

    private HealthCheckResponse.ServingStatus toGrpcHealthStatus(Status status) {
        HealthCheckResponse.ServingStatus servingStatus;

        if (Status.UP.equals(status)) {
            servingStatus = HealthCheckResponse.ServingStatus.SERVING;
        } else if (Status.DOWN.equals(status)) {
            servingStatus = HealthCheckResponse.ServingStatus.NOT_SERVING;
        } else if (Status.OUT_OF_SERVICE.equals(status)) {
            servingStatus = HealthCheckResponse.ServingStatus.SERVICE_UNKNOWN;
        } else {
            servingStatus = HealthCheckResponse.ServingStatus.UNKNOWN;
        }
        return servingStatus;
    }

}
  • 测试
grpc-health-probe -addr localhost:9090 -service disk
status: SERVING

grpc-health-probe -addr localhost:9090 -service redis
service unhealthy (responded with "UNKNOWN")
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页

打赏

呜呜呜啦啦啦

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值