Search code examples
cloud-foundryspring-cloud-configspring-data-redis

Pivotal cloud foundry RedisConnectionFactory


Currently I'm using Redis that is provided by PCF. I'm connecting to it using JedisConnectionFactory from spring-data-redis providing needed configs like this:

@Configuration
public class RedisConfig {

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        final JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
        jedisConFactory.setHostName("pivotal-redis-host");
        jedisConFactory.setPort(1234);
        jedisConFactory.setPassword("mySecretPassword");
        return jedisConFactory;
    }
}

spring-cloud-config provides AbstractCloudConfig class that can be used to configure various connections. Is there any noticeable benefits one must use it instead of JedisConnectionFactory? Looks like less configs is needed to be provided, but is there any other reason?

public class RedisCloudConfig extends AbstractCloudConfig {
    @Bean
    public RedisConnectionFactory redisConnection() {
        return connectionFactory().redisConnectionFactory();
    }
}

Thanks in advance.


Solution

  • The main difference with Spring Cloud Connectors is that it's reading the service information from the Redis service that you bound to your application on Cloud Foundry. It then automatically configures the Redis connection based on that dynamically bound information.

    Your example of using JedisConnectionFactory as well as @avhi's solution are placing the configuration information directly into either your source code or application configuration files. In this case, if your service changes then you'd need to reconfigure your app and run cf push again.

    With Spring Cloud Connectors, you can change services by simply unbinding and binding a new Redis service through CF, and running cf restart.