小编典典

SpringBoot Elasticache JedisMovedDataException:已移动

spring-boot

尝试将SpringBoot与带有Elasticache的SpringData结合使用:

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.

问题是-配置有什么问题?


阅读 1143

收藏
2020-05-30

共1个答案

小编典典

您正在Redis Cluster模式下运行Elasticache(仅Redis Cluster响应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=<node_host>:<port> # Comma-separated list of "host:port" pairs to bootstrap from.

application.properties默认情况下,Spring Boot会加载,Redis自动配置会RedisTemplate<Object, Object>默认配置Bean。对bean进行专门化是一个有效的用例–请勿复制自动配置已提供的功能,尤其是要实现自动配置的功能时。

也可以看看:

2020-05-30