gRPC 其实已经对如何实现心跳和健康检查有了说明了,详情见 https://github.com/grpc/grpc/blob/master/doc/health-checking.md 。而且框架已现了基本的 API 且达到基本开箱即用的程度。
原则上来说,应该很简单,只是调用 API 而已。但是,实际上呢,即使努力百度和谷歌,我们还不一定能跑起来。
其中最关键的就是 如何定义心跳服务,如果你只是简单的看文档,会以为要实现一个 protobuf 服务呢。
实际上呢,并不是,我们只要定义一个独一无二的 服务名 即可(”” 空字符串 表示所有服务)。
type Server
type Server struct {
healthgrpc.UnimplementedHealthServer
// contains filtered or unexported fields
}
func NewServer() *Server
func (s *Server) Check(ctx context.Context, in *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error)
// 将需要检查的服务注册进map
func (s *Server) SetServingStatus(service string, servingStatus healthpb.HealthCheckResponse_ServingStatus)
func (s *Server) Watch(in *healthpb.HealthCheckRequest, stream healthgrpc.Health_WatchServer) error
// Shutdown sets all serving status to NOT_SERVING
func (s *Server) Shutdown()
// Resume sets all serving status to SERVING
func (s *Server) Resume()
使用
客户端
在grpc.WithDefaultServiceConfig 中配置 HealthCheckConfig同时还需要导入_ “google.golang.org/grpc/health”
import _ "google.golang.org/grpc/health"
conn, err := grpc.Dial(address,grpc.WithInsecure(),
grpc.WithDefaultServiceConfig(
fmt.Sprintf(`{"LoadBalancingPolicy": "%s",
"HealthCheckConfig": {"ServiceName": "xxx"}}`
, roundrobin.Name))
)
一个完整客户端例子
conn, err := grpc.DialContext(ctx,
r.Scheme()+"://author/xxx",
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s",
"MethodConfig": [{"Name": [{"Service": "%s"}],
"RetryPolicy": {"MaxAttempts":2, "InitialBackoff": "0.1s", "MaxBackoff": "1s",
"BackoffMultiplier": 2.0,
"RetryableStatusCodes": ["UNAVAILABLE", "CANCELLED"]}}],
"HealthCheckConfig": {"ServiceName": "%s"}}`, roundrobin.Name, *serv, "")),
grpc.WithInsecure())
服务端
服务端注册一个叫xxxx的服务,用于grpc的健康检查。
healthpb "google.golang.org/grpc/health/grpc_health_v1"
s := grpc.NewServer()
hs := health.NewServer()
hs.SetServingStatus("",healthpb.HealthCheckResponse_SERVING)
healthpb.RegisterHealthServer(s, hs)
注意:两个serverName要对应