-
Notifications
You must be signed in to change notification settings - Fork 41.4k
Description
Describe the bug
When using spring-boot-starter-data-redis
, the application attempts to connect to Redis during the early initialization phase — before the environment variables or application.yml
values are resolved.
Even when spring.redis.host
is defined via environment variables or configuration files, the connection still falls back to the default localhost:6379
.
This causes Redis connection failures during startup, especially in containerized environments like Docker Compose.
Expected behavior
Redis auto-configuration should resolve spring.redis.host
and other relevant properties before attempting to connect to Redis.
Actual behavior
In certain configurations (e.g. when using RedisMessageListenerContainer
, or RedisTemplate in early beans), Redis tries to connect using the default value (localhost:6379
), even though a different host (e.g. redis
) was configured.
Steps to reproduce
- Create a Spring Boot app with
spring-boot-starter-data-redis
- Configure Redis host via environment variable:
SPRING_REDIS_HOST=redis
- Define a
RedisMessageListenerContainer
bean or use RedisTemplate early - Start app in Docker Compose alongside a Redis container named
redis
- App attempts to connect to
localhost:6379
and fails
Workaround
Manually define the connection factory to ensure proper resolution timing:
@Bean
public RedisConnectionFactory redisConnectionFactory(
@Value("${spring.redis.host}") String host,
@Value("${spring.redis.port}") int port
) {
return new LettuceConnectionFactory(host, port);
}
Environment
Spring Boot version: 3.2.x
Redis client: lettuce
Runtime: Docker Compose
OS: macOS (host)