mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
parent
3780f70c91
commit
b9e905c1af
@ -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…
x
Reference in New Issue
Block a user