首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用spring-data-redis 1.7.0.M1时如何配置redis-cluster

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

Stack Overflow用户
提问于 2016-02-18 10:26:43
回答 1查看 11.4K关注 0票数 4

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

代码语言:javascript
复制
<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”)]测试

抛出异常

代码语言:javascript
复制
 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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-19 17:57:52

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

代码语言:javascript
复制
@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将在下一版本中提供使用Redis集群的配置属性(spring.redis.cluster.nodesspring.redis.cluster.max-redirects)。详情请参见commit/166a27

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35471666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档