2

I am getting Unable to connect to Redis;nested exception is io.lettuce.core.RedisConnectionException using RedisTempalte 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

sree
  • 21
  • 1
  • 1
  • 2
  • Can you tell more about the configuration code you have written? Also, see if this helps https://stackoverflow.com/questions/60872465/unable-to-connect-to-redis-nested-exception-is-io-lettuce-core-redisconnectione – krishg Aug 25 '20 at 09:06

2 Answers2

1

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;
    }
}
Mark Wu
  • 41
  • 3
0

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;
}
abinash sahoo
  • 167
  • 1
  • 5