Upon trying to connect to a remote Redis instance hosted in AWS from a SpringBoot application, RedisConnectionFactory
keeps attempting to connect to localhost:6379
even although the target host has been specified in different ways. This problem occurs both from my development laptop and our development environment. I am able to connect to the target host from Redis InSight as well as from another microservice and, oddly, that microservice has an identical configuration to the one that is failing.
This is my default configuration class:
@Configuration
public class RedisCacheConfig {
@Value("${spring.data.redis.host}")
private String host;
@Value("${spring.data.redis.port}")
private String port;
@Value("${spring.caching.ttl.findAllBillingByEstadoTTL}")
private Integer findAllBillingByEstadoTTLValue;
@Bean
public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(host);
config.setPort(Integer.parseInt(port));
LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder()
.useSsl()
.and()
.commandTimeout(Duration.ofMillis(6000))
.build();
return new LettuceConnectionFactory(config, lettuceClientConfiguration);
}
@Bean
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer() {
return new GenericJackson2JsonRedisSerializer();
}
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer()));
}
@Bean
RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
return (builder) -> builder
.withCacheConfiguration("findAllBillingByEstado",
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(findAllBillingByEstadoTTLValue)));
}
}
As you can see, I'm not using an argument-less constructor for the LettuceConnectionFactory
, which is a problem shown in several similar questions such as this one. I wasn't using the .useSsl()
method before, but I gave it a try based on this question to no avail. Finally, based on this GitHub issue I also removed the ReactiveRedisConnectionFactory
bean in order to rely on Spring's autoconfiguration, but it still insists on connecting to localhost
.
What else could cause this problem?