RedisTemplate如何连接两个redis?

qq_28904659 2016-06-01 03:04:49
框架是SpringMvc,有两台装有redis的机子,RedisTemplate的话怎么配置连接两台redis?
...全文
2463 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
特里12 2017-05-09
  • 打赏
  • 举报
回复
如果是主从的话,怎么配啊?有没有办法判断当前连接的是哪个redis实例?
Gavin~ 2017-03-21
  • 打赏
  • 举报
回复
楼主你可以运行?为什么我的报错
baidu_36893818 2016-12-01
  • 打赏
  • 举报
回复
我配置两个数据源报错 Caused by: 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: jedisConnectionFactory,jedisConnectionFactory1 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE] ... 42 common frames omitted
baidu_36893818 2016-12-01
  • 打赏
  • 举报
回复
你好,我想问下你的两个数据源怎么配置的
qq_28904659 2016-06-01
  • 打赏
  • 举报
回复
谢谢,非常感谢!
oxcow 2016-06-01
  • 打赏
  • 举报
回复


<bean id="redisTemplate1" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>

<bean id="redisTemplate2" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>



@Resource(name = "redisTemplate1")
RedisTemplate redisTemplate1;

@Resource(name = "redisTemplate2")
RedisTemplate redisTemplate2;

或者

@Autowired
@Qualifier("redisTemplate1")
RedisTemplate redisTemplate1;

@Autowired
@Qualifier("redisTemplate2")
RedisTemplate redisTemplate2;
oxcow 2016-06-01
  • 打赏
  • 举报
回复
引用 4 楼 qq_28904659 的回复:
配置两个数据源后 @Autowired RedisTemplate redisTemplate; redisTemplate.opsForValue(); 这个用的那个数据源?
你既然有2哦不同的redisTemple 那你注入的时候就需要指明用那一个。用
@Qualifie
注明下
oxcow 2016-06-01
  • 打赏
  • 举报
回复
引用 3 楼 oxcow 的回复:
[quote=引用 2 楼 qq_28904659 的回复:] 不是主从关系,是两个实例。我举个假设吧,我在阿里云有两个机器IP分别 为 A:120.120.120.120 B:121.121.121.121 都装的redis,有个项目要同时用到这两部机器的redis,怎么配?我使用RedisTemplate来操作redis的,怎么用RedisTemplate操作两个不同的redis呢?
那说白了就是你有2个数据源了! 你配置2个RedisTemplate就行了,然后根据需要使用不同的就行了[/quote] 可以看下 http://docs.spring.io/spring-data/redis/docs/1.6.4.RELEASE/reference/html/#redis:template
qq_28904659 2016-06-01
  • 打赏
  • 举报
回复
配置两个数据源后 @Autowired RedisTemplate redisTemplate; redisTemplate.opsForValue(); 这个用的那个数据源?
oxcow 2016-06-01
  • 打赏
  • 举报
回复
引用 2 楼 qq_28904659 的回复:
不是主从关系,是两个实例。我举个假设吧,我在阿里云有两个机器IP分别 为 A:120.120.120.120 B:121.121.121.121 都装的redis,有个项目要同时用到这两部机器的redis,怎么配?我使用RedisTemplate来操作redis的,怎么用RedisTemplate操作两个不同的redis呢?
那说白了就是你有2个数据源了! 你配置2个RedisTemplate就行了,然后根据需要使用不同的就行了
qq_28904659 2016-06-01
  • 打赏
  • 举报
回复
不是主从关系,是两个实例。我举个假设吧,我在阿里云有两个机器IP分别 为 A:120.120.120.120 B:121.121.121.121 都装的redis,有个项目要同时用到这两部机器的redis,怎么配?我使用RedisTemplate来操作redis的,怎么用RedisTemplate操作两个不同的redis呢?
oxcow 2016-06-01
  • 打赏
  • 举报
回复
引用 楼主 qq_28904659 的回复:
框架是SpringMvc,有两台装有redis的机子,RedisTemplate的话怎么配置连接两台redis?
两个redis实例? 这两个实例之间有关系没?主从还是怎么的? 还是说你只是想配置连接两个redis? 如果是这种情况的话 直接配置2套不就行了。

<bean id="lettuceConnectionFactory"
          class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory"
          p:hostName="${redis.server.host}"
          p:port="${redis.server.port}"/>

    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
          p:connectionFactory-ref="lettuceConnectionFactory"/>

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