skip to Main Content

I am trying to connect to Redis Cluster using Spring Data Redis. Below are is how I configured it.

import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
public class RedisConfig {
 
    @Bean
    public JedisConnectionFactory jedisConnectionFactory(){
        RedisStandaloneConfiguration config= new RedisStandaloneConfiguration("server",portno);
        config.setPassword("password");
        return new JedisConnectionFactory(config);
    }

    @Bean
    public LettuceConnectionFactory lettuceConnectionFactory() {
        RedisStandaloneConfiguration config= new RedisStandaloneConfiguration("server",portno);
        config.setPassword("password");
        return new LettuceConnectionFactory(config);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(){
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        return redisTemplate;
    }
}

I have kept it pretty basic as per the documentation and avoided explicitly as Pool configs as such. I am trying to test if I did connect properly and start with redis operations using below Service class.

@Service
public class RedisListCache {
    private RedisTemplate<String, Object> redisTemplate;
    private ListOperations<String, Object> listOperations;

    public  RedisListCache(RedisTemplate<String, Object> redisTemplate){
        this.redisTemplate=redisTemplate;
        listOperations=redisTemplate.opsForList();
    }

    @PostConstruct
    public void setup(){
        listOperations.leftPush("keytest","Redistesting");
        System.out.println(listOperations.rightPop("keytest"));
    }
}

Now I am unable to connect to redis. Posting partial logs below

Caused by: io.lettuce.core.RedisCommandTimeoutException: Connection initialization timed out. Command timed out after 1 minute(s)
    at io.lettuce.core.internal.ExceptionFactory.createTimeoutException(ExceptionFactory.java:71) ~[lettuce-core-6.1.9.RELEASE.jar:6.1.9.RELEASE]
    at io.lettuce.core.protocol.RedisHandshakeHandler.lambda$channelRegistered$0(RedisHandshakeHandler.java:62) ~[lettuce-core-6.1.9.RELEASE.jar:6.1.9.RELEASE]
    at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98) ~[netty-common-4.1.70.Final.jar:4.1.70.Final]
    at io.netty.util.concurrent.PromiseTask.run(PromiseTask.java:106) ~[netty-common-4.1.70.Final.jar:4.1.70.Final]
    at io.netty.util.concurrent.DefaultEventExecutor.run(DefaultEventExecutor.java:66) ~[netty-common-4.1.70.Final.jar:4.1.70.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986) ~[netty-common-4.1.70.Final.jar:4.1.70.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.70.Final.jar:4.1.70.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.70.Final.jar:4.1.70.Final]

I am fairly new to in-memory data store and caching so please let me know on any inputs to get this working.

2

Answers


  1. Chosen as BEST ANSWER

    I could make a successful connection to redis using LettuceConnectionFactory by setting ssl to true and VerifyPeer to false.

    @Bean
        public LettuceConnectionFactory lettuceConnectionFactory() {
            LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
            lettuceConnectionFactory.setHostName("hostname");
            lettuceConnectionFactory.setPort(port);
            lettuceConnectionFactory.setPassword("yourpassword");
            lettuceConnectionFactory.setUseSsl(true);
            lettuceConnectionFactory.setVerifyPeer(false);
            return lettuceConnectionFactory;
        }
    
    

  2. I assume you are using Spring boot for this tryout.
    Spring boot by default uses lettuce client.So all you have to provide is redis connection details in application.properties/yaml and create RedisTemplate Bean with RedisConnectionFactory instance.

    RedisConfig.java

    @Configuration
    public class RedisConfig {
    
    @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){
            RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
            redisTemplate.setConnectionFactory(factory);
            return redisTemplate;
        }
      }
    

    Application.properties:

    spring.redis.database=0
    spring.redis.host=localhost
    spring.redis.port=6379
    spring.redis.password=mypass
    

    Make sure the Redis server is running in aforementioned server/port and use redisTemplate bean in your service layer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search