main.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. grpcc "github.com/go-micro/plugins/v4/client/grpc"
  6. "github.com/go-micro/plugins/v4/registry/consul"
  7. grpcs "github.com/go-micro/plugins/v4/server/grpc"
  8. "github.com/go-micro/plugins/v4/wrapper/trace/opentelemetry"
  9. "github.com/redis/go-redis/v9"
  10. "github.com/sirupsen/logrus"
  11. "go-micro.dev/v4"
  12. "go-micro.dev/v4/registry"
  13. "go-micro.dev/v4/server"
  14. "go.opentelemetry.io/otel"
  15. "go.opentelemetry.io/otel/propagation"
  16. "gorm.io/driver/mysql"
  17. "gorm.io/gorm"
  18. "gorm.io/gorm/logger"
  19. "gorm.io/gorm/schema"
  20. "sghgogs.com/sghblog/authorization-service/config"
  21. "sghgogs.com/sghblog/authorization-service/domain/repository"
  22. "sghgogs.com/sghblog/authorization-service/domain/service"
  23. "sghgogs.com/sghblog/authorization-service/handler"
  24. authorization_service "sghgogs.com/sghblog/authorization-service/proto"
  25. "sghgogs.com/sghblog/authorization-service/utils/authutil"
  26. "sghgogs.com/sghblog/authorization-service/utils/middleware"
  27. "sghgogs.com/sghblog/authorization-service/utils/tracing"
  28. "strings"
  29. "time"
  30. )
  31. var (
  32. name = "authorizationservice"
  33. version = "1.0.0"
  34. )
  35. func main() {
  36. // Load conigurations
  37. if err := config.Load(); err != nil {
  38. logrus.Fatal(err)
  39. }
  40. if cfg := config.RedisAddress(); cfg.Enable {
  41. authutil.NewJWTAuth(redis.NewClient(&redis.Options{
  42. Addr: cfg.URL, // Redis 服务器地址
  43. Password: cfg.Password, // Redis 密码,如果有的话
  44. DB: 0, // 默认数据库
  45. }), name, cfg.Enable)
  46. // cfg.Password
  47. }
  48. // 1. 连接数据库
  49. var db *gorm.DB
  50. if cfg := config.DataBase(); cfg.Enable {
  51. address := fmt.Sprintf("%v:%v@(%v:%v)/%v?charset=utf8mb4,utf8&parseTime=True&loc=Local", cfg.Mysql.User, cfg.Mysql.Password, cfg.Mysql.Host, cfg.Mysql.Port, cfg.Mysql.DataBase)
  52. db, _ = gorm.Open(mysql.Open(address), &gorm.Config{
  53. Logger: logger.Default.LogMode(logger.Info),
  54. NamingStrategy: schema.NamingStrategy{
  55. SingularTable: true,
  56. }})
  57. } else {
  58. // 没有配置数据库
  59. logrus.Info("There is no database configured")
  60. }
  61. // 2. Create service
  62. srv := micro.NewService(
  63. micro.Server(grpcs.NewServer()),
  64. micro.Client(grpcc.NewClient()),
  65. )
  66. authService := authutil.JWTAuthService.Auth
  67. opts := []micro.Option{
  68. micro.Name(name),
  69. micro.Version(version),
  70. micro.Address(config.Address()),
  71. micro.Auth(
  72. authService,
  73. ),
  74. }
  75. // 3.添加注册中心
  76. if cfg := config.Registry(); cfg.Enable {
  77. logrus.Info("添加注册中心")
  78. consul := consul.NewRegistry(func(options *registry.Options) {
  79. options.Addrs = []string{
  80. cfg.Consul.URL,
  81. }
  82. })
  83. opts = append(opts, micro.Registry(consul))
  84. }
  85. // 4.添加链路追踪
  86. if cfg := config.Tracing(); cfg.Enable {
  87. fmt.Println("加入进来了")
  88. tp, err := tracing.NewTracerProvider(name, version, srv.Server().Options().Id, cfg.Jaeger.URL)
  89. if err != nil {
  90. logrus.Fatal(err)
  91. }
  92. defer func() {
  93. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  94. defer cancel()
  95. if err := tp.Shutdown(ctx); err != nil {
  96. logrus.Fatal(err)
  97. }
  98. }()
  99. otel.SetTracerProvider(tp)
  100. otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
  101. traceOpts := []opentelemetry.Option{
  102. opentelemetry.WithHandleFilter(func(ctx context.Context, r server.Request) bool {
  103. if e := r.Endpoint(); strings.HasPrefix(e, "Health.") {
  104. return true
  105. }
  106. return false
  107. }),
  108. }
  109. opts = append(opts, micro.WrapHandler(opentelemetry.NewHandlerWrapper(traceOpts...)))
  110. }
  111. opts = append(opts, micro.WrapHandler(middleware.NewAuthWrapper(srv)))
  112. srv.Init(opts...)
  113. // authutil.JWTAuthService.Client
  114. // 5. 初始化数据库
  115. newRepository := repository.NewRepository(db)
  116. newRepository.InitTable()
  117. // 6.初始化 service
  118. newService := service.NewService(newRepository)
  119. // 注册 Register handler
  120. // 公共
  121. authorization_service.RegisterCommonServiceHandler(srv.Server(), &handler.AdminCommon{
  122. Service: newService,
  123. })
  124. // 角色相关
  125. authorization_service.RegisterAdminRoleServiceHandler(srv.Server(), &handler.AdminRole{
  126. Service: newService,
  127. })
  128. authorization_service.RegisterAdminUserServiceHandler(srv.Server(), &handler.AdminUser{Service: newService})
  129. // 权限认证使用
  130. authorization_service.RegisterAuthenticationServiceHandler(srv.Server(), &handler.Authentication{})
  131. // Run service
  132. logrus.Info("Run service")
  133. if err := srv.Run(); err != nil {
  134. logrus.Fatal(err)
  135. }
  136. }