Unable to connect to Redis,RedisConnectionException: Unable to connect to {hostName}:6379/<unresolved>:6379*
09:20 21 Oct 2023

I'm trying to configure a redis connection within my SpringBoot project. The application starts up without any issues, but when I try to test any Redis operation, specifically when making a call to a REST controller that actually invokes the repository, I keep encountering the same error.

Here are the versions I'm using in my pom.xml:


    org.springframework.boot
    spring-boot-starter-parent
    2.6.6
    


    org.springframework.boot
    spring-boot-starter-data-redis

Below is the configuration class I use for connecting to Redis:

@Configuration
@RequiredArgsConstructor
public class RedisConfigurationBuilder {

    private final RedisProperties redisProperties;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        try {
            RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
            redisStandaloneConfiguration.setHostName(redisProperties.getHost());
            redisStandaloneConfiguration.setPort(redisProperties.getPort());
            redisStandaloneConfiguration.setUsername(redisProperties.getUsername());
            redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));

            LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().useSsl().and()
                    .commandTimeout(Duration.ofSeconds(2)).build();

            return new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
        } catch (Exception e) {
            throw new RuntimeException("Failed to configure Redis", e);
        }
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}

This is the RedisProperties class, which reads the properties from my application.yml file:

@Component
@Getter
public class RedisProperties {

    @Value("${spring.data.redis.host}")
    private String host;

    @Value("${spring.data.redis.port}")
    private int port;

    @Value("${spring.data.redis.username}")
    private String username;

    @Value("${spring.data.redis.password}")
    private String password;

}

and finally, this is my application.yml:

spring:
  application:
    name: "application-name"
  data:
    redis:
      host: {hostName}
      port: 6379
      username: {myUsername}
      password: {myPassword}
      ssl: 
        enabled: true

Note: In the stack trace, I've replaced the real hostname with {hostName} just to hide the actual server I'm connecting to. The error message is as follows:

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to {hostName}:6379/ < unresolved > :6379

And this is the stack trace of the error I keep getting:

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to {hostName}:6379/< unresolved >:6379 at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1689) at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1597) at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1383) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to {hostName}/:6379 at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78) at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56) at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:330) at io.lettuce.core.RedisClient.connect(RedisClient.java:216) at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.lambda$getConnection$1(StandaloneConnectionProvider.java:115) at java.base/java.util.Optional.orElseGet(Optional.java:364) at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:115) at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1595) ... 77 common frames omitted Caused by: java.lang.IllegalArgumentException: Host has a port: {hostName}:6379 at io.lettuce.core.internal.LettuceAssert.isTrue(LettuceAssert.java:222) at io.lettuce.core.internal.HostAndPort.of(HostAndPort.java:57) at io.lettuce.core.SslConnectionBuilder.toHostAndPort(SslConnectionBuilder.java:109) at io.lettuce.core.SslConnectionBuilder.build(SslConnectionBuilder.java:99) at

java spring-boot spring-mvc redis