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