spring-webfluxazure-redis-cache

Spring webflux with Azure Redis Cache


I am using these properties in my spring webflux app:

# Specify the DNS URI of your Redis cache.
spring.redis.host= somename.redis.cache.windows.net
# Specify the port for your Redis cache.
spring.redis.port=6379
# Specify the access key for your Redis cache.
spring.redis.password= something
spring.redis.ssl=false

Controller code:

@Autowired
    private StringRedisTemplate template;

@GetMapping("/hello")
    public String hello() {
          ValueOperations<String, String> ops = this.template.opsForValue();

        // Add a Hello World string to your cache.
        String key = "greeting";
        if (!this.template.hasKey(key)) {
            ops.set(key, "Hello World!");
        }

        // Return the string from your cache.
        return ops.get(key);
    }

I am unable to connect to the cache from my spring webflux app which has the dependencies:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
    ...
}

I am able to connect to the same cache from Redis CLI on my computer, but my webflux app fails with below errors:

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1602) ~[spring-data-redis-3.0.4.jar:3.0.4]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):

and

Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379

and

Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379

and finally

Caused by: java.net.ConnectException: Connection refused: no further information

Am i missing anything? CLI ping responds with a "PONG" and cache status is "running" on portal.


Solution

  • I had to add an additional configuration for ReactiveRedisConnectionFactory in orger to override the localhost configured in the default lettuce connector:

    @Bean
        public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
            RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
            config.setHostName("somehost");
            config.setPort(6379);
            config.setPassword("accesskey");
    
            LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().build();
    
            return new LettuceConnectionFactory(config, clientConfig);
        }