I am attempting to connect with Redis Sentinel in JAVA Spring Boot using lettuce. But I am getting error NOAUTH Authentication required.
exception message :: Unable to connect to Redis; nested exception is io.lettuce.core.RedisException: java.util.concurrent.ExecutionException: io.lettuce.core.RedisCommandExecutionException: NOAUTH Authentication required.
But I am passing password correctly. When I test with Standalone redis connection, it is working. Below is the code for standalone connection.
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
RedisStandaloneConfiguration rs = new RedisStandaloneConfiguration("redis-host.svc.cluster.local", 6379);
rs.setPassword(RedisPassword.of("original_password"));
return new LettuceConnectionFactory(rs);
}
@Bean
public CacheManager cacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.disableCachingNullValues();
return RedisCacheManager.builder(lettuceConnectionFactory).cacheDefaults(redisCacheConfiguration).build();
}
@Bean
public RedisTemplate<String, String> redisTemplate() {
final RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setConnectionFactory(lettuceConnectionFactory());
return redisTemplate;
}
Below is the code for connecting to Redis sentinel
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
String pwd = "original_password";
RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration().master("master");
sentinelConfiguration.sentinel("redis-host.svc.cluster.local", 26379);
sentinelConfiguration.setPassword(RedisPassword.of(pwd));
return new LettuceConnectionFactory(sentinelConfiguration);
}
@Bean
public CacheManager cacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.disableCachingNullValues();
return RedisCacheManager.builder(lettuceConnectionFactory).cacheDefaults(redisCacheConfiguration).build();
}
@Bean
public RedisTemplate<String, String> redisTemplate() {
final RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setConnectionFactory(lettuceConnectionFactory());
return redisTemplate;
}
I could connect with same redis sentinel with Node.js properly. Below is the code for Node.js connecting to Redis sentinel correctly.
const Redis = require('ioredis');
const redisClient = new Redis({
sentinels: [
{
host: "redis-host.svc.cluster.local",
port: 26379
},
],
password: 'original_password',
sentinelPassword: 'original_password',
name: 'master',
});
(async function() {
await redisClient.set('key', 'value');
const value = await redisClient.get('key');
console.log(value);
})();
In node.js (io-redis), it seems like we have to pass both password and sentinelPassword to connect with Redis sentinel. Excluding any of those will result in same error which I am getting for JAVA.
But in java (luttece), I could not see any function to set the password, it only has option to set sentinelPassword password.
Kindly help to resolve this issue.
2
Answers
I was initially using spring-boot-starter-data-redis dependency to connect to redis sentinel. But it does not have any function to pass SentinelPassword, so hence redis throws Authentication required error.
Now I changed my redis package to spring-data-redis which has function to pass sentinelPassword and I am able to connect successfully.
This class has setters for both passwords:
https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/connection/RedisSentinelConfiguration.html