0

I'm running Java/Tomcat project in Elastic Beanstalk. I've setup an Elasticache group in the same vpc. Currently, just testing with a single EC2 instance. The app is Spring Boot with spring-boot-starter-redis. It tries to ping Redis with template.getConnectionFactory().getConnection().ping(); on startup and is throwing an exception. Root cause is java.net.ConnectException: Connection refused. If I telnet to the server and port, it works. I installed redis-cli on the same instance and was able to connect and ping to the group and each node. The code also works fine on my local with local redis. Is Jedis connecting to anything other than the visible Elasticache nodes?

  @Autowired
private RedisConnectionFactory connectionFactory;

/**
 * Configure connection factory as per redis server.
 */
@PostConstruct
public void configureConnectionManager() {
    if (cachingEnabled && connectionFactory instanceof JedisConnectionFactory) {
        LOGGER.info("Connecting to Redis cache.");
        JedisConnectionFactory jedisConnectionFactory =
                (JedisConnectionFactory) connectionFactory;
        if (port > 0) {
            jedisConnectionFactory.setPort(port);
        }
        if (StringUtils.isNotBlank(hostname)) {
            jedisConnectionFactory.setHostName(hostname);
        }
        jedisConnectionFactory.setUsePool(true);
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory);
        template.afterPropertiesSet();

        LOGGER.info("Testing connection to Redis server on "
                + jedisConnectionFactory.getHostName()
                + ":" + jedisConnectionFactory.getPort());
        // This will test the connection and throw a runtime exception
        // if the server can't be reached.
        template.getConnectionFactory().getConnection().ping();
        final RedisCacheManager redisCacheManager =
                new RedisCacheManager(template);
        redisCacheManager.setDefaultExpiration(ttl);
        this.cm = redisCacheManager;
    } else {
        // Default implementation incase cache turned off or exception.
        LOGGER.info("Caching disabled for this session.");
        this.cm = new NoOpCacheManager();
    }
}
jiggy
  • 3,828
  • 1
  • 25
  • 40
  • Can it be that your connection string to the Elasticache Redis instance from is wrong (or not in the right format for jedis)? – Liviu Costea Jul 23 '15 at 18:00
  • The log message is giving me the correct host and port. – jiggy Jul 23 '15 at 20:45
  • Have you tried to use the IP address of the host? Avoiding IPv6 issues... PS: Can you post the stack trace, just in case. – mp911de Jul 24 '15 at 08:38
  • It's not networking related, I actually solved the issue by moving this Java-based config to the equivalent XML and it works fine. The issue appears to be the configuration is not being applied to the Spring-managed beans. – jiggy Jul 24 '15 at 15:03

0 Answers0