You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成Redis Sentinel遇NOAUTH认证错误,如何配置密码?

问题解决:Redis Sentinel连接主节点NOAUTH错误

问题原因

你当前的配置把密码仅设置给了Sentinel节点,但Redis主从节点的认证密码没有被正确传递。当Lettuce通过Sentinel定位到主节点后,连接主节点时未携带认证信息,触发了NOAUTH HELLO must be called with the client already authenticated错误。

解决方案

方式1:通过RedisURI Builder手动配置(代码方式)

使用RedisURI.builder()可以明确区分Sentinel节点和Redis主从节点的密码(两者密码相同时可统一设置,不同时分别配置):

@Bean
public RedisURI redisUri() {
    return RedisURI.builder()
            // 添加Sentinel节点
            .withSentinel("host1", port1)
            .withSentinel("host2", port2)
            .withSentinel("host3", port3)
            // 指定Sentinel的master ID
            .withSentinelMasterId("mymaster")
            // 设置Redis主从节点的认证密码
            .withPassword("pass".toCharArray())
            // 如果Sentinel本身需要密码认证,添加此行;否则可删除
            .withSentinelPassword("pass".toCharArray())
            // 指定数据库
            .withDatabase(1)
            .build();
}

@Bean
public RedisClient redisClient(RedisURI redisUri) {
    return RedisClient.create(redisUri);
}

@Bean
public StatefulRedisConnection<String, String> statefulRedisConnection(RedisClient redisClient) {
    return redisClient.connect();
}

@Bean
public RedisCommands<String, String> redisCommands(StatefulRedisConnection<String, String> statefulRedisConnection) {
    return statefulRedisConnection.sync();
}

方式2:使用Spring Boot配置文件(更推荐)

利用Spring Boot的自动配置能力,在application.yml或application.properties中配置,无需手动创建Bean:

spring:
  data:
    redis:
      # Redis主从节点的认证密码
      password: pass
      database: 1
      sentinel:
        # Sentinel的master ID
        master: mymaster
        # Sentinel节点列表
        nodes: host1:port1,host2:port2,host3:port3
        # 如果Sentinel需要密码认证,添加此行;否则可删除
        password: pass

关键说明

  • 若Sentinel集群本身不需要密码认证,只需配置spring.data.redis.password即可,无需设置sentinel.password。
  • 你的原URI格式redis-sentinel://pass@host1:port1...中,pass仅作用于Sentinel节点的认证,不会自动传递给Redis主从节点,这是导致错误的核心原因。

内容的提问来源于stack exchange,提问作者bogizzla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 07:25:32