即使redis-cli ping成功,JedisConnectionFactory也无法连接到Elasticache

时间:2015-07-23 14:18:27

标签: redis spring-boot elastic-beanstalk jedis amazon-elasticache

我在Elastic Beanstalk中运行Java / Tomcat项目。我在同一个vpc中设置了一个Elasticache组。目前,仅使用单个EC2实例进行测试。该应用程序是Spring Boot with spring-boot-starter-redis。它尝试在启动时使用template.getConnectionFactory().getConnection().ping(); ping Redis并抛出异常。根本原因是java.net.ConnectException: Connection refused。如果我telnet到服务器和端口,它的工作原理。我在同一个实例上安装了redis-cli,并且能够连接并ping到组和每个节点。该代码也适用于我当地的本地redis。 Jedis是否连接到可见的Elasticache节点以外的任何其他节点?

  @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();
    }
}

0 个答案:

没有答案