本文介绍了SpringBoot Elasticache JedisMovedDataException: MOVED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to use SpringBoot with SpringData with Elasticache:

应用程序属性:

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

缓存配置:

@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: Redirect: slot 7228 to 10...:6379.*

org.springframework.data.redis.ClusterRedirectException: Redirect: slot 7228 to 10...:6379.*

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

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

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

The question is - what is wrong with configuration?

推荐答案

您在 Redis 集群模式下运行 Elasticache(只有 Redis 集群以 MOVED 响应)但连接工厂配置为独立模式模式.

You're running your Elasticache in Redis Cluster mode (only Redis Cluster responds with MOVED) but the connection factory is configured in standalone mode.

Spring Boot 可以自动配置您手动设置的所有内容.基本上,删除您的 CacheConfiguration 类(或至少删除大部分代码):

Spring Boot can auto-configure all the things you've set up manually for you. Basically, remove your CacheConfiguration class (or at least remove the majority of code):

@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 文件中配置以下属性:

And then configure the following properties in your application.properties file:

spring.redis.cluster.nodes=<node_host>:<port> # Comma-separated list of "host:port" pairs to bootstrap from.

Spring Boot 默认加载 application.properties,Redis 自动配置默认配置一个 RedisTemplate bean.专门化 bean 是一个有效的用例 - 不要重复自动配置已经提供的内容,特别是如果您想实现自动配置的功能.

Spring Boot loads application.properties by default and the Redis auto-config configures a RedisTemplate<Object, Object> bean by default. Specializing beans is a valid use-case – do not duplicate what's already provided by the auto-config, especially if you want to achieve what auto-config does.

另见:

这篇关于SpringBoot Elasticache JedisMovedDataException: MOVED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 12:31