使用spring-data-redis 1.7.0.M1时如何配置redis-cluster

时间:2016-02-18 02:26:43

标签: redis spring-data-redis redis-cluster

我使用spring-data-redis版本1.7.0.M1和jedis版本2.8.0 这是我的配置

                                                                                      

<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”)]来测试

抛出异常

 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?

1 个答案:

答案 0 :(得分:1)

基本上所需要的只是在RedisClusterConfiguration中设置群集节点的初始集合,并将其提供给JedisConnectionFactoryLettuceConnectionFactory

@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.nodesspring.redis.cluster.max-redirects),以便在下一版本中使用Redis群集。 有关详细信息,请参阅commit/166a27

spring-data-examples repository已包含Spring Data Redis群集支持的示例。