server.go 3.1 KB

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