mgo_config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package mgodb
  2. import (
  3. "context"
  4. "eta_gn/eta_api/utils"
  5. "github.com/qiniu/qmgo"
  6. "github.com/qiniu/qmgo/options"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/event"
  9. mgoptions "go.mongodb.org/mongo-driver/mongo/options"
  10. "sync"
  11. )
  12. var (
  13. ConnectTimeoutMS = int64(3000)
  14. SocketTimeoutMS = int64(500000)
  15. MaxPoolSize = uint64(50)
  16. MinPoolSize = uint64(0)
  17. )
  18. type MgoConfig struct {
  19. Host string `json:"host"`
  20. Url string `json:"url"`
  21. Port string `json:"port"`
  22. Username string `json:"username"`
  23. Password string `json:"password"`
  24. AuthSource string `json:"authSource"`
  25. AuthMechanism string `json:"auth_mechanism"`
  26. Database string `json:"database"`
  27. }
  28. // MgoNewClient
  29. // @Description: 创建一个mongodb客户端链接
  30. // @author: Roc
  31. // @datetime 2024-04-25 14:01:14
  32. // @return *qmgo.Client
  33. func MgoNewClient(mgoConfig MgoConfig) *qmgo.Client {
  34. // 创建cmdMonitor,用于打印SQL
  35. //startedCommands := make(map[int64]bson.Raw)
  36. startedCommands := sync.Map{} // map[int64]bson.Raw
  37. cmdMonitor := &event.CommandMonitor{
  38. Started: func(_ context.Context, evt *event.CommandStartedEvent) {
  39. startedCommands.Store(evt.RequestID, evt.Command)
  40. //startedCommands[evt.RequestID] = evt.Command
  41. },
  42. Succeeded: func(_ context.Context, evt *event.CommandSucceededEvent) {
  43. //log.Printf("Command: %v Reply: %v\n",
  44. // startedCommands[evt.RequestID],
  45. // evt.Reply,
  46. //)
  47. var commands bson.Raw
  48. v, ok := startedCommands.Load(evt.RequestID)
  49. if ok {
  50. commands = v.(bson.Raw)
  51. }
  52. //utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] %v \n", float64(evt.Duration)/1e6, commands, evt.Reply)
  53. utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] \n", float64(evt.Duration)/1e6, commands)
  54. },
  55. Failed: func(_ context.Context, evt *event.CommandFailedEvent) {
  56. //log.Printf("Command: %v Failure: %v\n",
  57. // startedCommands[evt.RequestID],
  58. // evt.Failure,
  59. //)
  60. var commands bson.Raw
  61. v, ok := startedCommands.Load(evt.RequestID)
  62. if ok {
  63. commands = v.(bson.Raw)
  64. }
  65. utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] \n %v \n", float64(evt.Duration)/1e6, commands, evt.Failure)
  66. },
  67. }
  68. // 创建options
  69. ops := options.ClientOptions{ClientOptions: &mgoptions.ClientOptions{}}
  70. ops.SetMonitor(cmdMonitor)
  71. ctx := context.Background()
  72. //mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl
  73. //var mongoUrl string
  74. //if mgoConfig.Password != "" {
  75. // mongoUrl = "mongodb://" + mgoConfig.Username + ":" + mgoConfig.Password + "@" +
  76. // mgoConfig.Host + ":" + mgoConfig.Port
  77. //
  78. // if mgoConfig.AuthSource != `` {
  79. // mongoUrl += mongoUrl + "?&authSource=" + mgoConfig.AuthSource
  80. // }
  81. //} else {
  82. // mongoUrl = "mongodb://" + mgoConfig.Host + ":" + mgoConfig.Port
  83. //}
  84. // 创建一个数据库链接
  85. client, err := qmgo.NewClient(ctx, &qmgo.Config{
  86. Uri: mgoConfig.Url,
  87. Auth: &qmgo.Credential{
  88. AuthMechanism: mgoConfig.AuthMechanism,
  89. AuthSource: mgoConfig.AuthSource,
  90. Username: mgoConfig.Username,
  91. Password: mgoConfig.Password,
  92. //PasswordSet: false,
  93. },
  94. Database: mgoConfig.Database,
  95. ConnectTimeoutMS: &ConnectTimeoutMS,
  96. SocketTimeoutMS: &SocketTimeoutMS,
  97. MaxPoolSize: &MaxPoolSize,
  98. MinPoolSize: &MinPoolSize,
  99. }, ops)
  100. if err != nil {
  101. panic("MongoDB连接异常:" + err.Error())
  102. }
  103. return client
  104. }
  105. func MgoGetColl(collName string) (cli *qmgo.QmgoClient, err error) {
  106. ctx := context.Background()
  107. config := &qmgo.Config{
  108. //Uri: MGO_URL,
  109. //Database: MGO_DB,
  110. Coll: collName,
  111. ConnectTimeoutMS: &ConnectTimeoutMS,
  112. SocketTimeoutMS: &SocketTimeoutMS,
  113. MaxPoolSize: &MaxPoolSize,
  114. MinPoolSize: &MinPoolSize,
  115. }
  116. cli, err = qmgo.Open(ctx, config)
  117. return
  118. }