transport/grpc: add health check (#861)

* add grpc healthcheck
pull/865/head
opensite 4 years ago committed by GitHub
parent 3780f70c91
commit b9e905c1af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      transport/grpc/server.go

@ -11,8 +11,9 @@ import (
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/transport"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
const loggerName = "transport/grpc"
@ -74,6 +75,7 @@ type Server struct {
log *log.Helper
middleware middleware.Middleware
grpcOpts []grpc.ServerOption
health *health.Server
}
// NewServer creates a gRPC server by options.
@ -86,6 +88,7 @@ func NewServer(opts ...ServerOption) *Server {
middleware: middleware.Chain(
recovery.Recovery(),
),
health: health.NewServer(),
}
for _, o := range opts {
o(srv)
@ -99,6 +102,8 @@ func NewServer(opts ...ServerOption) *Server {
grpcOpts = append(grpcOpts, srv.grpcOpts...)
}
srv.Server = grpc.NewServer(grpcOpts...)
// grpc health register
healthpb.RegisterHealthServer(srv.Server, srv.health)
return srv
}
@ -121,12 +126,14 @@ func (s *Server) Start() error {
}
s.lis = lis
s.log.Infof("[gRPC] server listening on: %s", lis.Addr().String())
s.health.Resume()
return s.Serve(lis)
}
// Stop stop the gRPC server.
func (s *Server) Stop() error {
s.GracefulStop()
s.health.Shutdown()
s.log.Info("[gRPC] server stopping")
return nil
}

Loading…
Cancel
Save