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