问题
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.
回答1:
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
.
来源:https://stackoverflow.com/questions/43514076/connections-to-multiple-redis-servers-with-spring-data-redis