spring-boot 速成(12) - 如何注入多个redis StringRedisTemplate

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,经济版 1GB 1个月
简介: 默认情况下,spring-boot的redis自动配置,只能注册一个StringRedisTemplate实例,如果希望注入多个,比如:1个读写database 0,1个读写database 1 ...

默认情况下,spring-boot的redis自动配置,只能注册一个StringRedisTemplate实例,如果希望注入多个,比如:1个读写database 0,1个读写database 1 ... ,默认的自动配置就不行了,可以参考下面的做法:

一、创建多实例配置类

  1 package cn.mwee.order.cloud.admin.common.config;
  2 
  3 import org.springframework.beans.factory.annotation.Value;
  4 import org.springframework.context.annotation.Bean;
  5 import org.springframework.context.annotation.Configuration;
  6 import org.springframework.context.annotation.Primary;
  7 import org.springframework.data.redis.connection.RedisConnectionFactory;
  8 import org.springframework.data.redis.connection.RedisSentinelConfiguration;
  9 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
 10 import org.springframework.data.redis.core.StringRedisTemplate;
 11 import org.springframework.data.redis.serializer.StringRedisSerializer;
 12 import redis.clients.jedis.JedisPoolConfig;
 13 
 14 import java.util.HashSet;
 15 import java.util.Set;
 16 
 17 /**
 18  * Created by 菩提树下的杨过(http://yjmyzz.cnblogs.com/) on 19/09/2017.
 19  */
 20 @Configuration
 21 public class RedisConfig {
 22 
 23     @Value("${redis.sentinel.group}")
 24     protected String sentinelGroupName;
 25 
 26     @Value("${redis.sentinel.nodes}")
 27     protected String sentinelNodes;
 28 
 29     @Value("${redis.maxTotal}")
 30     protected int maxTotal;
 31 
 32     @Value("${redis.minIdle}")
 33     protected int minIdle;
 34 
 35     @Value("${redis.maxIdle}")
 36     protected int maxIdle;
 37 
 38     @Value("${redis.maxWaitMillis}")
 39     protected long maxWaitMillis;
 40 
 41     @Value("${redis.testOnBorrow}")
 42     protected boolean testOnBorrow;
 43 
 44     @Value("${redis.testOnReturn}")
 45     protected boolean testOnReturn;
 46 
 47     @Value("${redis.password}")
 48     protected String password;
 49 
 50     @Value("${redis.timeout}")
 51     protected int timeout;
 52 
 53     @Bean
 54     public RedisSentinelConfiguration redisSentinelConfiguration() {
 55         String[] nodes = sentinelNodes.split(",");
 56         Set<String> setNodes = new HashSet<>();
 57         for (String n : nodes) {
 58             setNodes.add(n.trim());
 59         }
 60         RedisSentinelConfiguration configuration = new RedisSentinelConfiguration(sentinelGroupName, setNodes);
 61         return configuration;
 62     }
 63 
 64     @Bean
 65     public JedisPoolConfig jedisPoolConfig() {
 66         JedisPoolConfig poolConfig = new JedisPoolConfig();
 67         poolConfig.setMaxTotal(maxTotal);
 68         poolConfig.setMinIdle(minIdle);
 69         poolConfig.setMaxIdle(maxIdle);
 70         poolConfig.setMaxWaitMillis(maxWaitMillis);
 71         poolConfig.setTestOnBorrow(testOnBorrow);
 72         poolConfig.setTestOnReturn(testOnReturn);
 73         return poolConfig;
 74     }
 75 
 76     @Bean
 77     public StringRedisSerializer stringRedisSerializer() {
 78         return new StringRedisSerializer();
 79     }
 80 
 81     @Bean(name = "redisTemplate00")
 82     @Primary
 83     public StringRedisTemplate redisTemplate00() {
 84         return buildRedisTemplate(buildConnectionFactory(0));
 85     }
 86 
 87     @Bean(name = "redisTemplate01")
 88     public StringRedisTemplate redisTemplate01() {
 89         return buildRedisTemplate(buildConnectionFactory(1));
 90     }
 91 
 92     @Bean(name = "redisTemplate02")
 93     public StringRedisTemplate redisTemplate02() {
 94         return buildRedisTemplate(buildConnectionFactory(2));
 95     }
 96 
 97     @Bean(name = "redisTemplate03")
 98     public StringRedisTemplate redisTemplate03() {
 99         return buildRedisTemplate(buildConnectionFactory(3));
100     }
101 
102     @Bean(name = "redisTemplate04")
103     public StringRedisTemplate redisTemplate04() {
104         return buildRedisTemplate(buildConnectionFactory(4));
105     }
106 
107     private JedisConnectionFactory buildConnectionFactory(int database) {
108         JedisConnectionFactory connectionFactory = new JedisConnectionFactory(redisSentinelConfiguration(), jedisPoolConfig());
109         connectionFactory.setUsePool(true);
110         connectionFactory.setTimeout(timeout);
111         connectionFactory.setDatabase(database);
112         connectionFactory.setPassword(password);
113         connectionFactory.afterPropertiesSet();
114         return connectionFactory;
115     }
116 
117     protected StringRedisTemplate buildRedisTemplate(RedisConnectionFactory connectionFactory) {
118         StringRedisTemplate template = new StringRedisTemplate();
119         template.setConnectionFactory(connectionFactory);
120         template.setValueSerializer(stringRedisSerializer());
121         template.afterPropertiesSet();
122         return template;
123     }
124 
125 }
View Code

 

