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

一、创建多实例配置类

 package cn.mwee.order.cloud.admin.common.config;

 import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig; import java.util.HashSet;
import java.util.Set; /**
* Created by 菩提树下的杨过(http://yjmyzz.cnblogs.com/) on 19/09/2017.
*/
@Configuration
public class RedisConfig { @Value("${redis.sentinel.group}")
protected String sentinelGroupName; @Value("${redis.sentinel.nodes}")
protected String sentinelNodes; @Value("${redis.maxTotal}")
protected int maxTotal; @Value("${redis.minIdle}")
protected int minIdle; @Value("${redis.maxIdle}")
protected int maxIdle; @Value("${redis.maxWaitMillis}")
protected long maxWaitMillis; @Value("${redis.testOnBorrow}")
protected boolean testOnBorrow; @Value("${redis.testOnReturn}")
protected boolean testOnReturn; @Value("${redis.password}")
protected String password; @Value("${redis.timeout}")
protected int timeout; @Bean
public RedisSentinelConfiguration redisSentinelConfiguration() {
String[] nodes = sentinelNodes.split(",");
Set<String> setNodes = new HashSet<>();
for (String n : nodes) {
setNodes.add(n.trim());
}
RedisSentinelConfiguration configuration = new RedisSentinelConfiguration(sentinelGroupName, setNodes);
return configuration;
} @Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxTotal);
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMaxWaitMillis(maxWaitMillis);
poolConfig.setTestOnBorrow(testOnBorrow);
poolConfig.setTestOnReturn(testOnReturn);
return poolConfig;
} @Bean
public StringRedisSerializer stringRedisSerializer() {
return new StringRedisSerializer();
} @Bean(name = "redisTemplate00")
@Primary
public StringRedisTemplate redisTemplate00() {
return buildRedisTemplate(buildConnectionFactory(0));
} @Bean(name = "redisTemplate01")
public StringRedisTemplate redisTemplate01() {
return buildRedisTemplate(buildConnectionFactory(1));
} @Bean(name = "redisTemplate02")
public StringRedisTemplate redisTemplate02() {
return buildRedisTemplate(buildConnectionFactory(2));
} @Bean(name = "redisTemplate03")
public StringRedisTemplate redisTemplate03() {
return buildRedisTemplate(buildConnectionFactory(3));
} @Bean(name = "redisTemplate04")
public StringRedisTemplate redisTemplate04() {
return buildRedisTemplate(buildConnectionFactory(4));
} private JedisConnectionFactory buildConnectionFactory(int database) {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(redisSentinelConfiguration(), jedisPoolConfig());
connectionFactory.setUsePool(true);
connectionFactory.setTimeout(timeout);
connectionFactory.setDatabase(database);
connectionFactory.setPassword(password);
connectionFactory.afterPropertiesSet();
return connectionFactory;
} protected StringRedisTemplate buildRedisTemplate(RedisConnectionFactory connectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(connectionFactory);
template.setValueSerializer(stringRedisSerializer());
template.afterPropertiesSet();
return template;
} }

上面的示例,注入了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}  

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

spring-boot 速成(12) - 如何注入多个redis StringRedisTemplate的更多相关文章

  1. Spring Boot之配置文件值注入(@ConfigurationProperties)

    前言:Spring Boot配置文件值的注入有两种方式,分别是 @ConfigurationProperties @Value 这里我们使用第一种 首先我们创建一个application.yml文件, ...

  2. Spring Boot @Autowired 没法自动注入的问题

    Application 启动类: @SpringBootApplication @EnableConfigurationProperties @ComponentScan(basePackages = ...

  3. Spring Boot通过ImportBeanDefinitionRegistrar动态注入Bean

    在阅读Spring Boot源码时,看到Spring Boot中大量使用ImportBeanDefinitionRegistrar来实现Bean的动态注入.它是Spring中一个强大的扩展接口.本篇文 ...

  4. Spring boot将配置属性注入到bean类中

    一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...

  5. 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用

    2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...

  6. Spring boot将配置属性注入到bean 专题

    https://blog.csdn.net/wangmx1993328/article/details/81002901 Error starting ApplicationContext. To d ...

  7. spring boot测试类自动注入service或dao

    使用Spring Boot进行单元测试时,发现使用@Autowired注解的类无法自动注入,当使用这个类的实例的时候,报出NullPointerException,即空指针异常. Spring Boo ...

  8. spring boot 在框架中注入properties文件里的值(Spring三)

    前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...

  9. Spring Boot 项目实战(四)集成 Redis

    一.前言 上篇介绍了接口文档工具 Swagger 及项目监控工具 JavaMelody 的集成过程,使项目更加健壮.在 JAVA Web 项目某些场景中,我们需要用缓存解决如热点数据访问的性能问题,业 ...

随机推荐

  1. Python 入门基础9 --函数基础2 实参与形参

    今日内容: 一.函数参数 1.形参与实参定义 2.实参分类 3.形参分类 4.可变参数的整体使用 一.形参与实参定义 def fn(参数们): pass 1.1 形参 定义函数,在括号内声明的变量名, ...

  2. oracel回收站清理

    从powerdesigner中往oracle中导入表,出现了很多类似“BIN$Z35FPY7eFZDgUKjAC94NkA==$0 ”这样的表名, 原因是删除表的时候没有彻底的删除表,而是把表放入回收 ...

  3. How to become a successful bug bounty hunter

    出处:https://www.hackerone.com/blog/become-a-successful-bug-bounty-hunter 如果你梦想成为赏金猎人,你的梦想就会成真 - 不要把你的 ...

  4. Submatrix Sum

    Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...

  5. oracle赋某表truncate权限

    一.ins用户下创建存储过程 二.mobapp用户下赋权:执行存储过程的权限 三.odso_insert用户下执行存储过程,即可truncate表 查看odso_insert用户拥有的权限 一.ins ...

  6. Go语言Windows 10开发环境搭建:Eclipse+GoClipse

    Intel Core i5-8250U,Windows 10家庭中文版,go version go1.11 windows/amd64, Eclipse IDE for C/C++ Developer ...

  7. CRT软件光标不闪烁

    点击 选项->会话选项 然后在取消即可,就有了闪烁的光标,应该是个bug 也可以把后面的浏览器之类的东西缩小一下,也会恢复

  8. java 持有对象总结

    java提供了大量的持有对象的方式: 1)数组将数字和对象联系起来,它保存类型明确的对象,查询对象时,不需要对结果做类型转换,它可以时多维的,可以保存基本数据类型的数据,但是,数组一旦生成,其容量就不 ...

  9. java adapter(适配器)惯用方法

    如果现在有一个Iterable类,你想要添加一种或多种在foreach语句中使用这个类的方法,例如方向迭代,应该怎么做呢? 如果之间继承这个类,并且覆盖iterator()方法,你只能替换现有的方法, ...

  10. GeoHash解析及java实现

    GeoHash解析请参考这里: http://www.open-open.com/lib/view/open1417940079964.html java实现GeoHash,代码已注释. import ...