server.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package zrpc
  2. import (
  3. "log"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/tal-tech/go-zero/core/load"
  8. "github.com/tal-tech/go-zero/core/logx"
  9. "github.com/tal-tech/go-zero/core/netx"
  10. "github.com/tal-tech/go-zero/core/stat"
  11. "github.com/tal-tech/go-zero/zrpc/internal"
  12. "github.com/tal-tech/go-zero/zrpc/internal/auth"
  13. "github.com/tal-tech/go-zero/zrpc/internal/serverinterceptors"
  14. "google.golang.org/grpc"
  15. )
  16. const (
  17. allEths = "0.0.0.0"
  18. envPodIp = "POD_IP"
  19. )
  20. type RpcServer struct {
  21. server internal.Server
  22. register internal.RegisterFn
  23. }
  24. func MustNewServer(c RpcServerConf, register internal.RegisterFn) *RpcServer {
  25. server, err := NewServer(c, register)
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. return server
  30. }
  31. func NewServer(c RpcServerConf, register internal.RegisterFn) (*RpcServer, error) {
  32. var err error
  33. if err = c.Validate(); err != nil {
  34. return nil, err
  35. }
  36. var server internal.Server
  37. metrics := stat.NewMetrics(c.ListenOn)
  38. if c.HasEtcd() {
  39. listenOn := figureOutListenOn(c.ListenOn)
  40. server, err = internal.NewRpcPubServer(c.Etcd.Hosts, c.Etcd.Key, listenOn, internal.WithMetrics(metrics))
  41. if err != nil {
  42. return nil, err
  43. }
  44. } else {
  45. server = internal.NewRpcServer(c.ListenOn, internal.WithMetrics(metrics))
  46. }
  47. server.SetName(c.Name)
  48. if err = setupInterceptors(server, c, metrics); err != nil {
  49. return nil, err
  50. }
  51. rpcServer := &RpcServer{
  52. server: server,
  53. register: register,
  54. }
  55. if err = c.SetUp(); err != nil {
  56. return nil, err
  57. }
  58. return rpcServer, nil
  59. }
  60. func (rs *RpcServer) AddOptions(options ...grpc.ServerOption) {
  61. rs.server.AddOptions(options...)
  62. }
  63. func (rs *RpcServer) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) {
  64. rs.server.AddStreamInterceptors(interceptors...)
  65. }
  66. func (rs *RpcServer) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) {
  67. rs.server.AddUnaryInterceptors(interceptors...)
  68. }
  69. func (rs *RpcServer) Start() {
  70. if err := rs.server.Start(rs.register); err != nil {
  71. logx.Error(err)
  72. panic(err)
  73. }
  74. }
  75. func (rs *RpcServer) Stop() {
  76. logx.Close()
  77. }
  78. func figureOutListenOn(listenOn string) string {
  79. fields := strings.Split(listenOn, ":")
  80. if len(fields) == 0 {
  81. return listenOn
  82. }
  83. host := fields[0]
  84. if len(host) > 0 && host != allEths {
  85. return listenOn
  86. }
  87. ip := os.Getenv(envPodIp)
  88. if len(ip) == 0 {
  89. ip = netx.InternalIp()
  90. }
  91. if len(ip) == 0 {
  92. return listenOn
  93. } else {
  94. return strings.Join(append([]string{ip}, fields[1:]...), ":")
  95. }
  96. }
  97. func setupInterceptors(server internal.Server, c RpcServerConf, metrics *stat.Metrics) error {
  98. if c.CpuThreshold > 0 {
  99. shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold))
  100. server.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
  101. }
  102. if c.Timeout > 0 {
  103. server.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor(
  104. time.Duration(c.Timeout) * time.Millisecond))
  105. }
  106. if c.Auth {
  107. authenticator, err := auth.NewAuthenticator(c.Redis.NewRedis(), c.Redis.Key, c.StrictControl)
  108. if err != nil {
  109. return err
  110. }
  111. server.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator))
  112. server.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator))
  113. }
  114. return nil
  115. }