上面的示例,注入了5个redisTemplate实例,分别对应redis的0到4,共5个database。

 

二、配置类application.yml

redis:
  sentinel:
    group: ${redis.sentinel.group}
    nodes: ${redis.sentinel.nodes}
  maxTotal: ${redis.maxTotal}
  minIdle: ${redis.minIdle}
  maxWaitMillis: ${redis.maxWait}
  testOnBorrow: ${redis.testOnBorrow}
  testOnReturn: ${redis.testOnReturn}
  password:
  timeout: ${redis.timeout}  

  大家把里面的占位符,换成具体值就可以了。

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
25天前
|
存储 NoSQL Java
在Spring Boot中使用Redis生成订单号,并且保证当天有效性
在Spring Boot中使用Redis生成订单号,并且保证当天有效性
40 4
|
8天前
|
缓存 NoSQL Java
Redis Spring配置集群
【7月更文挑战第5天】
36 10
|
18天前
|
Java Spring 容器
spring如何进行依赖注入,通过set方法把Dao注入到serves
spring如何进行依赖注入,通过set方法把Dao注入到serves
|
18天前
|
NoSQL Java 应用服务中间件
蓝易云 - Spring redis使用报错Read timed out排查解决
以上都是可能的解决方案,具体的解决方案可能会因具体情况而异。
18 1
|
20天前
|
NoSQL Java 应用服务中间件
蓝易云 - Spring redis使用报错Read timed out排查解决
以上都是可能的解决方案,具体的解决方案可能会因具体情况而异。
13 2
|
25天前
|
监控 NoSQL Java
在 Spring Boot 中实现 Redis 的发布/订阅功能可以通过 RedisTemplate 和消息监听器来完成
在 Spring Boot 中实现 Redis 的发布/订阅功能可以通过 RedisTemplate 和消息监听器来完成
23 1
|
10天前
|
JSON NoSQL Java
Redis18的Java客户端-StringRedisTemplate,序列化存在的问题,使用StringRedisTemplate解决序列化的方法
Redis18的Java客户端-StringRedisTemplate,序列化存在的问题,使用StringRedisTemplate解决序列化的方法
|
14天前
|
缓存 NoSQL Java
Spring Boot整合Redis缓存的最佳实践
Spring Boot整合Redis缓存的最佳实践
|
17天前
|
缓存 NoSQL Java
Spring Boot与Redis的缓存一致性问题
Spring Boot与Redis的缓存一致性问题
|
17天前
|
消息中间件 NoSQL Java
Spring Boot中使用Redis和Lua脚本实现延时队列
Spring Boot中使用Redis和Lua脚本实现延时队列