【问题标题】:How to config redis-cluster when use spring-data-redis 1.7.0.M1使用 spring-data-redis 1.7.0.M1 时如何配置 redis-cluster
【发布时间】:2016-05-30 00:58:24
【问题描述】:

我使用 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?

【问题讨论】:

  • 集群需要在RedisConnectionFactory上配置。能否请您为其添加配置。此外,在 github 上的 spring-data-examples 存储库以及 reference documentation 中还有一个 Redis Cluster 示例项目。

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


【解决方案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 集群支持的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2015-06-14
    • 2021-09-13
    • 1970-01-01
    • 2012-06-24
    • 2018-07-06
    • 1970-01-01
    相关资源
    最近更新 更多