1
0
mirror of https://github.com/go-kratos/kratos.git synced 2026-05-22 10:15:24 +02:00

transport/grpc: add health check (#861)

* add grpc healthcheck
This commit is contained in:
opensite
2021-04-26 13:12:27 +08:00
committed by GitHub
parent 3780f70c91
commit b9e905c1af
+8 -1
View File
@@ -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
}