如何使用 spring-data-redis 连接到多个 redis 实例 [英] How to connect to multiple redis instances with spring-data-redis
问题描述
我正在尝试将一个 springboot 应用程序连接到 2 个不同的 redis 实例:一个用作数据库,一个仅用作缓存.我添加了不同名称的不同连接工厂和 redis 模板,我正在使用 @Qualifier 来链接它们.我试图从自动配置中禁用类 RedisAutoConfiguration,但没有任何效果.
I'm trying to connect with one springboot application to 2 different redis instances: one used as database and one used as cache only. I added different connection factories and redis templates with different names and I'm using @Qualifier to link them. I tried to disable from auto configuration the class RedisAutoConfiguration, but nothing works.
我总是收到这个错误:
Wrapped by: org.springframework.beans.factory.UnsatisfiedDependencyException: 在类路径资源 [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration$RedisConfiguration.class] 中定义名称为redisTemplate"的 bean 创建时出错:不满意通过构造函数参数表示的依赖关系为 [org.springframework.data.redis.connection.RedisConnectionFactory] 类型的索引 0:没有定义 [org.springframework.data.redis.connection.RedisConnectionFactory] 类型的合格 bean:预期的单个匹配 bean 但发现 2: redisCacheFactory,redisJitFactory;嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义 [org.springframework.data.redis.connection.RedisConnectionFactory] 类型的合格 bean:预期的单个匹配 bean 但找到 2:redisCacheFactory,redisJitFactory
Wrapped by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration$RedisConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.data.redis.connection.RedisConnectionFactory]: No qualifying bean of type [org.springframework.data.redis.connection.RedisConnectionFactory] is defined: expected single matching bean but found 2: redisCacheFactory,redisJitFactory; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.data.redis.connection.RedisConnectionFactory] is defined: expected single matching bean but found 2: redisCacheFactory,redisJitFactory
您能否就如何实现这一点给我任何提示?
Can you give me any hint on how is possible to implement this?
提前致谢!
推荐答案
问题是将 connectionFactory 提取为 bean.如果在模板 bean 中声明它可以正常工作.以下对我有用:
The problem is extracting the connectionFactory as beans. If you declare it inside template bean works properly. The following works for me:
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:defaultSerializer-ref="stringRedisSerializer">
<property name="connectionFactory">
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.ip}" p:port="6379" p:use-pool="true"/>
</property>
</bean>
<bean id="redisTemplate2" class="org.springframework.data.redis.core.RedisTemplate"
p:defaultSerializer-ref="stringRedisSerializer">
<property name="connectionFactory">
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.ip2}" p:port="6379" p:use-pool="true"/>
</property>
</bean>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
这篇关于如何使用 spring-data-redis 连接到多个 redis 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!