使用 spring-data-redis 1.7.0.M1 时如何配置 redis-cluster [英] How to config redis-cluster when use spring-data-redis 1.7.0.M1
问题描述
我使用的是 spring-data-redis 1.7.0.M1 版和 jedis 2.8.0 版这是我的配置
I use spring-data-redis version 1.7.0.M1,and jedis version 2.8.0 Here is my configuration
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"></property>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean>
并使用【redisTemplate.opsForValue().get("foo")】进行测试
and use 【redisTemplate.opsForValue().get("foo")】 to test
抛出异常
org.springframework.dao.InvalidDataAccessApiUsageException: MOVED 12182 192.168.1.223:7002; nested exception is redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 12182 192.168.1.223:7002
使用spring-data-redis 1.7.0.M1时如何配置redis-cluster?
How to config redis-cluster when use spring-data-redis 1.7.0.M1?
推荐答案
基本上所有需要的是在 RedisClusterConfiguration
中设置集群节点的初始集合并将其提供给 JedisConnectionFactory
或 LettuceConnectionFactory
.
Basically all that is needed is setting the inital collection of cluster nodes in RedisClusterConfiguration
and provide that one to JedisConnectionFactory
or LettuceConnectionFactory
.
@Configuration
class Config {
List<String> clusterNodes = Arrays.asList("127.0.0.1:30001", "127.0.0.1:30002", "127.0.0.1:30003");
@Bean
RedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory(new RedisClusterConfiguration(clusterNodes));
}
@Bean
RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
// just used StringRedisTemplate for simplicity here.
return new StringRedisTemplate(factory);
}
}
Spring Boot 将提供配置属性(spring.redis.cluster.nodes
, spring.redis.cluster.max-redirects
),以便在接下来的 Redis 集群中工作释放.请参阅 commit/166a27 了解详情.
Spring Boot will provide configuration properties (spring.redis.cluster.nodes
, spring.redis.cluster.max-redirects
) for working with Redis cluster in the next release.
See commit/166a27 for details.
spring-data-examples 存储库 已经包含 Spring Data Redis 集群支持的示例.
The spring-data-examples repository already contains an example of Spring Data Redis cluster support.
这篇关于使用 spring-data-redis 1.7.0.M1 时如何配置 redis-cluster的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!