当哨兵需要密码时,Jedis 无法使用哨兵找到主人

Jedis cannot find master using sentinel when sentinel required password

最近我在使用redis和jedis。在 Redis 版本 6 中,我们可以为哨兵设置所需的密码模式。我有 3 个工作哨兵,可以通过 redis-cli 连接和验证。但是使用绝地武士,我无法通过此警告连接到哨兵:

Cannot get master address from sentinel running @ 127.0.0.1:26379. Reason: redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.. Trying next one.

Cannot get master address from sentinel running @ 127.0.0.1:36379. Reason: redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.. Trying next one.

Cannot get master address from sentinel running @ 127.0.0.1:16379. Reason: redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.. Trying next one.

还有这个错误:

All sentinels down, cannot determine where is mymaster master is running...

这是我的代码:

        GenericObjectPoolConfig pc = new GenericObjectPoolConfig();
        pc.setMinIdle(2);
        pc.setMaxIdle(5);
        pc.setMaxTotal(5);
        JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, pc, 1000*10, PASSWORD);
        Jedis jedis = null;
        try {
            printer("Fetching connection from pool");
            jedis = pool.getResource();
            printer("Authenticating...");
            jedis.auth(PASSWORD);
            printer("auth complete...");
            Socket socket = jedis.getClient().getSocket();
            printer("Connected to " + socket.getRemoteSocketAddress());
            printer("Writing...");
            jedis.set("java-key-999", "java-value-999");
            printer("Reading...");
            printer(jedis.get("java-key-999"));
        } catch (JedisException e) {
            printer("Connection error of some sort!");
            printer(e.getMessage());
            Thread.sleep(2 * 1000);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

求助,感谢阅读支持<3

您的哨兵节点受密码保护。您必须提供 AUTH 参数才能连接到哨兵节点。

更新:

您提供的密码作为主节点的密码。为避免哨兵节点出现 NOAUTH 错误,您必须提供哨兵节点的密码。寻找任何采用哨兵节点密码的构造函数。该参数通常命名为 sentinelPassword。有几个这样的构造函数,其中最简单的是

JedisSentinelPool(String masterName, Set<String> sentinels, String password, String sentinelPassword)

欢迎您寻找其他最适合您的构造器。