springboot + redis 多数据源 (相同 + 完全独立)

spring.redis.common.database.index=0
spring.redis.common.lettuce.pool.min-idle=0
spring.redis.common.lettuce.pool.max-idle=8
spring.redis.common.lettuce.pool.max-active=8
spring.redis.common.lettuce-factory.max-wait=300

spring.redis.wh.host=192.168.1.130
spring.redis.wh.port=6379
spring.redis.wh.password=root


spring.redis.es.host=192.168.1.130
spring.redis.es.port=6380
spring.redis.es.password=root
 

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;

import java.time.Duration;

/**
 * @author wongH
 * @date 2020/5/6 15:53
 * @Version 1.0
 */
@Configuration
public class LettuceRedisConfig {


    @Value("${spring.redis.common.lettuce.pool.max-idle}")
    int maxIdle;

    @Value("${spring.redis.common.lettuce.pool.min-idle}")
    int minIdle;

    @Value("${spring.redis.common.lettuce.pool.max-active}")
    int maxTotal;

    @Value("${spring.redis.common.database.index}")
    int index;

    @Value("${spring.redis.common.lettuce-factory.max-wait}")
    long maxWaitMillis;

    @Primary
    @Bean(name = "RedisConnectionFactoryWh")
    public LettuceConnectionFactory connectionFactoryWh(
            @Value("${spring.redis.wh.host}") String hostName,
            @Value("${spring.redis.wh.port}") int port,
            @Value("${spring.redis.wh.password}") String password) {

        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(index);
        redisStandaloneConfiguration.setHostName(hostName);
        redisStandaloneConfiguration.setPort(port);
        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .commandTimeout(Duration.ofMillis(maxWaitMillis))
                .poolConfig(genericObjectPoolConfig())
                .build();

        return new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
    }

    @Bean(name = "RedisConnectionFactoryEs")
    public LettuceConnectionFactory connectionFactoryEs(
            @Value("${spring.redis.es.host}") String hostName,
            @Value("${spring.redis.es.port}") int port,
            @Value("${spring.redis.es.password}") String password) {

        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(index);
        redisStandaloneConfiguration.setHostName(hostName);
        redisStandaloneConfiguration.setPort(port);
        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .commandTimeout(Duration.ofMillis(maxWaitMillis))
                .poolConfig(genericObjectPoolConfig())
                .build();

        return new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
    }

    @Bean
    public GenericObjectPoolConfig genericObjectPoolConfig() {
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(maxIdle);
        genericObjectPoolConfig.setMinIdle(minIdle);
        genericObjectPoolConfig.setMaxTotal(maxTotal);
        genericObjectPoolConfig.setMaxWaitMillis(maxWaitMillis);
        return genericObjectPoolConfig;
    }
}

 

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {



    @Bean(name = "RedisWhDatabase")
    public RedisTemplate<String, Object> redisWhDatabase(@Qualifier("RedisConnectionFactoryWh") LettuceConnectionFactory lettuceConnectionFactory) {
        //设置序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        RedisTemplate<String, Object> temple = new RedisTemplate<>();
        temple.setConnectionFactory(lettuceConnectionFactory);
        RedisSerializer stringSerializer = new StringRedisSerializer();
        temple.setKeySerializer(stringSerializer);//key序列化
        temple.setValueSerializer(stringSerializer);//value序列化
        temple.setHashKeySerializer(stringSerializer);//Hash key序列化
        temple.setHashValueSerializer(jackson2JsonRedisSerializer);//Hash value序列化
        return temple;
    }

    @Bean(name = "StringRedisTemplateWh")
    public StringRedisTemplate stringRedisTemplateWh(@Qualifier("RedisConnectionFactoryWh") LettuceConnectionFactory lettuceConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(lettuceConnectionFactory);
        return stringRedisTemplate;
    }

    @Bean(name = "RedisEsDatabase")
    public RedisTemplate<String, Object> redisEsDatabase(@Qualifier("RedisConnectionFactoryEs") LettuceConnectionFactory lettuceConnectionFactory) {
        //设置序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        RedisTemplate<String, Object> temple = new RedisTemplate<>();
        temple.setConnectionFactory(lettuceConnectionFactory);
        RedisSerializer stringSerializer = new StringRedisSerializer();
        temple.setKeySerializer(stringSerializer);//key序列化
        temple.setValueSerializer(stringSerializer);//value序列化
        temple.setHashKeySerializer(stringSerializer);//Hash key序列化
        temple.setHashValueSerializer(jackson2JsonRedisSerializer);//Hash value序列化
        return temple;
    }

    @Bean(name = "StringRedisTemplateEs")
    public StringRedisTemplate stringRedisTemplateEs(@Qualifier("RedisConnectionFactoryEs") LettuceConnectionFactory lettuceConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(lettuceConnectionFactory);
        return stringRedisTemplate;
    }

}


 
//DataSourceHolder 和 CityEnum 内容和获取参考 https://blog.csdn.net/wangh92/article/details/105964865
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    private static RedisTemplate RedisWhDatabase;
    private static StringRedisTemplate stringRedisTemplateWh;

    private static RedisTemplate RedisEsDatabase;
    private static StringRedisTemplate stringRedisTemplateEs;

    @Autowired
    public RedisUtil(@Qualifier("RedisWhDatabase") RedisTemplate RedisWhDatabase,
                     @Qualifier("StringRedisTemplateWh") StringRedisTemplate stringRedisTemplateWh,
                     @Qualifier("RedisEsDatabase") RedisTemplate RedisEsDatabase,
                     @Qualifier("StringRedisTemplateEs") StringRedisTemplate stringRedisTemplateEs) {
        RedisUtil.RedisWhDatabase = RedisWhDatabase;
        RedisUtil.stringRedisTemplateWh = stringRedisTemplateWh;
        RedisUtil.RedisEsDatabase = RedisEsDatabase;
        RedisUtil.stringRedisTemplateEs = stringRedisTemplateEs;
    }

    public static void set(String key, String value, Long time) throws IOException {
        String city = DataSourceHolder.getDataSource();
        switch (CityEnum.valueOf(city)) {
            case wuhan:
                stringRedisTemplateWh.opsForValue().set(key, value, time, TimeUnit.SECONDS);
                System.err.println("wuhan RedisTemplate 插入成功");
                break;
            case enshi:
                stringRedisTemplateEs.opsForValue().set(key, value, time,TimeUnit.SECONDS);
                System.err.println("enshi RedisTemplate 插入成功");
                break;
            default:
                break;
        }
    }

    public static String get(String key) {
        String city = DataSourceHolder.getDataSource();
        String value = null;
        switch (CityEnum.valueOf(city)) {
            case wuhan:
                value = stringRedisTemplateWh.opsForValue().get(key);
                break;
            case enshi:
                value = stringRedisTemplateEs.opsForValue().get(key);
                break;
            default:
                break;
        }
        return value;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值