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) ...
随机推荐
- Python 协程检测Kubernetes服务端口
一.需求分析 在上一篇文章,链接如下: https://www.cnblogs.com/xiao987334176/p/10237551.html 已经得到了需要的数据,现在需要对这些端口做检测,判断 ...
- JS模块化编程(二):require.js基本用法
require.config() 接受一个配置对象 常用属性: paths: shim: 配置不兼容的模块 baseUrl: 引用模块的文件基目录
- Java编程的逻辑 (4) - 整数的二进制表示与位运算
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- CSS工具之CSS重置(CSS Reset)
css代码: /* v1.0 | 20080212 */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, ...
- AC自动机学习笔记-2(Trie图&&last优化)
我是连月更都做不到的蒟蒻博主QwQ 考虑到我太菜了,考完noip就要退役了,所以我决定还是把博客的倒数第二篇博客给写了,也算是填了一个坑吧.(最后一篇?当然是悲怆のnoip退役记啦QAQ) 所以我们今 ...
- Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp
D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] ...
- linux设置最大打开文件数
一.查看当前用户对进程打开文件最大数的限制 $ ulimit -a | grep open 二.系统对进程打开文件最大数是如何限制的 先来看man的一段解析: /proc/sys/fs/file-ma ...
- CSUOJ Water Drinking
Description The Happy Desert is full of sands. There is only a kind of animal called camel living on ...
- .NET工作准备--04ASP.NET
(已过时) ASP.NET 1.开发基础 *asp.net以什么形式运行?.net宿主的概念,ISAPI的概念,ASP.NET基本运行机制; .net宿主的概念:CLR被实现为一个标准的COM服务器组 ...
- [教程] Spring+Mybatis环境配置多数据源
一.简要概述 在做项目的时候遇到需要从两个数据源获取数据,项目使用的Spring + Mybatis环境,看到网上有一些关于多数据源的配置,自己也整理学习一下,然后自动切换实现从不同的数据源获取数据功 ...