component.go 1.3 KB

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