I am getting Unable to connect to Redis;nested exception is io.lettuce.core.RedisConnectionException using RedisTemplate error when I try to connect azure redis cache from my spring reactive application.
I have configured below in properties file
spring.redis.host=hostName
spring.redis.port=6379
spring.redis.password=password
also tried java based configuration using LettuceConnectionFactory
-
Can you tell more about the configuration code you have written? Also, see if this helps stackoverflow.com/questions/60872465/…krishg– krishg2020年08月25日 09:06:44 +00:00Commented Aug 25, 2020 at 9:06
2 Answers 2
I have encountered the same problem when enabling password authentication in AWS ElastiCache because of not using SSL for the Redis client.
Bellow RedisConfig.java uses SSL to connect and my error disappeared. I am using Spring Reactive by the way.
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Setter
public class RedisConfig {
private String host;
private String password;
@Bean
@Primary
public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory(RedisConfiguration defaultRedisConfig) {
LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
.useSsl().and()
.commandTimeout(Duration.ofMillis(60000)).build();
return new LettuceConnectionFactory(defaultRedisConfig, clientConfig);
}
@Bean
public RedisConfiguration defaultRedisConfig() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(host);
config.setPassword(RedisPassword.of(password));
return config;
}
}
Comments
Make Sure to use jedisConFactory.setUseSsl(true); for aws redis connection. By Default ssl is off for redis connection.
@Bean
JedisConnectionFactory jedisConnectionFactory() {
final JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(300);
jedisPoolConfig.setMaxIdle(20);
jedisPoolConfig.setMaxWaitMillis(2000);
jedisPoolConfig.setBlockWhenExhausted(true);
JedisConnectionFactory jedisConFactory = new JedisConnectionFactory(jedisPoolConfig);
jedisConFactory.setHostName(redisHost);
jedisConFactory.setPort(redisPort);
jedisConFactory.setPassword(redisPassword);
jedisConFactory.setUsePool(true);
jedisConFactory.setTimeout(redisTimeout);
jedisConFactory.setUseSsl(redisSsl);
return jedisConFactory;
}
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
//template.setEnableTransactionSupport(true);
template.setExposeConnection(true);
template.afterPropertiesSet();
return template;
}
Comments
Explore related questions
See similar questions with these tags.