rpcserver.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package internal
  2. import (
  3. "net"
  4. "github.com/zeromicro/go-zero/core/proc"
  5. "github.com/zeromicro/go-zero/core/stat"
  6. "github.com/zeromicro/go-zero/zrpc/internal/serverinterceptors"
  7. "google.golang.org/grpc"
  8. "google.golang.org/grpc/health/grpc_health_v1"
  9. )
  10. type (
  11. // ServerOption defines the method to customize a rpcServerOptions.
  12. ServerOption func(options *rpcServerOptions)
  13. rpcServerOptions struct {
  14. metrics *stat.Metrics
  15. }
  16. rpcServer struct {
  17. name string
  18. *baseRpcServer
  19. }
  20. )
  21. func init() {
  22. InitLogger()
  23. }
  24. // NewRpcServer returns a Server.
  25. func NewRpcServer(address string, opts ...ServerOption) Server {
  26. var options rpcServerOptions
  27. for _, opt := range opts {
  28. opt(&options)
  29. }
  30. if options.metrics == nil {
  31. options.metrics = stat.NewMetrics(address)
  32. }
  33. return &rpcServer{
  34. baseRpcServer: newBaseRpcServer(address, &options),
  35. }
  36. }
  37. func (s *rpcServer) SetName(name string) {
  38. s.name = name
  39. s.baseRpcServer.SetName(name)
  40. }
  41. func (s *rpcServer) Start(register RegisterFn) error {
  42. lis, err := net.Listen("tcp", s.address)
  43. if err != nil {
  44. return err
  45. }
  46. unaryInterceptors := []grpc.UnaryServerInterceptor{
  47. serverinterceptors.UnaryTracingInterceptor,
  48. serverinterceptors.UnaryCrashInterceptor,
  49. serverinterceptors.UnaryStatInterceptor(s.metrics),
  50. serverinterceptors.UnaryPrometheusInterceptor,
  51. serverinterceptors.UnaryBreakerInterceptor,
  52. }
  53. unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
  54. streamInterceptors := []grpc.StreamServerInterceptor{
  55. serverinterceptors.StreamTracingInterceptor,
  56. serverinterceptors.StreamCrashInterceptor,
  57. serverinterceptors.StreamBreakerInterceptor,
  58. }
  59. streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
  60. options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),
  61. WithStreamServerInterceptors(streamInterceptors...))
  62. server := grpc.NewServer(options...)
  63. register(server)
  64. // register the health check service
  65. grpc_health_v1.RegisterHealthServer(server, s.health)
  66. s.health.Resume()
  67. // we need to make sure all others are wrapped up,
  68. // so we do graceful stop at shutdown phase instead of wrap up phase
  69. waitForCalled := proc.AddWrapUpListener(func() {
  70. s.health.Shutdown()
  71. server.GracefulStop()
  72. })
  73. defer waitForCalled()
  74. return server.Serve(lis)
  75. }
  76. // WithMetrics returns a func that sets metrics to a Server.
  77. func WithMetrics(metrics *stat.Metrics) ServerOption {
  78. return func(options *rpcServerOptions) {
  79. options.metrics = metrics
  80. }
  81. }