3

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

  • Read data from Redis Server1
  • Read data from Redis Server2
  • Read data from Redis Server3

AND

  • Save the information to MySQL.

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.

mp911de
  • 17,546
  • 2
  • 55
  • 95
Shahbaz Ahmad
  • 191
  • 2
  • 3
  • 10
  • @_Shahbaz Ahmad_ @mp911de I need the structure like yours, But I can't implement that how to set related redis endpoint to RedisTemplate. Can you share your implementation if you implement it? – Samet Baskıcı Jul 26 '18 at 07:19
  • We moved out from spring and created a library very similar to mp911de answer. The lib is in core java and storing the connection of each redis instance. The instances are given static namse. Applications passes instance name and information/command to store/execute. – Shahbaz Ahmad Jul 27 '18 at 07:31

1 Answers1

2

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.

mp911de
  • 17,546
  • 2
  • 55
  • 95