默认情况下,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. 20155310 2016-2017-2 《Java程序设计》第五周学习总结

    20155310 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 •收集对象的行为,像是新增对象的add()方法.移除对象的remove()方法等,都是定义在 ...

  2. iOS常用小功能

    CHENYILONG Blog 常用小功能 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong  ...

  3. 20165230 《Java程序设计》实验五《网络编程与安全》实验报告

    20165230 <Java程序设计>实验五<网络编程与安全>实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:田坤烨 学号:20165230 成绩: ...

  4. 对linux内核中jiffies+Hz表示一秒钟的理解

    jiffies在内核中是一个全局变量,它用来统计系统启动以来系统中产生的总节拍数,这个变量定义在include/linux/jiffies.h中,定义形式如下. unsigned long volat ...

  5. linux批量关闭进程

    ps aux | grep gunicorn_api | awk '{print $2}' | xargs kill -9 gunicorn 换成你的关键字即可.

  6. PHP URL中包含中文,查看时提示404

    使用Microsoft Web Platform在IIS里配置安装一个wordpress,一切顺利. 当添加一片文章时,自动生成URL类似如下: http://localhost/wordpress/ ...

  7. JavaEE之JavaWeb简介

  8. C#控制台程序输出彩色文字

    /* * 由SharpDevelop创建. * 用户: 从前的我 * 日期: 2012-06-03 * 时间: 21:30 * * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件 */ u ...

  9. JS 解析JSON实现导序

    var chartData = [ { "online": '2013-10-23', "new": 0.00, "login": 0.00 ...

  10. php实现https(tls/ssl)双向认证

    php实现https(tls/ssl)双向认证 通常情况下,在部署https的时候,是基于ssl单向认证的,也就是说只要客户端认证服务器,而服务器不需要认证客户端. 但在一些安全性较高的场景,如银行, ...