Redis Spring数据与生菜:com.lambdaworks.Redis.RedisCommandExecutio



我在集群模式下使用AWS ElastiCache(Redis(。我有两个连接到ElastiCache的实现。其中一个实现是直接使用本地Lettuce驱动程序,另一个实现使用Spring数据,Lettuce作为底层驱动程序。AWS ElastiCache具有群集配置终结点。我想使用此端点连接到ElastiCache。我能够使用本地Lettuce驱动程序实现的集群端点成功连接到ElastiCache,但在使用集群端点的spring数据时出现以下错误

Spring数据:1.8.9-RELEASE(不能选择使用更高版本的Spring(

生菜:4.5.0-最终

@Bean
public LettuceConnectionFactory redisConnectionFactory() {
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
lettuceConnectionFactory.setHostName(<cluster_endpoint>);
lettuceConnectionFactory.setPort();
lettuceConnectionFactory.setUseSsl(Boolean.valueOf(useSsl));
//lettuceConnectionFactory.setPassword(password);
return lettuceConnectionFactory;
}

错误:

Caused by: org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is com.lambdaworks.redis.RedisCommandExecutionException: MOVED 12894 cache---.usw2.cache.amazonaws.com:6379
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:50)
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:48)
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41)

它在以下两种情况下都很好-
1( 我使用集群节点端点,并通过RedisClusterConfiguration注入LettuceConnectionFactory
2( 如果我使用Lettuce作为使用StatefulRedisClusterConnection的直接实现(而不是通过Spring数据(。

造成上述两个错误的原因是什么?我更喜欢通过使用集群配置端点将Lettuce与Spring数据一起使用。

以下是使用Spring Data-连接到Elasticache Redis集群的解决方案

使用ElastiCache集群配置端点:

@Bean
public RedisClusterConfiguration redisClusterConfiguration() {
RedisClusterConfiguration clusterConfiguration = new 
RedisClusterConfiguration();
clusterConfiguration.clusterNode("host", port);
new LettuceConnectionFactory(clusterConfiguration);
}

使用ElastiCache节点端点:

@Bean
public RedisClusterConfiguration redisClusterConfiguration() {
RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration()
.clusterNode("redis-cluster----0001-001.redis-cluster---.usw2.cache.amazonaws.com",6379)
.clusterNode("redis-cluster----0001-002.redis-cluster---.usw2.cache.amazonaws.com",6379)
.clusterNode("redis-cluster----0001-003.redis-cluster---.usw2.cache.amazonaws.com",6379)
.clusterNode("redis-cluster----0001-004.redis-cluster---.usw2.cache.amazonaws.com",6379);
return clusterConfiguration;
}

感谢Mark Paluch,他在Spring Data论坛上回复了我的问题。以下是详细信息-https://jira.spring.io/browse/DATAREDIS-898

最新更新