Jedis使用带密码的Redis6 Sentinel时无法找到Master节点问题求助
问题根因
你当前传入JedisSentinelPool构造方法的PASSWORD参数,仅作用于Redis主、从节点的身份认证,不会用于Sentinel节点的连接认证。当你为Sentinel节点也配置了密码认证后,Jedis访问Sentinel时未携带有效凭证,就会返回NOAUTH Authentication required错误,无法获取主节点地址,最终触发所有Sentinel节点不可达的报错。
解决方案
根据你使用的Jedis版本选择对应修复方案:
Jedis 2.9+ / 3.x版本
使用包含Sentinel密码参数的重载构造方法,单独传入Sentinel节点的认证密码:
GenericObjectPoolConfig pc = new GenericObjectPoolConfig(); pc.setMinIdle(2); pc.setMaxIdle(5); pc.setMaxTotal(5); // 构造方法参数说明: // 1. 主节点名称 2. Sentinel节点集合 3. 连接池配置 4. 连接超时时间 5. 读写超时时间 // 6. Redis主从节点密码 7. Redis库号 8. 客户端名称 9. Sentinel节点密码 JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, pc, 1000 * 10, 1000 * 10, PASSWORD, 0, null, SENTINEL_PASSWORD);
如果你的Sentinel节点密码和Redis主从节点密码一致,PASSWORD和SENTINEL_PASSWORD可传相同值。
Jedis 4.x+版本
推荐使用JedisSentinelPoolConfig统一配置所有认证参数:
JedisSentinelPoolConfig poolConfig = new JedisSentinelPoolConfig(); poolConfig.setMinIdle(2); poolConfig.setMaxIdle(5); poolConfig.setMaxTotal(5); // 配置Redis主从密码 poolConfig.setPassword(PASSWORD); // 配置Sentinel节点密码 poolConfig.setSentinelPassword(SENTINEL_PASSWORD); JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, poolConfig);
注意事项
- 你代码中
jedis.auth(PASSWORD)的调用是多余操作,JedisSentinelPool返回的连接已经自动完成了主从节点的认证,手动调用auth反而可能触发认证重复报错,建议删除该部分逻辑。 - 确认Sentinel配置文件中
requirepass参数值和你代码中传入的Sentinel密码完全一致。 - 若Sentinel开启了ACL权限控制,需保证使用的账号拥有Sentinel节点的访问权限。
内容的提问来源于stack exchange,提问作者Liam Nguyen
相关产品推荐
相关产品推荐

