component.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /* tlsConfig := &tls.Config{
  34. MinVersion: tls.VersionTLS12,
  35. PreferServerCipherSuites: true,
  36. }*/
  37. RDB = redis.NewUniversalClient(&redis.UniversalOptions{
  38. Addrs: []string{redisConfig.GetString("address")},
  39. Password: redisConfig.GetString("password"),
  40. DB: redisConfig.GetInt("db"),
  41. //TLSConfig: tlsConfig,
  42. })
  43. if p.App().NodeId() == "m-center" {
  44. SetIndex()
  45. }
  46. }