Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 472
user1354825 Avatar asked Feb 01 '26 06:02

user1354825


1 Answers

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);
    }

like image 154
user1354825 Avatar answered Feb 03 '26 23:02

user1354825



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!