server.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package micro
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/rcrowley/go-metrics"
  7. "github.com/rpcxio/rpcx-consul/serverplugin"
  8. "github.com/smallnest/rpcx/server"
  9. "time"
  10. )
  11. const Tenant = "Tenant"
  12. func CreateAndInitService(basePath string) *server.Server {
  13. var ctx = gctx.New()
  14. bindAddr, _ := g.Config().Get(ctx, "setting.bind-addr")
  15. registryType, _ := g.Config().Get(ctx, "setting.registry-type")
  16. registryAddr, _ := g.Config().Get(ctx, "setting.registry-addr")
  17. s := server.NewServer()
  18. g.Log().Infof(context.Background(), "服务启动, BindName: %v, BindAddr: %s", basePath, bindAddr.String())
  19. if registryType.String() == "consul" {
  20. addConsulRegistryPlugin(ctx, s, basePath, bindAddr.String(), registryAddr.String())
  21. }
  22. return s
  23. }
  24. func addConsulRegistryPlugin(ctx context.Context, s *server.Server, basePath, srvAddr, consulAddr string) {
  25. r := &serverplugin.ConsulRegisterPlugin{
  26. ServiceAddress: "tcp@" + srvAddr,
  27. ConsulServers: []string{consulAddr},
  28. BasePath: basePath,
  29. Metrics: metrics.NewRegistry(),
  30. UpdateInterval: time.Minute,
  31. }
  32. err := r.Start()
  33. if err != nil {
  34. g.Log().Fatal(ctx, err)
  35. }
  36. g.Log().Infof(ctx, "注册到Consul: %v, basePath: %v, MicorSrv: %v", consulAddr, basePath, srvAddr)
  37. s.Plugins.Add(r)
  38. }
  39. //// HandleAuth 处理身份验证
  40. //func HandleAuth(ctx context.Context, req *protocol.Message, token string, authExcludePaths []string) error {
  41. // tenant := getTenant(req)
  42. // g.Log().Infof(ctx, " ServicePath: %s, ServiceMethod: %s,Tenant:%s", req.ServicePath, req.ServiceMethod, tenant)
  43. //
  44. // reqPath := "/" + req.ServicePath + "/" + req.ServiceMethod
  45. // if authPath(reqPath, authExcludePaths) {
  46. // req.Metadata["authExclude"] = "false"
  47. // var rsp gtoken.Resp
  48. // notAuthSrv := ctx.Value("NotAuthSrv")
  49. // if notAuthSrv != nil && notAuthSrv.(bool) {
  50. // rsp = gtoken.GFToken.ValidToken(ctx, token)
  51. // } else {
  52. // ctx = context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{"tenant": tenant})
  53. // rsp = validToken(ctx, token)
  54. // }
  55. //
  56. // if rsp.Code != 0 && rsp.Code != 200 {
  57. // return gerror.New("Token 认证失败!")
  58. // }
  59. // if req.Metadata != nil {
  60. // req.Metadata["userInfo"] = rsp.DataString()
  61. // }
  62. // return nil
  63. // }
  64. // return nil
  65. //}
  66. //
  67. //// 判断路径是否需要进行认证拦截
  68. //// return true 需要认证
  69. //func authPath(urlPath string, authExcludePaths []string) bool {
  70. // // 去除后斜杠
  71. // if strings.HasSuffix(urlPath, "/") {
  72. // urlPath = gstr.SubStr(urlPath, 0, len(urlPath)-1)
  73. // }
  74. //
  75. // // 排除路径处理,到这里nextFlag为true
  76. // for _, excludePath := range authExcludePaths {
  77. // tmpPath := excludePath
  78. // // 前缀匹配
  79. // if strings.HasSuffix(tmpPath, "/*") {
  80. // tmpPath = gstr.SubStr(tmpPath, 0, len(tmpPath)-2)
  81. // if gstr.HasPrefix(urlPath, tmpPath) {
  82. // // 前缀匹配不拦截
  83. // return false
  84. // }
  85. // } else {
  86. // // 全路径匹配
  87. // if strings.HasSuffix(tmpPath, "/") {
  88. // tmpPath = gstr.SubStr(tmpPath, 0, len(tmpPath)-1)
  89. // }
  90. // if urlPath == tmpPath {
  91. // // 全路径匹配不拦截
  92. // return false
  93. // }
  94. // }
  95. // }
  96. // return true
  97. //}
  98. //
  99. //// 验证token
  100. //func validToken(ctx context.Context, token string) gtoken.Resp {
  101. // grsp := gtoken.Resp{}
  102. // if token == "" {
  103. // grsp.Code = 401
  104. // grsp.Msg = "valid token empty"
  105. // return grsp
  106. // }
  107. //
  108. // authService := InitMicroSrvClient(ctx, "Auth", "micro_srv.auth")
  109. // defer authService.Close()
  110. // rsp := &gtoken.Resp{}
  111. // err := authService.Call(ctx, "ValidToken", token, rsp)
  112. // if err != nil {
  113. // g.Log().Error(ctx, err)
  114. // grsp.Code = 401
  115. // return grsp
  116. // }
  117. // grsp.Code = int(rsp.Code)
  118. // grsp.Msg = rsp.Msg
  119. // grsp.Data = rsp.Data
  120. // return grsp
  121. //}
  122. //
  123. //func getTenant(msg *protocol.Message) string {
  124. // var tenant string
  125. // if msg.Metadata != nil {
  126. // tenant = msg.Metadata[Tenant]
  127. // }
  128. // return tenant
  129. //}