在连接到集群模式中面临的问题禁用了golang中的redis集群

k5ifujac  于 2023-04-05  发布在  Redis
关注(0)|答案(1)|浏览(146)

“我试过下面的代码,

func (rs *RedisClient) Initialise() *RedisClient {
   if rs.enable {
      if rs.instanceDefiner {
         rs.client = redis.NewClient(&redis.Options{
            Addr:     rs.RedisHosts,
            Password: "", // no password set
            DB:       0,  // use default DB
         })
      } else {
         rs.clusterClient = redis.NewClusterClient(&redis.ClusterOptions{
            Addrs: strings.Split(rs.RedisHosts, ","),
         })
         ctx := context.TODO()
         pong, err := rs.clusterClient.Ping(ctx).Result()
         if err != nil {
            logger.Error("Redis connectivity unsuccessful", "err", err)
         } else {
            logger.Info("redis connectivity successful", "ping", pong)
         }
      }
   }
   return rs
}

当我将主机名传递为master.usw02-dev-timeseries-v11-migration.jlpe80.usw2.cache.amazonaws.com:6379时。
它说群集模式被禁用是不支持的。
我希望能够连接到redis缓存。`

bq8i3lrv

bq8i3lrv1#

你试过“新通用客户端”吗?
如果您使用的是'redis/v8',请尝试此操作;

func (rs *RedisClient) Initialise() *RedisClient {
    if rs.enable {
        rs.client = redis.NewUniversalClient(&redis.UniversalOptions{
            Addrs:    strings.Split(rs.RedisHosts, ","),
            Password: "", // no password set
            DB:       0,  // use default DB
        })

        ctx := context.TODO()
        pong, err := rs.client.Ping(ctx).Result()
        if err != nil {
            logger.Error("connectivity unsuccessful", "err", err)
        } else {
            logger.Info("connectivity successful", "ping", pong)
        }
    }
    return rs
}

相关问题