Redis多database(数据源)

Redis哨兵模式切换database

为了实现数据隔离
本文是切换redis的database
用同样的方法也可以切换不同的redis服务器或者集群

注册多个RedisTemplate

实现了BeanFactoryAware接口,会在Bean初始化阶段执行其方法
多个Redis实例也可使用这种方式

get(String topOrg) 方法可以根据topOrg去BeanFactory中获取对应的redisTemplate
topOrg可以存在ThreadLocal或者其他地方

@Component
public class RedisUtilFactory implements BeanFactoryAware {
   

    private final static Logger logger = LoggerFactory.getLogger(RedisUtilFactory.class);

    @Autowired
    private RedisUtilConfig redisUtilConfig;

    private static DefaultListableBeanFactory beanFactory;


    /**
     * get current redisTemplate
     *
     * @return
     */
    public RedisTemplate get(String topOrg) {
   
        String beanName = composeBeanName(topOrg);
        if (beanFactory.containsBean(beanName)) {
   
            Object bean = beanFactory.getBean(beanName);
            if (bean instanceof RedisTemplate) {
   
                return (RedisTemplate) bean;
            }
        }
        // todo exception
        logger.info("can not find redisTemplate by topOrg");
        return null;
    }

    /**
     * get current redisTemplate
     *
     * @return
     */
    public RedisTemplate get() {
   
        return get(ContextUtil.getTopOrg());
    }


    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
   
        DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) beanFactory;


        logger.info("[databaseMap]" + redisUtilConfig.getDatabase().toString());

        for (String topOrg : redisUtilConfig.getDatabase().keySet()) {
   
            String beanName = composeBeanName(topOrg);
            if (!listableBeanFactory.containsBean(beanName)) {
   
                listableBeanFactory.registerSingleton(beanName, redisUtilConfig.getRedisTemplate(redisUtilConfig.getDatabase().get(topOrg)));
            }
        }
        RedisUtilFactory.beanFactory = listableBeanFactory;
    }

    private String composeBeanName(String topOrg) {
   
        return RedisTemplate.class.getSimpleName() + redisUtilConfig.getDatabase().get(topOrg);
    }

}

redis配置类

package com.oneconnect.sg.config;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;
import java.util.ArrayList;
import java.
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值