rpcserver.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package internal
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/zeromicro/go-zero/core/proc"
  6. "github.com/zeromicro/go-zero/core/stat"
  7. "github.com/zeromicro/go-zero/internal/health"
  8. "github.com/zeromicro/go-zero/zrpc/internal/serverinterceptors"
  9. "google.golang.org/grpc"
  10. "google.golang.org/grpc/health/grpc_health_v1"
  11. )
  12. const probeNamePrefix = "zrpc"
  13. type (
  14. // ServerOption defines the method to customize a rpcServerOptions.
  15. ServerOption func(options *rpcServerOptions)
  16. rpcServerOptions struct {
  17. metrics *stat.Metrics
  18. health bool
  19. }
  20. rpcServer struct {
  21. *baseRpcServer
  22. name string
  23. middlewares ServerMiddlewaresConf
  24. healthManager health.Probe
  25. }
  26. )
  27. // NewRpcServer returns a Server.
  28. func NewRpcServer(addr string, middlewares ServerMiddlewaresConf, opts ...ServerOption) Server {
  29. var options rpcServerOptions
  30. for _, opt := range opts {
  31. opt(&options)
  32. }
  33. if options.metrics == nil {
  34. options.metrics = stat.NewMetrics(addr)
  35. }
  36. return &rpcServer{
  37. baseRpcServer: newBaseRpcServer(addr, &options),
  38. middlewares: middlewares,
  39. healthManager: health.NewHealthManager(fmt.Sprintf("%s-%s", probeNamePrefix, addr)),
  40. }
  41. }
  42. func (s *rpcServer) SetName(name string) {
  43. s.name = name
  44. s.baseRpcServer.SetName(name)
  45. }
  46. func (s *rpcServer) Start(register RegisterFn) error {
  47. lis, err := net.Listen("tcp", s.address)
  48. if err != nil {
  49. return err
  50. }
  51. unaryInterceptors := s.buildUnaryInterceptors()
  52. unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
  53. streamInterceptors := s.buildStreamInterceptors()
  54. streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
  55. options := append(s.options, grpc.ChainUnaryInterceptor(unaryInterceptors...),
  56. grpc.ChainStreamInterceptor(streamInterceptors...))
  57. server := grpc.NewServer(options...)
  58. register(server)
  59. // register the health check service
  60. if s.health != nil {
  61. grpc_health_v1.RegisterHealthServer(server, s.health)
  62. s.health.Resume()
  63. }
  64. s.healthManager.MarkReady()
  65. health.AddProbe(s.healthManager)
  66. // we need to make sure all others are wrapped up,
  67. // so we do graceful stop at shutdown phase instead of wrap up phase
  68. waitForCalled := proc.AddWrapUpListener(func() {
  69. if s.health != nil {
  70. s.health.Shutdown()
  71. }
  72. server.GracefulStop()
  73. })
  74. defer waitForCalled()
  75. return server.Serve(lis)
  76. }
  77. func (s *rpcServer) buildStreamInterceptors() []grpc.StreamServerInterceptor {
  78. var interceptors []grpc.StreamServerInterceptor
  79. if s.middlewares.Trace {
  80. interceptors = append(interceptors, serverinterceptors.StreamTracingInterceptor)
  81. }
  82. if s.middlewares.Recover {
  83. interceptors = append(interceptors, serverinterceptors.StreamRecoverInterceptor)
  84. }
  85. if s.middlewares.Breaker {
  86. interceptors = append(interceptors, serverinterceptors.StreamBreakerInterceptor)
  87. }
  88. return interceptors
  89. }
  90. func (s *rpcServer) buildUnaryInterceptors() []grpc.UnaryServerInterceptor {
  91. var interceptors []grpc.UnaryServerInterceptor
  92. if s.middlewares.Trace {
  93. interceptors = append(interceptors, serverinterceptors.UnaryTracingInterceptor)
  94. }
  95. if s.middlewares.Recover {
  96. interceptors = append(interceptors, serverinterceptors.UnaryRecoverInterceptor)
  97. }
  98. if s.middlewares.Stat {
  99. interceptors = append(interceptors, serverinterceptors.UnaryStatInterceptor(s.metrics))
  100. }
  101. if s.middlewares.Prometheus {
  102. interceptors = append(interceptors, serverinterceptors.UnaryPrometheusInterceptor)
  103. }
  104. if s.middlewares.Breaker {
  105. interceptors = append(interceptors, serverinterceptors.UnaryBreakerInterceptor)
  106. }
  107. return interceptors
  108. }
  109. // WithMetrics returns a func that sets metrics to a Server.
  110. func WithMetrics(metrics *stat.Metrics) ServerOption {
  111. return func(options *rpcServerOptions) {
  112. options.metrics = metrics
  113. }
  114. }
  115. // WithRpcHealth returns a func that sets rpc health switch to a Server.
  116. func WithRpcHealth(health bool) ServerOption {
  117. return func(options *rpcServerOptions) {
  118. options.health = health
  119. }
  120. }