遇到这样一个需求:运营人员在发布内容的时候可以选择性的发布到测试库、开发库和线上库。 
项目使用的是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多数据源设置的更多相关文章

  1. springboot redis多数据源

    springboot中默认的redis配置是只能对单个redis库进行操作的. 那么我们需要多个库操作的时候这个时候就可以采用redis多数据源. 本代码参考RedisAutoConfiguratio ...

  2. springboot2.1+redis多数据源的配置

    springboot系列学习笔记全部文章请移步值博主专栏**: spring boot 2.X/spring cloud Greenwich.    由于是一系列文章,所以后面的文章可能会使用到前面文 ...

  3. 补习系列(14)-springboot redis 整合-数据读写

    目录 一.简介 二.SpringBoot Redis 读写 A. 引入 spring-data-redis B. 序列化 C. 读写样例 三.方法级缓存 四.连接池 小结 一.简介 在 补习系列(A3 ...

  4. SpringBoot+Redis整合

    SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...

  5. springboot添加多数据源连接池并配置Mybatis

    springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018  ...

  6. springboot配置Druid数据源

    springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...

  7. SpringBoot Redis缓存 @Cacheable、@CacheEvict、@CachePut

    文章来源 https://blog.csdn.net/u010588262/article/details/81003493 1. pom.xml <dependency> <gro ...

  8. springBoot整合多数据源

    springBoot整合相关 1:springBoot整合多数据源: 应用场景:     项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 工具/版本: jdk1. ...

  9. springboot + mybatis + 多数据源

    此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1) ...

随机推荐

  1. SpringMVC高级参数绑定(数组和List)

    本节内容: 参数绑定之数组 将表单的数据绑定到List 复制下上篇博客中的工程,作为今天开发的工程. 一.参数绑定之数组 1. 需求 在商品列表页面选中多个商品,然后删除. 2. 需求分析 功能要求商 ...

  2. UVALive - 6709

    二维线段树单点修改板子题 #include<bits/stdc++.h> #define LL long long #define fi first #define se second # ...

  3. Class PropertyPlaceholderHelper的使用

    1.代码 private static Properties properties = new Properties(); public static String getProperties(Str ...

  4. 实现C语言字符串操作的库函数 包括基本的字符串复制 字符串长度 字符串比较等多种函数(C代码)

    头文件 "mystring.h" #ifndef _MYSTR_H #define _MYSTR_H #include <stdio.h> #include <s ...

  5. com.jcraft.jsch.JSchException: Auth fail

    背景 服务器信息: 服务器A:10.102.110.1 服务器B:10.102.110.2 需要从服务器A通过Sftp传输文件到服务器B. 应用项目中有一个功能,要通个关Sftp进行日志文件的传输,在 ...

  6. iOS技术篇:sizeToFit 和 sizeThatFits 区别

    sizeToFit:会计算出最优的 size 而且会改变自己的size UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , ...

  7. 子类 父类强转 HttpServlet service实现

    相当于 走父类 临时走了一趟 HttpServletRequest ->ServletRequets -> HttpServeltRequest /* */ public void ser ...

  8. android 四大组件

     韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 活动,服务,广播接受者,内容提供者. 活动 能够提供 用户界面.服务 没有用户界面.广 ...

  9. 1063 合并果子 2004年NOIP全国联赛普及组

    题目描述 Description 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等 ...

  10. BZOJ.3143.[HNOI2013]游走(概率 期望 高斯消元)

    题目链接 参考 远航之曲 把走每条边的概率乘上分配的标号就是它的期望,所以我们肯定是把大的编号分配给走的概率最低的边. 我们只要计算出经过所有点的概率,就可以得出经过一条边(\(u->v\))的 ...