component.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package mdb
  2. import (
  3. "github.com/go-redis/redis/v8"
  4. mhayaGORM "github.com/mhaya/components/gorm"
  5. mhayaMongo "github.com/mhaya/components/mongo"
  6. clog "github.com/mhaya/logger"
  7. cactor "github.com/mhaya/net/actor"
  8. cprofile "github.com/mhaya/profile"
  9. "go.mongodb.org/mongo-driver/mongo"
  10. "gorm.io/gorm"
  11. )
  12. type ActorDB struct {
  13. cactor.Base
  14. }
  15. var (
  16. LogstashDB *gorm.DB
  17. MDB *mongo.Database
  18. RDB redis.UniversalClient
  19. )
  20. func (p *ActorDB) AliasID() string {
  21. return "db"
  22. }
  23. // OnInit Actor初始化前触发该函数
  24. func (p *ActorDB) OnInit() {
  25. if p.App().Find(mhayaMongo.Name) != nil {
  26. mongo := p.App().Find(mhayaMongo.Name).(*mhayaMongo.Component)
  27. if mongo == nil {
  28. clog.DPanicf("[component = %s] not found.", mhayaMongo.Name)
  29. }
  30. // 获取 db_id = "center_db_1" 的配置
  31. dbID := p.App().Settings().GetConfig("db_id_list").GetString("game_db_id")
  32. MDB = mongo.GetDb(dbID)
  33. if MDB == nil {
  34. clog.Panic("game_db_id not found")
  35. }
  36. }
  37. if p.App().Find(mhayaGORM.Name) != nil {
  38. gormComponent := p.App().Find(mhayaGORM.Name).(*mhayaGORM.Component)
  39. if gormComponent == nil {
  40. clog.DPanicf("[component = %s] not found.", mhayaGORM.Name)
  41. }
  42. // 获取 db_id = "center_db_1" 的配置
  43. dbID := p.App().Settings().GetConfig("db_id_list").GetString("game_db_id")
  44. LogstashDB = gormComponent.GetDb(dbID)
  45. if LogstashDB == nil {
  46. clog.Panic("game_db_id not found")
  47. }
  48. }
  49. redisConfig := cprofile.GetConfig("redis")
  50. /* tlsConfig := &tls.Config{
  51. MinVersion: tls.VersionTLS12,
  52. PreferServerCipherSuites: true,
  53. }*/
  54. RDB = redis.NewUniversalClient(&redis.UniversalOptions{
  55. Addrs: []string{redisConfig.GetString("address")},
  56. Password: redisConfig.GetString("password"),
  57. DB: redisConfig.GetInt("db"),
  58. //TLSConfig: tlsConfig,
  59. })
  60. if p.App().NodeId() == "m-center" {
  61. SetIndex()
  62. }
  63. }