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
在這裏插入圖片描述
在這裏插入圖片描述

有沒有更簡便的寫法呢
請教一下
武漢加油!
大家共勉!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章