Redis配置多数据源

2022-05-05 15:08:49 浏览数 (1)

使用场景

Redis具有多个databases,通过CONFIG GET databases命令可查看,Redis不同的database间具有数据隔离性,即在项目中我们配置一个databases 0信息后,并无法获取其他databases的数据,所以,当我们业务中需要获取其他databases时,便需要配置多Redis数据源。

配置文件

以properties为例

代码语言:javascript复制
#redis databases 1
spring.redis.database=1
spring.redis.host=127.0.0.1
spring.redis.password=123456
spring.redis.port=6379
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=1
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=7000

#redis databases 2  unread只是业务一个标识,可自定义
spring.unread.redis.database=2
spring.unread.redis.host=127.0.0.2
spring.unread.redis.password=123456
spring.unread.redis.port=6380
spring.unread.redis.pool.max-idle=8
spring.unread.redis.pool.min-idle=1
spring.unread.redis.pool.max-active=8
spring.unread.redis.pool.max-wait=-1
spring.unread.redis.timeout=7000
RedisTemplate配置

创建一个基于读取Redis配置的Bean

代码语言:javascript复制
@Configuration
@ConditionalOnClass({JedisConnection.class, RedisOperations.class, RedisProperties.Jedis.class})
public class Redis4UnReadMessageConfig {
    @Bean(name = "redisUnReadTemplate")
    public FastJsonRedisTemplate redisTemplate(
        //与properties一一对应
            @Value("${spring.unread.redis.host}") String hostName,
            @Value("${spring.unread.redis.port}") int port,
            @Value("${spring.unread.redis.password}") String password,
            @Value("${spring.unread.redis.database}") int index
    ) {
        FastJsonRedisTemplate temple = new FastJsonRedisTemplate();
        temple.setConnectionFactory(
                connectionFactory2(hostName, port, password,index));
        return temple;
    }
    @Deprecated
    public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
                                                    int minIdle, int index, long maxWaitMillis) {
        JedisConnectionFactory jedis = new JedisConnectionFactory();
        jedis.setHostName(hostName);
        jedis.setPort(port);
        if (!StringUtils.isEmpty(password)) {
            jedis.setPassword(password);
        }
        if (index != 0) {
            jedis.setDatabase(index);
        }
        jedis.setPoolConfig(poolCofig(maxIdle, minIdle, maxWaitMillis));
        // 初始化连接pool
        jedis.afterPropertiesSet();
        RedisConnectionFactory factory = jedis;
        return factory;
    }
    @Deprecated
    public JedisPoolConfig poolCofig(int maxIdle, int minIdle, long maxWaitMillis) {
        JedisPoolConfig poolCofig = new JedisPoolConfig();
        poolCofig.setMaxIdle(maxIdle);
        poolCofig.setMinIdle(minIdle);
        poolCofig.setMaxWaitMillis(maxWaitMillis);
        return poolCofig;
    }

    /**
     * @Author: Jeremy
     * @Description: springboot2.x默认使用lettuce取代jedis
     * @Date: 2020/5/8 14:17
     */
    public RedisConnectionFactory connectionFactory2(String hostName, int port, String password,int index){

        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(hostName);
        configuration.setPort(port);
        configuration.setPassword(password);
        configuration.setDatabase(index);
        LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration);
        factory.afterPropertiesSet();
        return factory;
    }
}
注解使用

将多数据源的redisTemplate注入使用

注意: 配合@Autowired使用@Qualifier将上文Config定义的Bean注入。

代码语言:javascript复制
    @Autowired
    @Qualifier("Redis4UnReadMessageConfig")
    public FastJsonRedisTemplate Redis4UnReadMessageConfig;

0 人点赞