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

标签 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?

最佳答案

基本上,所需要做的就是在 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 集群支持的示例。

关于redis - 使用 spring-data-redis 1.7.0.M1 时如何配置 redis-cluster,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35471666/

相关文章:

node.js - _http_server.js :192 throw new RangeError (`Invalid status code: ${statusCode}` );

ubuntu - nmap 忽略 --script 参数

spring-boot - 如何在 Spring Boot 应用程序中创建第二个 RedisTemplate 实例

java - JedisConnectionException - 读取超时错误

node.js - node_redis(npm redis模块)支持redis集群模式还是必须使用ioredis?

redis - 当您不在 Redis 中取消监视 WATCH 的键时会发生什么

redis - 如何知道redis的master/slave状态?

python - rq 入队函数 : fails with TypeError:function_xxx() takes 2 positional arguments but 3 were given

spring-boot - spring boot 数据 redis 存储库 @Id 注释 vs jpa 存储库 @Id

java - Spring data Redis 到 Elastic Cache 连接 list