Variables
var ( HealthCheckResponse_ServingStatus_name = map[int32]string{ 0: "UNKNOWN", 1: "SERVING", 2: "NOT_SERVING", 3: "SERVICE_UNKNOWN", } HealthCheckResponse_ServingStatus_value = map[string]int32{ "UNKNOWN": 0, "SERVING": 1, "NOT_SERVING": 2, "SERVICE_UNKNOWN": 3, })
type HealthServer interface { Check(context.Context, *HealthCheckRequest) (*HealthCheckResponse, error) Watch(*HealthCheckRequest, Health_WatchServer) error}
type HealthClient interface { Check(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (*HealthCheckResponse, error) Watch(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (Health_WatchClient, error)}
type Health_WatchClient interface { Recv() (*HealthCheckResponse, error) grpc.ClientStream}
type Health_WatchServer interface { Send(*HealthCheckResponse) error grpc.ServerStream}
type HealthCheckResponse struct { Status HealthCheckResponse_ServingStatus `protobuf:"varint,1,opt,name=status,proto3,enum=grpc.health.v1.HealthCheckResponse_ServingStatus" json:"status,omitempty"` // contains filtered or unexported fields}func (x *HealthCheckResponse) GetStatus() HealthCheckResponse_ServingStatus
type HealthCheckResponse_ServingStatus int32const ( HealthCheckResponse_UNKNOWN HealthCheckResponse_ServingStatus = 0 HealthCheckResponse_SERVING HealthCheckResponse_ServingStatus = 1 HealthCheckResponse_NOT_SERVING HealthCheckResponse_ServingStatus = 2 HealthCheckResponse_SERVICE_UNKNOWN HealthCheckResponse_ServingStatus = 3 // Used only by the Watch method.)
包函数
func NewHealthClient(cc grpc.ClientConnInterface) HealthClientfunc RegisterHealthServer(s grpc.ServiceRegistrar, srv HealthServer)
举例(自定义cs端)
client
package mainimport ( "context" "fmt" "google.golang.org/grpc" "google.golang.org/grpc/health/grpc_health_v1" "sync")func main() { c := NewClient() rsp, err := c.Check(context.Background(), &grpc_health_v1.HealthCheckRequest{}, nil) fmt.Println(err) fmt.Println(rsp)}var ( mutex sync.Mutex instance *Client)type Client struct { conn *grpc.ClientConn healthClient grpc_health_v1.HealthClient}func NewClient() *Client { if instance == nil { mutex.Lock() defer mutex.Unlock() if instance == nil { instance = &Client{} } } return instance}func (c *Client) getConn() (*grpc.ClientConn, error) { if c.conn == nil { target := fmt.Sprintf("%s:%d", "127.0.0.1", 8000) conn, err := grpc.Dial( target, grpc.WithInsecure(), ) if err != nil { return nil, err } c.conn = conn } return c.conn, nil}func (c *Client) getHealthClient() (grpc_health_v1.HealthClient, error) { if c.healthClient == nil { mutex.Lock() defer mutex.Unlock() if c.healthClient == nil { conn, err := c.getConn() if err != nil { return nil, err } c.healthClient = grpc_health_v1.NewHealthClient(conn) } } return c.healthClient, nil}func (c *Client) Check(ctx context.Context, req *grpc_health_v1.HealthCheckRequest, opts ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error) { client, err := c.getHealthClient() if err != nil { return nil, err } fmt.Println("check") return client.Check(ctx, req)}func (c *Client) Watch(ctx context.Context, req *grpc_health_v1.HealthCheckRequest, opts ...grpc.CallOption) (grpc_health_v1.Health_WatchClient, error) { client, err := c.getHealthClient() if err != nil { return nil, err } return client.Watch(ctx, req)}
service
package mainimport ( "context" "google.golang.org/grpc" "google.golang.org/grpc/grpclog" "google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/grpc/reflection" "log" "net" "os")func main() { grpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stdout, os.Stdout, os.Stdout)) lis, err := net.Listen("tcp", ":8000") if err != nil { log.Fatalf("failed to listen: %v", err) } grpcServer := grpc.NewServer() grpc_health_v1.RegisterHealthServer(grpcServer, NewGrpcHealthChecker()) reflection.Register(grpcServer) if err := grpcServer.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) }}type GrpcHealthChecker struct{}func (s *GrpcHealthChecker) Check(ctx context.Context, req *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) { grpclog.Info("Serving the Check request for health check") return &grpc_health_v1.HealthCheckResponse{ Status: grpc_health_v1.HealthCheckResponse_SERVING, }, nil}func (s *GrpcHealthChecker) Watch(req *grpc_health_v1.HealthCheckRequest, server grpc_health_v1.Health_WatchServer) error { grpclog.Info("Serving the Watch request for health check") return server.Send(&grpc_health_v1.HealthCheckResponse{ Status: grpc_health_v1.HealthCheckResponse_SERVING, })}func NewGrpcHealthChecker() *GrpcHealthChecker { return &GrpcHealthChecker{}}