Grpc健康检查是通过一个普通的用户rpc调用进行实现,Grpc的健康检查定义了如下的protobuf,这样就能实现所有的Grpc协议健康检查的互通。
Firstly, since it is a GRPC service itself, doing a health check is in the same format as a normal rpc. Secondly, it has rich semantics such as per-service health status. Thirdly, as a GRPC service, it is able reuse all the existing billing, quota infrastructure, etc, and thus the server has full control over the access of the health checking service.
syntax = "proto3"; package grpc.health.v1; message HealthCheckRequest { string service = 1; } message HealthCheckResponse { enum ServingStatus { UNKNOWN = 0; SERVING = 1; NOT_SERVING = 2; SERVICE_UNKNOWN = 3; // Used only by the Watch method. } ServingStatus status = 1; } service Health { rpc Check(HealthCheckRequest) returns (HealthCheckResponse); rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse); }
package main import ( "context" "fmt" "log" ) import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" healthpb "google.golang.org/grpc/health/grpc_health_v1" ) const ( address = "localhost:20000" ) func main() { // Set up a connection to the server conn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { log.Fatalf("did not connect: %v", err) } defer func() { _ = conn.Close() }() checkHealth("org.apache.dubbogo.samples.api.Greeter", conn) } func checkHealth(service string, conn *grpc.ClientConn) { fmt.Printf(">>>>> gRPC-go check %s status", service) req := &healthpb.HealthCheckRequest{ Service: service, } ctx := context.Background() rsp, err := healthpb.NewHealthClient(conn).Check(ctx, req) if err != nil { panic(err) } fmt.Printf("get service status = %+v\n", rsp) }
org.apache.dubbogo.samples.api.Greeter
服务。使用grpc-health-probe检查该服务的健康状态,grpc-health-probe -addr=localhost:20000 -service "org.apache.dubbogo.samples.api.Greeter"