How to connect to multiple redis instances with spring-data-redis

与世无争的帅哥 提交于 2019-12-08 07:30:57

问题


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.

I always receive this error:

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?

Thanks in advance!


回答1:


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"/>


来源:https://stackoverflow.com/questions/36621090/how-to-connect-to-multiple-redis-instances-with-spring-data-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!