Connecting my springboot application to the redis service on development (my system) works perfectly, but it wasn’t working when deployed to the server to run using docker compose.

It throws the error below;

Caused by: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:292)

at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:514)

The issue was related to a Redisconfiguration. Redis was expecting the IP to be running on localhost which wasn’t the case when I deployed it to the server, my applications were not running on localhost IP.

After carefully researching online, adding redis host and redis port to JedisConnectionFactory Bean fixed the issue.

@Configuration
@EnableRedisHttpSession
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {

@Value(“${spring.redis.host:localhost}”)
private String REDIS_HOST;

@Value(“${spring.redis.port:6379}”)
private Integer REDIS_PORT;

@Bean
public JedisConnectionFactory connectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(REDIS_HOST, REDIS_PORT);
return new JedisConnectionFactory(config);
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setEnableTransactionSupport(true);
template.setConnectionFactory(connectionFactory());
return template;
}
}

 

Share: