NOAUTH使用LettuceConnectionFactory连接到Redis Sentinel时需要身份验证

时间:2016-06-21 10:40:41

标签: spring-data spring-data-redis

我正在尝试使用带密码的Sentinel连接到Redis服务器。我收到了RedisCommandExecutionException:调用redisTemplate.opsForValue().get("key")

时需要NOAUTH身份验证
Exception in thread "Thread-6" org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis on localhost:6379; nested exception is com.lambdaworks.redis.RedisCommandExecutionException: NOAUTH Authentication required.
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.createLettuceConnector(LettuceConnectionFactory.java:544)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.initConnection(LettuceConnectionFactory.java:213)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:517)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:189)

这是我配置连接工厂的方式:

RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration().master("mymaster").sentinel("localhost", 26379);
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(sentinelConfiguration);
lettuceConnectionFactory.setDatabase(1);
lettuceConnectionFactory.setPassword("foobared");

当我尝试使用JedisConnectionFactory时,它可以正常工作。

LettuceConnectionFactory与sentinel一起使用时,似乎没有设置密码。

我正在使用spring-data-redis 1.7.2。

更新:

我检查了DefaultLettucePool中的代码,发现当涉及sentinelConfiguration时,密码不会用于生成RedisURI。当我按如下方式修改代码时,它现在可以正常工作。

DefaultLettucePool:

private RedisURI getRedisURI() {

    if (isRedisSentinelAware()) {
        return LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration, password, getDatabase());
    }

    return createSimpleHostRedisURI();
}

在LettuceConverters中:

public static RedisURI sentinelConfigurationToRedisURI(RedisSentinelConfiguration sentinelConfiguration, String password, int database) {
    Assert.notNull(sentinelConfiguration, "RedisSentinelConfiguration is required");

    Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
    RedisURI.Builder builder = null;
    for (RedisNode sentinel : sentinels) {

        if (builder == null) {
            builder = RedisURI.Builder.sentinel(sentinel.getHost(), sentinel.getPort(), sentinelConfiguration.getMaster()
                    .getName());
        }
        else {
            builder.withSentinel(sentinel.getHost(), sentinel.getPort());
        }
    }

    if (password != null) {
        builder = builder.withPassword(password);
    }
    builder.withDatabase(database);

    return builder.build();
}

0 个答案:

没有答案