I have a Redis Memoryspace instance in Google Cloud and I'm trying to connect to it from my Java App. If I try to connect to it using google cloud shell i succeed using these commands:
gcloud container clusters get-credentials CLUSTER_NAME --zone CLUSTER_ZONE --project PROJECT_ID
kubectl run -i --tty redisbox --image=gcr.io/google_containers/redis:v1 -- sh
redis-cli -h HOST-IP
This is my Java connection class:
@Configuration
public class RedisConfig {
@Value("${spring.data.redis.port}")
private int redisPort;
@Value("${spring.data.redis.host}")
private String redisHost;
@Bean
public RedisStandaloneConfiguration redisStandaloneConfiguration() {
return new RedisStandaloneConfiguration(redisHost, redisPort);
}
@Bean
public ClientOptions clientOptions(){
return ClientOptions.builder()
.disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
.autoReconnect(true)
.build();
}
@Bean
LettucePoolingClientConfiguration lettucePoolConfig(ClientOptions options, ClientResources dcr){
return LettucePoolingClientConfiguration.builder()
.poolConfig(new GenericObjectPoolConfig())
.clientOptions(options)
.clientResources(dcr)
.build();
}
@Bean
public RedisConnectionFactory connectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration,
LettucePoolingClientConfiguration lettucePoolConfig) {
return new LettuceConnectionFactory(redisStandaloneConfiguration, lettucePoolConfig);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
But when I try to do anything like this:
redisTemplate.getConnectionFactory().getConnection().ping();
I get the following error:
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to (my host)/<unresolved>:6379
How can I connect to my Redis Memoryspace instance locally?