component.go 2.6 KB

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