feat: add redis cluster mode in option (#1178)

Signed-off-by: rfyiamcool <rfyiamcool@163.com>
pull/1183/head
fengyun.rui 1 year ago committed by GitHub
parent d8dbcbbec6
commit 8b649a55a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -224,6 +224,7 @@ config/config.yaml 文件为存储组件提供了详细的配置说明。
``` ```
redis: redis:
clusterMode: false #是否为 redis cluster 模式
address: [ 127.0.0.1:16379 ] #地址 address: [ 127.0.0.1:16379 ] #地址
username: #用户名 username: #用户名
password: openIM123 #密码 password: openIM123 #密码

@ -75,9 +75,10 @@ type configStruct struct {
} `yaml:"mongo"` } `yaml:"mongo"`
Redis struct { Redis struct {
Address []string `yaml:"address"` ClusterMode bool `yaml:"clusterMode"`
Username string `yaml:"username"` Address []string `yaml:"address"`
Password string `yaml:"password"` Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"redis"` } `yaml:"redis"`
Kafka struct { Kafka struct {

@ -39,7 +39,7 @@ func NewRedis() (redis.UniversalClient, error) {
} }
specialerror.AddReplace(redis.Nil, errs.ErrRecordNotFound) specialerror.AddReplace(redis.Nil, errs.ErrRecordNotFound)
var rdb redis.UniversalClient 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{ rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: config.Config.Redis.Address, Addrs: config.Config.Redis.Address,
Username: config.Config.Redis.Username, 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) ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel() defer cancel()
err = rdb.Ping(ctx).Err() err = rdb.Ping(ctx).Err()
if err != nil { if err != nil {
return nil, fmt.Errorf("redis ping %w", err) return nil, fmt.Errorf("redis ping %w", err)
} }
return rdb, err return rdb, err
} }

Loading…
Cancel
Save