spring boot实操之整合redis多数据源

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/huangpeigui/article/details/90518844

本实例以两个redis实例为讲解对象。

 

第一步:封装基础配置类

 

package com.tencent.config.redisconfig;

import org.springframework.cache.CacheManager;
import org.springframework.data.redis.cache.RedisCacheManager;
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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author : huang
 * @Description: TODO
 * @date Date : 2019-05-13-16:39 16:39
 **/
public class BaseRedisConfig {
    public JedisConnectionFactory buildJedisConnectionFactory(RedisPropertiesConfig redisPropertiesConfig) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setDatabase(redisPropertiesConfig.getDatabase());
        jedisConnectionFactory.setHostName(redisPropertiesConfig.getHost());
        jedisConnectionFactory.setPort(redisPropertiesConfig.getPort());
        jedisConnectionFactory.setPassword(redisPropertiesConfig.getPassword());
        jedisConnectionFactory.setTimeout(redisPropertiesConfig.getTimeout());

        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(redisPropertiesConfig.getPool().getMaxIdle());
        poolConfig.setMinIdle(redisPropertiesConfig.getPool().getMinIdle());
        poolConfig.setMaxTotal(redisPropertiesConfig.getPool().getMaxActive());
        poolConfig.setMaxWaitMillis(redisPropertiesConfig.getPool().getMaxWait());
        poolConfig.setTestOnBorrow(true);

        jedisConnectionFactory.setPoolConfig(poolConfig);
        return jedisConnectionFactory;

    }

    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
        return redisCacheManager;
    }

    public RedisTemplate buidRedisTemplate(RedisConnectionFactory redisConnectionFactory) {

        /* Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);

        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        template.setConnectionFactory(redisConnectionFactory);
        //template.setKeySerializer(jackson2JsonRedisSerializer);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();


        return template;
        */

        RedisSerializer stringSerializer = new StringRedisSerializer();
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        return redisTemplate;
    }

}
package com.tencent.config.redisconfig;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author : huang
 * @Description: TODO
 * @date Date : 2019-05-13-10:27 10:27
 **/

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RedisPropertiesConfig {

    /**
     * redis 数据库
     */
    private Integer database;

    /**
     * redis 主机地址
     */
    private String host;

    /**
     * 端口
     */
    private Integer port;

    /**
     * 密码
     */
    private String password;

    /**
     * 驱动类名
     */
    private Integer timeout;


    private Pool pool;

    @Data
    public static class Pool {

        private Integer maxActive;

        private Integer minIdle;

        private Integer maxIdle;

        private Integer maxWait;
    }
}

第二步:配置主redis

package com.tencent.config.redisconfig;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author:huang
 * @data:2019/05/13
 * @decription
 */
@Slf4j
@Configuration
@EnableCaching
public class MasterRedisConfig extends BaseRedisConfig {

    @Primary
    @Bean(name = "masterJedisConnectionFactory")
    @Override
    public JedisConnectionFactory buildJedisConnectionFactory(@Qualifier("masterRedisProperties")RedisPropertiesConfig redisPropertiesConfig) {
        log.info("MasterRedisConfig RedisPropertiesConfig:{}",redisPropertiesConfig);
        return super.buildJedisConnectionFactory(redisPropertiesConfig);
    }

    @Bean(name = "masterRedisTemplate")
    @Override
    public RedisTemplate<Object, Object> buidRedisTemplate(@Qualifier("masterJedisConnectionFactory")RedisConnectionFactory redisConnectionFactory) {
        return super.buidRedisTemplate(redisConnectionFactory);
    }

    @Bean
    @Override
    public CacheManager cacheManager(@Qualifier("masterRedisTemplate")RedisTemplate redisTemplate) {
        return super.cacheManager(redisTemplate);
    }

    @Bean(name = "masterRedisProperties")
    @ConfigurationProperties(prefix = "spring.redis.master")
    public RedisPropertiesConfig getBaseDBProperties() {
        return new RedisPropertiesConfig();
    }

}

第三步:配置其他redis

package com.tencent.config.redisconfig;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
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;

/**
 * @author:huang
 * @data:2019/05/13
 * @decription
 */
@Slf4j
@Configuration
@EnableCaching
public class LbsRedisConfig extends BaseRedisConfig {

    @Bean(name = "lbsJedisConnectionFactory")
    @Override
    public JedisConnectionFactory buildJedisConnectionFactory(@Qualifier("lbsRedisProperties")RedisPropertiesConfig redisPropertiesConfig) {
        log.info("LbsRedisConfig RedisPropertiesConfig:{}",redisPropertiesConfig);

        return super.buildJedisConnectionFactory(redisPropertiesConfig);
    }

    @Bean(name = "lbsRedisTemplate")
    @Override
    public RedisTemplate<Object, Object> buidRedisTemplate(@Qualifier("lbsJedisConnectionFactory")RedisConnectionFactory redisConnectionFactory) {

        return super.buidRedisTemplate(redisConnectionFactory);
    }

    @Bean
    @Override
    public CacheManager cacheManager(@Qualifier("lbsRedisTemplate")RedisTemplate redisTemplate) {
        return super.cacheManager(redisTemplate);
    }

    @Bean(name = "lbsRedisProperties")
    @ConfigurationProperties(prefix = "spring.redis.lbs")
    public RedisPropertiesConfig getBaseDBProperties() {
        return new RedisPropertiesConfig();
    }

}

使用:

 

yaml文件配置如下:

spring:
 
  redis:
    #主redis配置
    master:
      database: 0
      host: 10.2.2.2
      port: 6379
      timeout: 20000
      password: 
      pool:
        maxActive: 8
        minIdle: 0
        maxIdle: 8
        maxWait: -1

    #lbs 只读redis配置
    lbs:
      database: 0
      host: 10.1.1.1
      port: 6379
      timeout: 20000
      password: 
      pool:
        maxActive: 8
        minIdle: 0
        maxIdle: 8
        maxWait: -1

至此配置完成,需要源代码可以找博主要。

展开阅读全文

没有更多推荐了,返回首页