默认情况下,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. CSS那些事!这个篇幅是我特意开的,不是因为帮助小菜之类的,而是在多人的团队配合中各种命名冲突的规范让人蛋疼

    CSS那些事!这个篇幅是我特意开的,不是因为帮助小菜之类的,而是在多人的团队配合中各种命名冲突的规范让人蛋疼. css这个东西只要不是新的离谱都会写,但是每个人的命名风格,方法,都不同 有人喜欢驼峰, ...

  2. [转载]jdk环境变量配置方法

    JDK下载 在安装完jdk后,还需要对jdk的环境变量进行配置才能正常使用,下面教大家如何配置jdk环境变量: 1.右键选择 计算机→属性→高级系统设置→高级→环境变量 2.系统变量→新建 变量名:J ...

  3. 01:MFC应用程序编程

    一 MFC的发展 VC 1.0->VC 5.0->VC 6.0->VC2008 SP1)->VS2010 二 MFC基础 1 MFC 微软基础类库 采用类的方式,将Win32 ...

  4. Xgboost理解

    一.xgboost模型函数形式 xgboost也是GBDT的一种,只不过GBDT在函数空间进行搜索最优F的时候,采用的是梯度下降法也就是一阶泰勒展开:而xgboost采用的是二阶泰勒展开也就是牛顿法, ...

  5. Shell中各种判断语法

    Shell判断 按照文件类型进行判断 -b 判断文件是否存在,并且是否为快设备文件(是块设备文件为真) -c 判断文件是否存在,并且是否为字符设备文件(是字符设备文件为真) -d 判断文件是否存在,并 ...

  6. 【Python】Flask系列-数据库笔记

    MySQL-python中间件的介绍与安装: 1.如果是在类unix系统上,直接进入虚拟环境,输入sudo pip install mysql-python. 2.如果是在windows系统上,那么在 ...

  7. linux下热插拔事件的产生是怎样通知到用户空间,kobject_uevent_env之uevent【转】

    转自:http://blog.csdn.net/myarrow/article/details/8259888 1.kobject, ktype, kset 1) kobject: 代表sysfs中的 ...

  8. Scala工具库

    1. Scala json解析库:https://github.com/json4s/json4s

  9. MVC Autofac依赖注入

    通过Dll实现全部类的属性注入,该演示实例主要通过多层架构中单一的对象方式来演示,没有采取接口的方式, 新建AutoFacHelper类,如下代码: public class AutoFacHelpe ...

  10. Android 拍摄(横\竖屏)视频的懒人之路

    想一想,我们聊过AudioReord,AudioTrack,MediaPlayer,那多媒体四大金刚,就剩下了MediaRecorder了(SoundPool?我这里信号不好···).其实MediaR ...