SpringBoot Elasticache JedisMovedDataException:MOVED
SpringBoot Elasticache JedisMovedDataException: MOVED
尝试将 SpringBoot 与 SpringData 与 Elasticache 一起使用:
application.properties:
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:重定向:插槽 7228 到 10...:6379.*
redis.clients.jedis.exceptions.JedisMovedDataException:移动 7228 10...:6379.*
问题是 - 配置有什么问题?
您是 运行 Redis 集群模式下的 Elasticache(只有 Redis 集群响应 MOVED
),但连接工厂配置为独立模式。
Spring Boot 可以自动配置您手动为您设置的所有内容。基本上,删除您的 CacheConfiguration
class(或至少删除大部分代码):
@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.
Spring 引导默认加载 application.properties
,Redis 自动配置默认配置 RedisTemplate<Object, Object>
bean。专门化 beans 是一个有效的用例——不要复制自动配置已经提供的内容,特别是如果你想实现自动配置的功能。
另请参阅:
尝试将 SpringBoot 与 SpringData 与 Elasticache 一起使用:
application.properties:
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:重定向:插槽 7228 到 10...:6379.*
redis.clients.jedis.exceptions.JedisMovedDataException:移动 7228 10...:6379.*
问题是 - 配置有什么问题?
您是 运行 Redis 集群模式下的 Elasticache(只有 Redis 集群响应 MOVED
),但连接工厂配置为独立模式。
Spring Boot 可以自动配置您手动为您设置的所有内容。基本上,删除您的 CacheConfiguration
class(或至少删除大部分代码):
@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.
Spring 引导默认加载 application.properties
,Redis 自动配置默认配置 RedisTemplate<Object, Object>
bean。专门化 beans 是一个有效的用例——不要复制自动配置已经提供的内容,特别是如果你想实现自动配置的功能。
另请参阅: