springboot集成多个redis数据源 亲身填坑

简介

原来一直用不到多源的redis集成,结果以用发现有不少坑。
其实整体来说是很简单的,大致分为这么几步:
(1)properties配置文件中增加配置信息,我就增加了一个新的dbIndex
(2)RedisConfig配置类中增加@Bean配置
(3)修改Autowired注入,使用@Resource(name = )

但是我自己写了一个RedisUtils方法,也就是封装了一些set expire get方法,结果坑了我一下。
最后我只能增加了一个RedisUtils_1方法,然后正常使用。

这里分享一下我的写法,虽然实现起来没什么问题,但是总觉得会有更简便的写法。
所以也请教一下大家,对于这种封装了Utils类的,有没有更简单的写法呢。
下面是我的方法。

首先是conf类

package springboot.demo.ConfClass;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
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 redis.clients.jedis.JedisPoolConfig;

/**
 * @Author Braylon
 * @Date 2020/2/2 14:40
 */
@Configuration
public class RedisConf {

    @Bean(name = "redisLogDB")
    public RedisTemplate<String, Object> redisTemplate(
            @Value("${spring.redis.host}")String hostName,
            @Value("${spring.redis.port}")int port,
            @Value("${spring.redis.password}")String password,
            @Value("${spring.redis.pool.max-idle}")int maxIdle,
            @Value("${spring.redis.pool.max-active}")int maxTotal,
            @Value("${spring.redis.log}")int index,
            @Value("${spring.redis.pool.max-wait}")long maxWaitMills
    ) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(this.connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMills));
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    @Bean(name = "markdownPsg")
    public RedisTemplate<String, Object> mdPsgRedisTemplate(
            @Value("${spring.redis.host}")String hostName,
            @Value("${spring.redis.port}")int port,
            @Value("${spring.redis.password}")String passord,
            @Value("${spring.redis.pool.max-idle}")int maxIdle,
            @Value("${spring.redis.pool.max-active}")int maxTotal,
            @Value("${spring.redis.passages}")int index,
            @Value("${spring.redis.pool.max-wait}")long maxWaitMills
    ) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(this.connectionFactory(hostName, port, passord, maxIdle, maxTotal, index, maxWaitMills));
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle, int maxTotal, int index, long maxWaitMillis) {
        JedisConnectionFactory jcf = new JedisConnectionFactory();
        jcf.setHostName(hostName);
        jcf.setPort(port);
        jcf.setPassword(password);
        jcf.setDatabase(index);
        jcf.setPoolConfig(this.poolConfig(maxIdle, maxTotal, maxWaitMillis));
        jcf.afterPropertiesSet();
        RedisConnectionFactory factory = jcf;
        return factory;
    }

    public JedisPoolConfig poolConfig(int maxIdle, int maxTotal, long maxWaitMillis) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setTestOnBorrow(false);
        return jedisPoolConfig;
    }
}

然后添加@Resource
这里我添加到RedisUtils,是我自己封装的类
但是为了实现不同的数据源,有两个utils
在这里插入图片描述
在这里插入图片描述
然后在业务逻辑类中用@Autowired注入RedisUtils_1 和RedisUtils
在这里插入图片描述
在这里插入图片描述

有没有更简便的写法呢
请教一下
武汉加油!
大家共勉!

发布了44 篇原创文章 · 获赞 48 · 访问量 7159

猜你喜欢

转载自blog.csdn.net/qq_40742298/article/details/104255152
今日推荐