Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis cluster integration with Spring boot

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

I include below in my application.properties file.

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 property of JedisConnectionFactory.

Also I see the exception in creationStackTrace property of JedisPool.

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,

@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.

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

like image 727
Harshana Avatar asked Feb 06 '23 05:02

Harshana


2 Answers

All that is needed is setting the initial collection of cluster nodes in RedisClusterConfiguration and provide that one to 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);
    }
}
like image 60
Monzurul Shimul Avatar answered Feb 07 '23 18:02

Monzurul Shimul


The following should work

application.properties

spring.redis.cluster.nodes=127.0.0.1:6379
spring.redis.cluster.max-redirects=3

ClusterConfigurationProperties.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class ClusterConfigurationProperties {

/*
 * spring.redis.cluster.nodes[0] = 127.0.0.1:7379 spring.redis.cluster.nodes[1]
 * = 127.0.0.1:7380 ...
 */
private List<String> nodes;
/**
 * spring.redis.cluster.max-redirects=3
 */
private int maxRedirects;

/**
 * Get initial collection of known cluster nodes in format {@code host:port}.
 *
 * @return
 */
public List<String> getNodes() {
    return nodes;
}

public void setNodes(List<String> nodes) {
    this.nodes = nodes;
}

public int getMaxRedirects() {
    return maxRedirects;
}

public void setMaxRedirects(int maxRedirects) {
    this.maxRedirects = maxRedirects;
}

}

RedisConfig.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import io.lettuce.core.ReadFrom;

@Configuration
public class RedisConfig {

@Autowired
private ClusterConfigurationProperties clusterProperties;

@Bean
LettuceConnectionFactory redisConnectionFactory(RedisClusterConfiguration redisConfiguration) {

    LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
            .readFrom(ReadFrom.REPLICA_PREFERRED).build();

    return new LettuceConnectionFactory(redisConfiguration, clientConfig);
}

@Bean
RedisClusterConfiguration redisConfiguration() {
    RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(clusterProperties.getNodes());
    redisClusterConfiguration.setMaxRedirects(clusterProperties.getMaxRedirects());

    return redisClusterConfiguration;
}

@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
@Primary
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    template.setKeySerializer(new StringRedisSerializer());
    // other settings...
    return template;
}
}
like image 41
craftsmannadeem Avatar answered Feb 07 '23 19:02

craftsmannadeem