Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot Elasticache JedisMovedDataException: MOVED

Trying to use SpringBoot with SpringData with Elasticache:

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();
}

}

Service call:

@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;
}

Error:

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

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

The question is - what is wrong with configuration?

like image 583
Dmitry Makarichev Avatar asked Oct 10 '17 13:10

Dmitry Makarichev


1 Answers

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 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;
  }
}

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 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.

See also:

  • Common application properties
  • Externalized Configuration
like image 52
mp911de Avatar answered Sep 28 '22 06:09

mp911de