spring-projects / spring-session

Spring Session
https://spring.io/projects/spring-session
Apache License 2.0
1.86k stars 1.11k forks source link

Azure Redis Cache - keep getting: readAddress(..) failed: Connection reset by peer #3112

Closed dreamstar-enterprises closed 2 months ago

dreamstar-enterprises commented 2 months ago

I can connect using the redis cli, but not through Spring Boot Webflux, I keep getting readAddress(..) failed: Connection reset by peer

So, i figured this was a spring issue, using lettuce.

Here is my code:

@Bean
    @Primary
    fun reactiveRedisConnectionFactory(): ReactiveRedisConnectionFactory {

        // Configure Redis standalone configuration
        val config = RedisStandaloneConfiguration()
        config.hostName = redisHostName
        config.port = redisPort
        config.setPassword(redisPassword) // Use the token as password

        // Create socket options
        val socketOptions = SocketOptions.builder()
            .keepAlive(true)
            .build()

        // Create client options
        val clientOptions = ClientOptions.builder()
            .socketOptions(socketOptions)
            .build()

        // Create Lettuce client configuration with authentication details
        val clientConfig = LettucePoolingClientConfiguration.builder()
            .commandTimeout(Duration.ofSeconds(60))
            .clientResources(DefaultClientResources.create())
            .clientOptions(clientOptions)
            .poolConfig(buildLettucePoolConfig())
            .useSsl()
            .build()

        // Create Lettuce connection factory
        return LettuceConnectionFactory(config, clientConfig).apply {
            afterPropertiesSet()
        }
    }

    protected fun buildLettucePoolConfig(): GenericObjectPoolConfig<Any> {
        val poolConfig = GenericObjectPoolConfig<Any>()
        poolConfig.maxTotal = 100
        poolConfig.maxIdle = 50
        poolConfig.minIdle = 50
        poolConfig.setMaxWait(Duration.ofMillis(60000))
        poolConfig.timeBetweenEvictionRuns = Duration.ofMillis(60000)
        poolConfig.minEvictableIdleDuration = Duration.ofMillis(60000)
        return poolConfig
    }
}

spring.data.redis.host=hostname.redis.cache.windows.net
spring.data.redis.password=access-key
spring.data.redis.port=6380

And the error chain I keep getting, after about 10 seconds:

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis Caused by: org.springframework.data.redis.connection.PoolException: Could not get a resource from the pool Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to hostname.redis.cache.windows.net/:6380 Caused by: io.netty.channel.unix.Errors$NativeIoException: readAddress(..) failed: Connection reset by peer

Would be grateful for nay help and support

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior.

Expected behavior A clear and concise description of what you expected to happen.

Sample

A link to a GitHub repository with a minimal, reproducible sample.

Reports that include a sample will take priority over reports that do not. At times, we may require a sample, so it is good to try and include a sample up front.

marcusdacoregio commented 2 months ago

Hi @dreamstar-enterprises, thanks for the report.

I don't see Spring Session involved in your configuration. In addition to that, Spring Session uses Spring Data Redis to manage the connections with Redis, you should open this issue on their issue tracker.

dreamstar-enterprises commented 2 months ago

Hi Marcus, My BFF code can be found here: https://github.com/dreamstar-enterprises/docs/tree/master/Spring%20BFF/bff but I was able to resolve it, though I still often get timeout or connection issues with Azure Redis Cache

Many thanks