遇到这样一个需求:运营人员在发布内容的时候可以选择性的发布到测试库、开发库和线上库。 
项目使用的是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. .NetCore中使用ExceptionLess记录Polly中的操作异常日志

    结合上一篇文章我写了一个demo测试下 重试2次 _polly.PollyRetry<Exception>(()=>_demoQuery.GetTestAOPAsync(), ); ...

  2. 数学之美——HMM模型(一)介绍

    一直想写点关于数学方面的blog,这对于数据挖掘分析,NLP处理等都有着比较重要的作用,之前在CSDN上想写点HMM方面的文章,一直没写成,最近几天终于抽点时间完成了HMM的文章,加以整理,遂有这个系 ...

  3. PHP内置函数实现简单洗牌

    function wash_card($num_card) { $a = array_keys(array_fill(0, $num_card, '')); $b = array_keys(array ...

  4. 020 shuffle的重要作用,以及分区的实践

    一:学shuffle原理的必要性 1.说明 学习shuffle的作用是可以对程序进行优化. 在shuffle这个部分有三个部分需要注意: 分区 排序 分组 这个可以进行优化. 二:分区的实践 1.说明 ...

  5. # Java反射2——获取实体所有属性和方法,并对属性赋值

    1.一个普通的实体Person: private int id; private String name; private Date createdTime; ... //其它字段 // get se ...

  6. 008.Zabbix多图展示

    一 Screen介绍 Screen能将某个主机多个图形,或者多个主机的同一种信息放在一起展示. 二 创建多主机监控图形 依次添加VMware-Win7和VMware-CentOS7两台主机的监控图形. ...

  7. Docker化高可用redis集群

    最近遇到部分系统因为redis服务挂掉,导致部分服务不可用.所以希望搭建一个redis集群镜像,把原先散落各处的redis服务器统一管理起来,并且保障高可用和故障自动迁移. 一:redis集群分类 大 ...

  8. HDU 1402 A * B Problem Plus 快速傅里叶变换 FFT 多项式

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 快速傅里叶变换优化的高精度乘法. https://blog.csdn.net/ggn_2015/artic ...

  9. android.intent.category.DEFAULT

    我们需要什么时候加android.intent.category.DEFAULT呢? 1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐式) intent什么是explicit(显示) i ...

  10. Codeforces 986D Perfect Encoding FFT

    题意: 给定一个数n,选出m个数使得 $\Pi_{i=1}^m a_i\ge n$,求$\sum_{i=1}^m a_i$的最小值 ,其中$m$的大小不限 $n$的长度$\le 10^6$ 简单的计算 ...