go grpc-go健康检查如何实现?

How to implement go grpc-go health check?

我正在寻找 grpc-go 健康检查的文档和代码示例。

寻找

中的问题

没有明确的答案我可以重新使用以在我的程序中实施健康检查。

您发布的其中一个 link 有一个 link 读我: https://github.com/grpc/grpc/blob/master/doc/health-checking.md

您现在应该实施该逻辑,并在您的服务集中注册该服务。

自述文件建议手动将服务注册到健康检查服务中,这样您就可以将服务列表作为参数传递来创建健康检查服务。

我建议为您的服务定义一个接口,这样健康检查服务就可以以相同的方式处理所有这些:

type HealthMeter interface {
    GetHealth() mysample.HealthCheckResponse_ServingStatus
    WatchHealth() <- chan mysample.HealthCheckResponse_ServingStatus
}

// implement service "A" and "B" as both interfaces (gRPC server AND HealthMeter)

grpcServer := grpc.NewServer()
srvA := servers.NewServiceA()
srvB := servers.NewServiceB()
healthSrv := servers.NewHealthCheckServer(map[string]servers.HealthMeter{
    "serviceA": srvA,
    "serviceB": srvB,
})

mysample.RegisterServiceAServer(grpcServer, srvA)
mysample.RegisterServiceBServer(grpcServer, srvB)
mysample.RegisterHealthServer(grpcServer, healthSrv)

希望对你有所帮助

我建议您看看这个 Github project 以了解如何构建为生产准备的 gRPC 服务,其中包括运行状况检查等。

具体到健康检查你可以查看它是如何完成的here

图书馆利用这个更专注的 project

如果您不想使用该库,您可以像这样实施健康检查:

import (
    "google.golang.org/grpc/health"
    "google.golang.org/grpc/health/grpc_health_v1"
)
grpcServer := grpc.NewServer()
grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())