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

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

一、创建多实例配置类

spring-boot 速成(12) - 如何注入多个redis StringRedisTemplatespring-boot 速成(12) - 如何注入多个redis StringRedisTemplate
  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}  

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

上一篇:为什么的inital-scale属性无效


下一篇:PHP strtotime -1 month 获取上个月月份踩坑