component.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package mdb
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "github.com/go-redis/redis/v8"
  6. mhayaMongo "github.com/mhaya/components/mongo"
  7. clog "github.com/mhaya/logger"
  8. cactor "github.com/mhaya/net/actor"
  9. cprofile "github.com/mhaya/profile"
  10. "go.mongodb.org/mongo-driver/mongo"
  11. "strings"
  12. "time"
  13. "strings"
  14. )
  15. type ActorDB struct {
  16. cactor.Base
  17. Name string
  18. }
  19. var (
  20. MDB *mongo.Database
  21. RDB redis.UniversalClient
  22. )
  23. func (p *ActorDB) AliasID() string {
  24. return "db" + p.Name
  25. }
  26. // OnInit Actor初始化前触发该函数
  27. func (p *ActorDB) OnInit() {
  28. mongo := p.App().Find(mhayaMongo.Name).(*mhayaMongo.Component)
  29. if mongo == nil {
  30. clog.DPanicf("[component = %s] not found.", mhayaMongo.Name)
  31. }
  32. // 获取 db_id = "center_db_1" 的配置
  33. dbID := p.App().Settings().GetConfig("db_id_list").GetString("game_db_id")
  34. MDB = mongo.GetDb(dbID)
  35. if MDB == nil {
  36. clog.Panicf("game_db_id not found")
  37. }
  38. redisConfig := cprofile.GetConfig("redis")
  39. addr := strings.Split(redisConfig.GetString("address"), ",")
  40. isTls := redisConfig.GetInt("tls", 0)
  41. if isTls == 1 {
  42. tlsConfig := &tls.Config{
  43. MinVersion: tls.VersionTLS12,
  44. PreferServerCipherSuites: true,
  45. }
  46. /* RDB = redis.NewUniversalClient(&redis.UniversalOptions{
  47. Addrs: []string{redisConfig.GetString("address")},
  48. Password: redisConfig.GetString("password"),
  49. DB: redisConfig.GetInt("db"),
  50. TLSConfig: tlsConfig,
  51. })*/
  52. clusterOptions := &redis.ClusterOptions{
  53. Addrs: addr,
  54. Password: "", // 如果启用了身份验证,请提供密码
  55. PoolSize: 1000, // 每个节点的最大连接数
  56. DialTimeout: 10 * time.Second,
  57. ReadTimeout: 30 * time.Second,
  58. WriteTimeout: 30 * time.Second,
  59. TLSConfig: tlsConfig,
  60. }
  61. // 创建集群客户端
  62. rdb := redis.NewClusterClient(clusterOptions)
  63. // 测试连接
  64. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  65. defer cancel()
  66. pong, err := rdb.Ping(ctx).Result()
  67. if err != nil {
  68. clog.Panicf("无法连接到Redis集群: %v ,param:%v", err, addr)
  69. }
  70. clog.Warnf("连接到Redis集群: %v,param:%v", pong, addr)
  71. RDB = rdb
  72. } else {
  73. RDB = redis.NewUniversalClient(&redis.UniversalOptions{
  74. Addrs: addr,
  75. Password: redisConfig.GetString("password"),
  76. DB: redisConfig.GetInt("db"),
  77. })
  78. // 测试连接
  79. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  80. defer cancel()
  81. pong, err := RDB.Ping(ctx).Result()
  82. if err != nil {
  83. clog.Panicf("无法连接到Redis: %v ,param:%v", err, addr)
  84. }
  85. clog.Warnf("连接到Redis: %v,param:%v", pong, addr)
  86. }
  87. if p.App().NodeId() == "m-center" {
  88. SetIndex()
  89. }
  90. }