0

Spring gateway cant' read or connect to redis... Getting such error:

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis
...
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379

The problem is redis configured not locally but on specific ip, application.yml:

spring:
  redis:
    host: 1.2.3.4
    port: 6379
    password: passw
    database: 0
    timeout: 3000
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 2
        max-wait: 5000ms

I can't connect to redis via telnet, but obviously it's not even trying to connect to remote host, but instead connecting locally...

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.5'
    id 'io.spring.dependency-management' version '1.1.0'
}
group = 'com.example'
sourceCompatibility = '17'
repositories {
    mavenCentral()
}
ext {
    set('springCloudVersion', "2022.0.1")
}
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    // --- cache
    implementation 'org.springframework.boot:spring-boot-starter-cache'
    implementation 'com.github.ben-manes.caffeine:caffeine:3.1.5'
    // --- redis
    implementation group: 'org.springframework.data', name: 'spring-data-redis', version: '3.0.4'
    implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
    //
    implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer'
    implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-reactor-resilience4j'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
tasks.named('test') {
    useJUnitPlatform()
}

Why could be the cause?

littleAlien
  • 721
  • 2
  • 8
  • 20
  • What does your redis connection configuration look like? I.e. `LettuceConnectionFactory` – tobifasc Apr 04 '23 at 12:27
  • Do you have connection settings in your application.properties? I.e. https://www.baeldung.com/spring-data-redis-properties#properties – tobifasc Apr 05 '23 at 08:27
  • @tobifasc yes, it's in original post, please take a look above – littleAlien Apr 05 '23 at 14:13
  • Sorry my fault. I wasn't even aware you could configure spring with yaml :-) – tobifasc Apr 05 '23 at 14:15
  • According to [this answer](https://stackoverflow.com/a/63485555/2633917) creating a `RedisTemplate` bean helped. Have you tried this? – tobifasc Apr 05 '23 at 14:18
  • @tobifasc no, but it helped me to see that properties actually not set in RedisConnectionFactory... it doesn't have 'host' value from application.yml... – littleAlien Apr 05 '23 at 15:02
  • @tobifasc thanks, fixed the issue by obvious creating properties and instantiating LettuceConnectionFactory with those properties. Wonder if spring developers ever test what they release?!... – littleAlien Apr 05 '23 at 15:14
  • Glad you got it working! After looking at the question again i noticed: the property hierarchy should probably be spring.data.redis (instead of spring.redis...). See [here](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#redis-properties). Maybe then it works without explicitly creating the LettuceConnectionFactory with your properties – tobifasc Apr 05 '23 at 16:12
  • @tobifasc yes, thanks! now it's working even without code changes! ) – littleAlien Apr 05 '23 at 18:14

1 Answers1

0

Thanks to @tobifasc, the fix is to change redis configuration to spring.data.redis...

littleAlien
  • 721
  • 2
  • 8
  • 20