From 8b649a55a233f11544832bf4484d156d8a5c0605 Mon Sep 17 00:00:00 2001 From: "fengyun.rui" Date: Thu, 12 Oct 2023 10:46:31 +0800 Subject: [PATCH] feat: add redis cluster mode in option (#1178) Signed-off-by: rfyiamcool --- README-zh_CN.md | 1 + pkg/common/config/config.go | 7 ++++--- pkg/common/db/cache/init_redis.go | 5 +++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README-zh_CN.md b/README-zh_CN.md index 37a4dd410..3865f5fa3 100644 --- a/README-zh_CN.md +++ b/README-zh_CN.md @@ -224,6 +224,7 @@ config/config.yaml 文件为存储组件提供了详细的配置说明。 ``` redis: + clusterMode: false #是否为 redis cluster 模式 address: [ 127.0.0.1:16379 ] #地址 username: #用户名 password: openIM123 #密码 diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index b0746449b..966033247 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -75,9 +75,10 @@ type configStruct struct { } `yaml:"mongo"` Redis struct { - Address []string `yaml:"address"` - Username string `yaml:"username"` - Password string `yaml:"password"` + ClusterMode bool `yaml:"clusterMode"` + Address []string `yaml:"address"` + Username string `yaml:"username"` + Password string `yaml:"password"` } `yaml:"redis"` Kafka struct { diff --git a/pkg/common/db/cache/init_redis.go b/pkg/common/db/cache/init_redis.go index fd0aff4a7..1a5507f89 100644 --- a/pkg/common/db/cache/init_redis.go +++ b/pkg/common/db/cache/init_redis.go @@ -39,7 +39,7 @@ func NewRedis() (redis.UniversalClient, error) { } specialerror.AddReplace(redis.Nil, errs.ErrRecordNotFound) var rdb redis.UniversalClient - if len(config.Config.Redis.Address) > 1 { + if len(config.Config.Redis.Address) > 1 || config.Config.Redis.ClusterMode { rdb = redis.NewClusterClient(&redis.ClusterOptions{ Addrs: config.Config.Redis.Address, Username: config.Config.Redis.Username, @@ -58,12 +58,13 @@ func NewRedis() (redis.UniversalClient, error) { }) } - var err error = nil + var err error ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() err = rdb.Ping(ctx).Err() if err != nil { return nil, fmt.Errorf("redis ping %w", err) } + return rdb, err }