spring-boot 速成(12) - 如何注入多个redis StringRedisTemplate
默认情况下,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的更多相关文章
- Spring Boot之配置文件值注入(@ConfigurationProperties)
前言:Spring Boot配置文件值的注入有两种方式,分别是 @ConfigurationProperties @Value 这里我们使用第一种 首先我们创建一个application.yml文件, ...
- Spring Boot @Autowired 没法自动注入的问题
Application 启动类: @SpringBootApplication @EnableConfigurationProperties @ComponentScan(basePackages = ...
- Spring Boot通过ImportBeanDefinitionRegistrar动态注入Bean
在阅读Spring Boot源码时,看到Spring Boot中大量使用ImportBeanDefinitionRegistrar来实现Bean的动态注入.它是Spring中一个强大的扩展接口.本篇文 ...
- Spring boot将配置属性注入到bean类中
一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...
- 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用
2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...
- Spring boot将配置属性注入到bean 专题
https://blog.csdn.net/wangmx1993328/article/details/81002901 Error starting ApplicationContext. To d ...
- spring boot测试类自动注入service或dao
使用Spring Boot进行单元测试时,发现使用@Autowired注解的类无法自动注入,当使用这个类的实例的时候,报出NullPointerException,即空指针异常. Spring Boo ...
- spring boot 在框架中注入properties文件里的值(Spring三)
前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...
- Spring Boot 项目实战(四)集成 Redis
一.前言 上篇介绍了接口文档工具 Swagger 及项目监控工具 JavaMelody 的集成过程,使项目更加健壮.在 JAVA Web 项目某些场景中,我们需要用缓存解决如热点数据访问的性能问题,业 ...
随机推荐
- Flex 程序执行顺序!
Flex 执行加载过程会有几个概念:preloader, SystemManager, Flex Application! flex 界面初始化时,看到的 Loading 加载条,那是 flex 自动 ...
- 在 Linux 中安装 VMware Tools
由于较新的Linux版本中都包含了vm的部分组件,导致直接安装VMware Tools失败.所以这里写了篇新的. 软件版本:VMware 12 Linux版本:Ubuntu Desktop 16.04 ...
- 内置函数bytes()
a=b'\x00\x9c@c' print a[3]#99,c的ascii码是99 print a[1]#156 并且byte是无法修改的 c[1]=155 Traceback (most recen ...
- C. NN and the Optical Illusion(几何)
题目链接:http://codeforces.com/contest/1100/problem/C 题目大意:给你n和r,n指的是有n个圆围在里面的圆的外面,r指的是里面的圆的半径,然后让你求外面的圆 ...
- 采用jacob实现word转pdf
网络上已经有很多这方面的内容,在用之前也是参考了好多别人的文章,下面记录下我自己的整合过程.整个过程都比较简单: 开发环境:win8 64位系统,在2008下面部署也是一样的. 文档要求jdk的版本要 ...
- 在maven 2工程中加入iTextAsian支持(maven添加自定义jar包到本地仓库)
最近需要在工程中加入JasperReports,其中要用到把报表导出为pdf文件的功能.JasperReports内部使用iText来输出pdf文档,而iText对中文是放在单独的包iTextAsia ...
- mybatis一对多关联查询——(九)
1.需求: 查询所有订单信息及订单下的订单明细信息. 订单信息与订单明细为一对多关系. 2. sql语句 确定主查询表:订单表 确定关联查询表:订单明细表 在一对一查询基础上添加订单明细表关 ...
- redis从入门到踩坑
背景 Redis在互联网项目的使用也是非常普遍的,作为最常用的NO-SQL数据库,对Redis的了解已经成为了后端开发的必备技能.小编对Redis的使用时间不长,但是项目中确两次踩中了Redis的坑, ...
- python运行execjs解密js
[转]http://www.knowsky.com/1041161.html python 记一次计算qzonetoken经历 之前用python写了个发表说说的爬虫,但最近发现在post数据时返回不 ...
- 009_【OS X和iOS系统学习笔记】 OS X架构
1.OS X是整个操作系统的集体名称,而Darwin是其中的一个组件. 2.Darwin是操作系统的类UNIX核心,本身由内核.XNU和运行时组成. 3.uname指令:可以得到有关架构的详细信息以及 ...