java Redis 集群与 Spring Boot 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41918560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:14:27  来源:igfitidea点击:

Redis cluster integration with Spring boot

javaspringcachingspring-bootredis

提问by Harshana

I have a redis cluster with master, slave and 3 sentinel servers. The master and slave is map to dns names as node1-redis-dev.com, node2-redis-dev.com. The redis server version is 2.8

我有一个带有主服务器、从服务器和 3 个哨兵服务器redis 集群。主从映射到 dns 名称为 node1-redis-dev.com, node2-redis-dev.com。redis 服务器版本为2.8

I include below in my application.properties file.

我在我的 application.properties 文件中包含以下内容。

spring.redis.cluster.nodes=node1-redis-dev.com:6379,node2-redis-dev.com:6379
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=-1
spring.redis.pool.max-wait=-1

But when I inspect the StringRedisTemplate, I see localhost instead of cluster information under hostName propertyof JedisConnectionFactory.

但是当我检查 StringRedisTemplate 时,我在JedisConnectionFactory 的hostName 属性下看到 localhost 而不是集群信息

Also I see the exception in creationStackTrace property of JedisPool.

我还看到 JedisPool 的 creationStackTrace 属性中的异常。

java.lang.Exception
    at org.apache.commons.pool2.impl.BaseGenericObjectPool.<init>(BaseGenericObjectPool.java:139)
    at org.apache.commons.pool2.impl.GenericObjectPool.<init>(GenericObjectPool.java:107)
    at redis.clients.util.Pool.initPool(Pool.java:43)
    at redis.clients.util.Pool.<init>(Pool.java:31)
    at redis.clients.jedis.JedisPool.<init>(JedisPool.java:80)
    at redis.clients.jedis.JedisPool.<init>(JedisPool.java:74)
    at redis.clients.jedis.JedisPool.<init>(JedisPool.java:55)
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.createRedisPool(JedisConnectionFactory.java:228)
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.createPool(JedisConnectionFactory.java:204)

Debug information

调试信息

The CasheRepository class looks like below,

CasheRepository 类如下所示,

@Component
@CacheConfig(cacheNames = "enroll", cacheManager = "enrollCM")
public class EnrollCashRepository {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;    
    //Other methods 
}

I am using spring boot 1.3.4 with spring-boot-starter-redis 1.2.7 which import jedis 2.7.3 dependency.

我正在使用 spring boot 1.3.4 和 spring-boot-starter-redis 1.2.7,它导入 jedis 2.7.3 依赖项。

What am I missing with integrate redis cluster with Spring boot applicatiom?

将 redis 集群与 Spring Boot 应用程序集成在一起,我缺少什么?

回答by Monzurul Haque Shimul

All that is needed is setting the initial collection of cluster nodes in RedisClusterConfiguration and provide that one to JedisConnectionFactory.

所需要做的就是在 RedisClusterConfiguration 中设置集群节点的初始集合,并将该集合提供给 JedisConnectionFactory。

@Configuration
class Config {

    List<String> clusterNodes = Arrays.asList("node1-redis-dev.com:6379", "node2-redis-dev.com:6379");

    @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);
    }
}