Spring Boot App

时间:2016-07-31 11:32:17

标签: amazon-elasticache spring-data-redis

我正在尝试使用spring-data-redis将AWS elasticache redis与spring boot应用程序集成。使用服务时获得以下异常。

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:191)

... 84 more

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused
at redis.clients.jedis.Connection.connect(Connection.java:164)

使用以下代码

@Bean
public RedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
    JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
    redisConnectionFactory.setHostName("AWS primary replication group endpoint");
    redisConnectionFactory.setPort(6379);
    redisConnectionFactory.setUsePool(true);
    redisConnectionFactory.setTimeout(3600);
    return redisConnectionFactory;
}

我可以从部署服务的EC2实例连接到AWS Redis。任何示例代码或步骤都非常有用。

1 个答案:

答案 0 :(得分:0)

可能会迟到这个派对,但根据我的经验,在spring.redis.hostapplication.yml或命令行中声明application.properties作为-D参数不被尊重RedisProperties内的RedisAutoConfiguration

最终对我有用的是导出环境变量,如此

export SPRING_REDIS_HOST=...

在启动尝试连接到ElastiCache的服务之前。