Shahbaz Ahmad
Shahbaz Ahmad

Reputation: 201

Connections to multiple Redis servers with Spring Data Redis

I'm working on an application(Spring) with the following requirements:

AND

Can someone give us a thought to connect to different Redis servers using Spring Data Redis.

Got a link: http://forum.spring.io/forum/spring-projects/data/nosql/104599-how-to-connect-to-multiple-redis-instances-using-redistemplate?view=stream

But that's too old.

Any help would be appreciated.

Upvotes: 3

Views: 6740

Answers (1)

mp911de
mp911de

Reputation: 18137

There's not out-of-the-box support for accessing multiple servers at once but you can get there yourself.

Usually, you would use RedisTemplate to interact with Redis. RedisTemplate uses RedisConnectionFactory to obtain a connection per requests. You can implement RedisConnectionFactory yourself and dispatch getConnection() calls to the connection factory that is configured with your server. A Map<String, RedisConnectionFactory> can hold multiple connection factories. You would dispatch by a custom discriminator (usually something that you set on ThreadLocal level).

Spring Framework provides something similar for JDBC with AbstractRoutingDataSource. The code at GitHub should give you an approach how to implement a routing RedisConnectionFactory.

Upvotes: 2

Related Questions