Search code examples
javaspring-bootcachingspring-dataspring-data-redis

Connection to Redis Cache using Spring Data Redis


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.


Solution

  • 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;
        }