component.go 1.3 KB

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