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数据源。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值