使用Spring Data Redis连接到多个Redis服务器 [英] Connections to multiple Redis servers with Spring Data Redis
问题描述
我正在处理具有以下要求的应用程序(Spring):
I'm working on an application(Spring) with the following requirements:
- 从Redis Server1读取数据
- 从Redis Server2读取数据
- 从Redis Server3读取数据
AND
- 将信息保存到MySQL。
有人可以考虑使用Spring Data Redis连接到不同的Redis服务器。
Can someone give us a thought to connect to different Redis servers using Spring Data Redis.
但那太旧了。
任何帮助将不胜感激。
Any help would be appreciated.
推荐答案
对于一次访问多个服务器没有开箱即用的支持,但您可以自己到达那里。
There's not out-of-the-box support for accessing multiple servers at once but you can get there yourself.
通常,你会使用 RedisTemplate
与Redis互动。 RedisTemplate
使用 RedisConnectionFactory
获取每个请求的连接。您可以自己实现 RedisConnectionFactory
并调度 getConnection()
对您的服务器配置的连接工厂的调用。 Map< String,RedisConnectionFactory>
可以容纳多个连接工厂。您将通过自定义鉴别器(通常是您在 ThreadLocal
级别设置的内容)进行调度。
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为JDBC提供类似的东西 AbstractRoutingDataSource
。 GitHub上的代码应该为您提供一种如何实现路由的方法 RedisConnectionFactory
。
Spring Framework provides something similar for JDBC with AbstractRoutingDataSource
. The code at GitHub should give you an approach how to implement a routing RedisConnectionFactory
.
这篇关于使用Spring Data Redis连接到多个Redis服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!