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. Name string
  13. }
  14. var (
  15. MDB *mongo.Database
  16. RDB redis.UniversalClient
  17. )
  18. func (p *ActorDB) AliasID() string {
  19. return "db" + p.Name
  20. }
  21. // OnInit Actor初始化前触发该函数
  22. func (p *ActorDB) OnInit() {
  23. mongo := p.App().Find(mhayaMongo.Name).(*mhayaMongo.Component)
  24. if mongo == nil {
  25. clog.DPanicf("[component = %s] not found.", mhayaMongo.Name)
  26. }
  27. // 获取 db_id = "center_db_1" 的配置
  28. dbID := p.App().Settings().GetConfig("db_id_list").GetString("game_db_id")
  29. MDB = mongo.GetDb(dbID)
  30. if MDB == nil {
  31. clog.Panic("game_db_id not found")
  32. }
  33. redisConfig := cprofile.GetConfig("redis")
  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. }