2017-10-10 160 views
0

試圖與SpringData使用SpringBoot與Elasticache:SpringBoot Elasticache JedisMovedDataException:移到

application.properties:

spring.redis.host=XXXX-dev.XXXX.clusXXXcfg.XXX.cache.amazonaws.com 
spring.redis.port=6379 

CacheConfiguration:

@Configuration 
@PropertySource("classpath:application.properties") 
public class CacheConfiguration { 


@Value("${spring.redis.host}") 
private String redisHostName; 

@Bean 
public RedisTemplate<String, Company> redisTemplate() { 
    RedisTemplate<String, Company> template = new RedisTemplate(); 
    template.setConnectionFactory(jedisConnectionFactory()); 
    return template; 
} 

@Bean 
JedisConnectionFactory jedisConnectionFactory() { 
    JedisConnectionFactory factory = new JedisConnectionFactory(); 
    factory.setHostName(redisHostName); 
    factory.setUsePool(true); 
    return factory; 
} 


@Bean 
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
    return new PropertySourcesPlaceholderConfigurer(); 
} 

}

服務電話:

@Autowired 
RedisTemplate<String, Company> redisTemplate; 

private ValueOperations valueOperations; 

@PostConstruct 
private void init() { 
    valueOperations = redisTemplate.opsForValue(); 
} 

@Override 
public String createOtp(Company company) { 
    String token = UUID.randomUUID().toString(); 
    valueOperations.set(token, company); 
    valueOperations.getOperations().expire(token, 5, TimeUnit.MINUTES); 
    return token; 
} 

錯誤:

org.springframework.data.redis.ClusterRedirectException:重定向:時隙7228〜10 :6379. *

redis.clients.jedis.exceptions.JedisMovedDataException:MOVED 7228 10. :6379. *

問題是 - 配置有什麼問題?

回答

0

您正在Redis集羣模式下運行Elasticache(僅Redis集羣響應MOVED),但連接工廠配置爲獨立模式。

Spring Boot可以自動配置您爲您手動設置的所有內容。基本上,刪除您CacheConfiguration類(或至少去除大部分的代碼):

@Configuration 
public class CacheConfiguration { 

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

,然後在application.properties文件中配置以下特性:默認和

spring.redis.cluster.nodes=XXXX-dev.XXXX.clusXXXcfg.XXX.cache.amazonaws.com # Comma-separated list of "host:port" pairs to bootstrap from. 

春季啓動負載application.properties默認情況下,Redis自動配置配置一個RedisTemplate<Object, Object> bean。專門的bean是一個有效的用例 - 不要複製auto-config已經提供的內容,特別是如果你想實現auto-config的功能。

參見:

+0

THX很多!它現在工作:) –