From b3dcff2d76dc857ad489c37d36fbcf2204528d70 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Thu, 16 Jun 2022 14:45:39 +0800 Subject: [PATCH 1/2] redis interface --- config/config.yaml | 1 + pkg/common/config/config.go | 1 + pkg/common/db/model.go | 44 ++++++++++++++++++++++++------------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 85c5bc9f1..4d9569d07 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -39,6 +39,7 @@ redis: dbMaxActive: 0 dbIdleTimeout: 120 dbPassWord: openIM #redis密码 建议修改 + enableCluster: false kafka: ws2mschat: diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 0618ca7d7..185236744 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -102,6 +102,7 @@ type config struct { DBMaxActive int `yaml:"dbMaxActive"` DBIdleTimeout int `yaml:"dbIdleTimeout"` DBPassWord string `yaml:"dbPassWord"` + EnableCluster bool `yaml:"enableCluster"` } RpcPort struct { OpenImUserPort []int `yaml:"openImUserPort"` diff --git a/pkg/common/db/model.go b/pkg/common/db/model.go index a6ed0408a..895bea53a 100644 --- a/pkg/common/db/model.go +++ b/pkg/common/db/model.go @@ -28,7 +28,14 @@ type DataBases struct { mgoSession *mgo.Session //redisPool *redis.Pool mongoClient *mongo.Client - rdb *go_redis.Client + rdb go_redis.UniversalClient +} + +type RedisClient struct { + client *go_redis.Client + cluster *go_redis.ClusterClient + go_redis.UniversalClient + enableCluster bool } func key(dbAddress, dbName string) string { @@ -112,22 +119,29 @@ func init() { // ) // }, //} - DB.rdb = go_redis.NewClient(&go_redis.Options{ - Addr: config.Config.Redis.DBAddress, - Password: config.Config.Redis.DBPassWord, // no password set - DB: 0, // use default DB - PoolSize: 100, // 连接池大小 - }) - //DB.rdb = go_redis.NewClusterClient(&go_redis.ClusterOptions{ - // Addrs: []string{config.Config.Redis.DBAddress}, - // PoolSize: 50, - //Password: config.Config.Redis.DBPassWord, - //}) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - _, err = DB.rdb.Ping(ctx).Result() - if err != nil { - panic(err.Error()) + if config.Config.Redis.EnableCluster { + DB.rdb = go_redis.NewClusterClient(&go_redis.ClusterOptions{ + Addrs: []string{config.Config.Redis.DBAddress}, + PoolSize: 50, + Password: config.Config.Redis.DBPassWord, + }) + _, err = DB.rdb.Ping(ctx).Result() + if err != nil { + panic(err.Error()) + } + } else { + DB.rdb = go_redis.NewClient(&go_redis.Options{ + Addr: config.Config.Redis.DBAddress, + Password: config.Config.Redis.DBPassWord, // no password set + DB: 0, // use default DB + PoolSize: 100, // 连接池大小 + }) + _, err = DB.rdb.Ping(ctx).Result() + if err != nil { + panic(err.Error()) + } } } From 57d96b85f1edffa702c776c963e0f4f8f496d32b Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Thu, 16 Jun 2022 14:47:39 +0800 Subject: [PATCH 2/2] add global recv opt --- pkg/common/db/model.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/common/db/model.go b/pkg/common/db/model.go index 895bea53a..bc8af133d 100644 --- a/pkg/common/db/model.go +++ b/pkg/common/db/model.go @@ -125,7 +125,6 @@ func init() { DB.rdb = go_redis.NewClusterClient(&go_redis.ClusterOptions{ Addrs: []string{config.Config.Redis.DBAddress}, PoolSize: 50, - Password: config.Config.Redis.DBPassWord, }) _, err = DB.rdb.Ping(ctx).Result() if err != nil {