redis/lettuce

CacheManager Not working with RedisCredentialsProviderFactory

hk250097 opened this issue · 4 comments

We have implemented RedisConnectionFactory with custom IAMCredentialsProviderFactory to support Google Cloud Memstore IAM policy.
The cacheManager bean is also defined as below
From the application we do

@Autowired
CacheManager cacheManager;
Cache cache = cacheManager.getCache("FWS");

Now it throws

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1847)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1778)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1580)
............
Caused by: io.lettuce.core.RedisException: Cannot obtain initial Redis Cluster topology
at io.lettuce.core.cluster.RedisClusterClient.lambda$getPartitions$0(RedisClusterClient.java:332)
............
Caused by: io.lettuce.core.cluster.topology.DefaultClusterTopologyRefresh$CannotRetrieveClusterPartitions: Cannot retrieve cluster partitions from [redis://10.49.176.245]
Details:
[redis://<>]: recvAddress(..) failed: Connection reset by peer
Suppressed: io.lettuce.core.RedisConnectionException: Unable to connect to [10.49.176.245/:6379]: recvAddress(..) failed: Connection reset by peer
at io.lettuce.core.cluster.topology.DefaultClusterTopologyRefresh.lambda$openConnections$12(DefaultClusterTopologyRefresh.java:347)
at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
@Bean
RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
        .serializeKeysWith(keySerializer)
        .serializeValuesWith(valueSerializer)
        .entryTtl(Duration.ofMinutes(TTL_IN_MINUTES));
    RedisCacheManager redisCacheManager = RedisCacheManager.builder(redisConnectionFactory)
        .cacheDefaults(cacheConfiguration)
        .cacheWriter(RedisCacheWriter.nonLockingRedisCacheWriter((redisConnectionFactory)))
        .build();
    redisCacheManager.afterPropertiesSet();
    return redisCacheManager;
}

@Bean
public RedisConnectionFactory redisConnectionFactory() throws Exception {
    String discoveryEndPointURL = buildDiscoveryEndPointURL();
    RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Collections.singletonList(discoveryEndPointURL));
    LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder()
        .redisCredentialsProviderFactory(new IAMCredentialsProviderFactory(redisServiceAccount))
        .clientOptions(clusterClientOptions()).build();
    LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisClusterConfiguration,lettuceClientConfiguration);
    lettuceConnectionFactory.afterPropertiesSet();
    return lettuceConnectionFactory;
}

When i disable IAM, i.e commented the line

//.redisCredentialsProviderFactory(new IAMCredentialsProviderFactory(redisServiceAccount))\

and redis ip is changed to Not IAM IP endpoint.
It works fine.
Could you please let me know whey is this error ?

Fixed

Fixed

Hey @hk250097 , just in case somebody runs into the same issue it would help if you explain the solution. Thanks!

Fixed

Hey @hk250097 , just in case somebody runs into the same issue it would help if you explain the solution. Thanks!

@tishun SSL wasn't set. I did set the setSSL on the lettuce Configuration and it worked.

Thanks!