springboot redis多数据源设置
遇到这样一个需求:运营人员在发布内容的时候可以选择性的发布到测试库、开发库和线上库。
项目使用的是spring boot集成redis,实现如下:
1. 引入依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2.多数据源设置
application.yml设置(application.properties同理):
spring:
redis:
database: 0
pool:
max-active: 8
max-idle: 9
max-wait: -1
min-idle: 0
redis-dev:
host: 填redis的ip地址
prot: 填redis的端口号
password: 填redis的密码
testOnBorrow: fals
redis-test:
host:
prot:
password:
testOnBorrow: false
redis-online:
host:
prot:
password:
testOnBorrow: false
针对每个数据源写一个配置类:
这里就只列举其中一个,不同的数据源设置不同的@Bean和@Value注解即可
@Configuration
public class RedisDevConfiguration {
@Bean(name = "redisDevTemplate")
public StringRedisTemplate redisTemplate(@Value("${spring.redis-dev.host}") String hostName,
@Value("${spring.redis-dev.port}") int port, @Value("${spring.redis-dev.password}") String password,
@Value("${spring.redis-dev.testOnBorrow}") boolean testOnBorrow,
@Value("${spring.redis.pool.max-idle}") int maxIdle, @Value("${spring.redis.pool.max-active}") int maxTotal,
@Value("${spring.redis.database}") int index, @Value("${spring.redis.pool.max-wait}") long maxWaitMillis) {
StringRedisTemplate temple = new StringRedisTemplate();
temple.setConnectionFactory(
connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));
return temple;
}
public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
int maxTotal, int index, long maxWaitMillis, boolean testOnBorrow) {
JedisConnectionFactory jedis = new JedisConnectionFactory();
jedis.setHostName(hostName);
jedis.setPort(port);
if (StringUtils.isNotEmpty(password)) {
jedis.setPassword(password);
}
if (index != 0) {
jedis.setDatabase(index);
}
jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis, testOnBorrow));
// 初始化连接pool
jedis.afterPropertiesSet();
RedisConnectionFactory factory = jedis;
return factory;
}
public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
JedisPoolConfig poolCofig = new JedisPoolConfig();
poolCofig.setMaxIdle(maxIdle);
poolCofig.setMaxTotal(maxTotal);
poolCofig.setMaxWaitMillis(maxWaitMillis);
poolCofig.setTestOnBorrow(testOnBorrow);
return poolCofig;
}
}
3.构造redis实例
写一个redis实例抽象类
public abstract class AbRedisConfiguration {
protected StringRedisTemplate temple;
public void setData(String key, String value) {
getTemple().opsForValue().set(key, value);
}
public String getData(String key) {
return getTemple().opsForValue().get(key);
}
public StringRedisTemplate getTemple() {
return temple;
}
}
继成抽象类实现操作类,这里只列举一个
@Component
public class RedisDev extends AbRedisConfiguration {
@Resource(name = "redisDevTemplate")
private StringRedisTemplate temple;
public StringRedisTemplate getTemple() {
return temple;
}
}
4.使用
在service中注入redis操作类
@Autowired
private RedisDev redisDev;
@Autowired
private RedisTest redisTest;
@Autowired
private RedisTest redisOnline;
调用不同的操作类即可使用不同的redis数据源。
springboot redis多数据源设置的更多相关文章
- springboot redis多数据源
springboot中默认的redis配置是只能对单个redis库进行操作的. 那么我们需要多个库操作的时候这个时候就可以采用redis多数据源. 本代码参考RedisAutoConfiguratio ...
- springboot2.1+redis多数据源的配置
springboot系列学习笔记全部文章请移步值博主专栏**: spring boot 2.X/spring cloud Greenwich. 由于是一系列文章,所以后面的文章可能会使用到前面文 ...
- 补习系列(14)-springboot redis 整合-数据读写
目录 一.简介 二.SpringBoot Redis 读写 A. 引入 spring-data-redis B. 序列化 C. 读写样例 三.方法级缓存 四.连接池 小结 一.简介 在 补习系列(A3 ...
- SpringBoot+Redis整合
SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...
- springboot添加多数据源连接池并配置Mybatis
springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018 ...
- springboot配置Druid数据源
springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...
- SpringBoot Redis缓存 @Cacheable、@CacheEvict、@CachePut
文章来源 https://blog.csdn.net/u010588262/article/details/81003493 1. pom.xml <dependency> <gro ...
- springBoot整合多数据源
springBoot整合相关 1:springBoot整合多数据源: 应用场景: 项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 工具/版本: jdk1. ...
- springboot + mybatis + 多数据源
此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1) ...
随机推荐
- BZOJ 1934 Vote 善意的投票(最小割+二分图)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1934 题目大意: 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题 ...
- ***Linux系统下如何查看及修改文件读写权限
查看文件权限的语句: 在终端输入:ls -l xxx.xxx (xxx.xxx是文件名) 那么就会出现相类似的信息,主要都是这些:-rw-rw-r-- 一共有10位数 其中: 最前面那个 - 代表的是 ...
- .NetCore 分页控件实现原理处理以及条件分页处理
说明 自定义一个类继承TagHelper,注意自定义类的 必须以TagHelper结尾,这个有点类是属性 Attribute的写法 protected TagHelper(); // // 摘要: / ...
- CF 554B 找相同行
给定一个由n*n块地砖铺成的房间,每块砖用0表示未打扫,1表示已打扫. 要求打扫时只能整列地扫,未打扫的会变为已打扫,已打扫的会变为未打扫.即1会变成0,而0会变成1,目标是 使最后整行为1的行数最大 ...
- mysql多表查询,group by并将结果导出来csv文件
SQL手动操作得少,遇到一个CASE,就记录一个CASE吧. 今天遇到的是统计一个发布结果. 这个发布表中,有两个字段是外键,并且要求按其中一个外键,作group by操作, 最后,导出来excel能 ...
- day12--python操作mysql
本篇对于Python操作MySQL主要使用两种方式: 1.原生模块 pymsql(http://www.cnblogs.com/wupeiqi/articles/5713330.htm ...
- wfst的compose算法
介绍一些compose算法,以及这部分的代码实现. 原理部分参考: 走进语音识别中的 WFST(二) 可以看下示例图: 我们先来看一下 Composition 的效果,图(a)和图(b)Composi ...
- fpm制作rpm包
一.前言 在企业中我们有事安装软件包.部分都是源码安装,如nginx安装路径都已经固化了,但实际业务中,我们都是把软件包安装到固定目录下,不满足需要,这是其一.其二,编译安装很耗时,比如mysql,特 ...
- 【BZOJ】3168: [Heoi2013]钙铁锌硒维生素
题解 Ca Fe Zn Se 显然我们既然初始矩阵就能通过线性变换变成单位矩阵,则该矩阵一定有逆 没有逆输出NIE 而且因为这些向量两两正交,则表示一个向量的时候表示方法唯一 那么我们求一个逆可以求出 ...
- 040 DataFrame中的write与read编程
一:SparkSQL支持的外部数据源 1.支持情况 2.External LIbraries 不是内嵌的,看起来不支持. 但是现在已经有很多开源插件,可以进行支持. 3.参考材料 · 支持的格式:ht ...