JedisCluster密码设置


问题:通过jedisCluster.auth("password");

报错:redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required 

如果是jedis单机模式的话,我们可以直接使用jedis.auth来进行设置

Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.auth("password");
但是 jedisCluster.auth("password");查看源码发现只是实现了异常处理,并没有什么用。

/**
   * @deprecated No key operation doesn't make sense for Redis Cluster and Redis Cluster doesn't
   *             support authorization scheduled to be removed on next major release
   */
  @Deprecated
  @Override
  public String auth(String password) {
    throw new JedisClusterException("No way to dispatch this command to Redis Cluster.");
  }
 查看JedisCluster构造方法发现,有一个这样的

new JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout,int maxAttempts, String password, final GenericObjectPoolConfig poolConfig);

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
 
public class GetData {
    public static void main(String[] args) throws IOException {
        JedisPoolConfig config = new JedisPoolConfig();
        config .setMaxTotal(500);
        config .setMinIdle(2);
        config .setMaxIdle(500);
        config .setMaxWaitMillis(10000);
        config .setTestOnBorrow(true);
        config .setTestOnReturn(true);    
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("xxxx", 6379));
        nodes.add(new HostAndPort("xxxx", 6380));
        nodes.add(new HostAndPort("xxxx", 6381));
        nodes.add(new HostAndPort("xxxx", 6382));
        JedisCluster jedis = new JedisCluster(nodes, 10000, 10000, 100, "password", i2);        
    }        
}
解决办法: 通过JedisCluster jedis = new JedisCluster(nodes, 10000, 10000, 100, "password", config);        来设置集群密码:但是jedis版本必须是2.9及以上
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值