Set connection parameters in RedisConnectionFactory

I have simple Redis client based on Spring below. As default it connects to local host, but I need to set different host and port.

I suppose I can do it in function below:

@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {
    //LettuceConnectionFactory(connectionFactory);
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.addMessageListener(listenerAdapter, new PatternTopic("chat"));

    return container;
}

I found that I class name of connectionFactory is LettuceConnectionFactory and it has private field client that contains connection information.

What is correct way to change default connection parameters in RedisConnectionFactory?

Whole code:

@SpringBootApplication
public class MessagingRedisApplication {

    private static final Logger lg = LoggerFactory.getLogger(MessagingRedisApplication.class);

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {
        LettuceConnectionFactory(connectionFactory)
        lg.info( connectionFactory.toString() );
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("chat"));

        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    @Bean
    Receiver receiver() {
        return new Receiver();
    }

    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }

    public static void main(String[] args) throws InterruptedException {

        ApplicationContext ctx = SpringApplication.run(MessagingRedisApplication.class, args);

        StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);

        Receiver receiver = ctx.getBean(Receiver.class);

        while (receiver.getCount() == 0) {

            lg.info("Sending message...");

            template.convertAndSend("chat", "Hello from Redis!");
            Thread.sleep(500L);
        }

        System.exit(0);
    }
}

2 answers

  • answered 2020-10-20 18:23 Shawrup

    You should use default spring provided properties to configure your redis connections. For host and port, the followging properties should be enough.

    spring.redis.host=redis.yourhost.com
    spring.redis.port=6379
    

    Then the connectionfactory bean will created with the given host and port. The whole set of properties are available here. Search with spring.redis.

  • answered 2020-10-20 18:26 michalk

    If you want to configure host name programatically instead of using spring.redis properties you can create your own RedisConnectionFactory and pass RedisConfiguration to it's constructor :

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisConfiguration redisConfiguration = new RedisStandaloneConfiguration("myHostName");
        return new LettuceConnectionFactory(redisConfiguration);
    }
    

    In this case I assume you are using single node instance so RedisStandaloneConfiguration is used.