使用莴苣的Redis Spring数据:com.lambdaworks.redis.RedisCommandExecutionException:MOVED错误

时间:2018-11-20 22:07:52

标签: spring-data amazon-elasticache spring-data-redis lettuce

我在集群模式下使用AWS ElastiCache(Redis)。我有两个连接到ElastiCache的实现。其中一种实现是直接使用本机的Lettuce驱动程序,而另一种实现是使用带有Lettuce作为底层驱动程序的Spring数据。 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)我使用群集节点端点,并通过LettuceConnectionFactory注入RedisClusterConfiguration
2)如果我使用StatefulRedisClusterConnection将Lettuce用作直接实现(不是通过Spring数据)。

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

1 个答案:

答案 0 :(得分:0)

以下是使用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