双redis配置

现在springboot配置redis 

# 配置outredis
spring.redis.host: ******
spring.redis.password: ******
spring.redis.port: 6379
spring.redis.timeout: 6000
# 连接池最大连接数(使用负值表示没有限制) 默认为8
spring.redis.lettuce.pool.max-active: 100
 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认为-1
spring.redis.lettuce.pool.max-wait: -1
# 连接池中的最大空闲连接 默认为8
spring.redis.lettuce.pool.max-idle: 8
# 连接池中的最小空闲连接 默认为 0
spring.redis.lettuce.pool.min-idle: 0

# 配置inredis
spring.redis2.host: ******
spring.redis2.password: ******
spring.redis2.port: 6379

写config 

@Configuration
public class RedisAutoConfiguration {

    @Bean
    public RedisAspect openTracingRedisAspect() {
        return new RedisAspect();
    }


    @Bean
    @ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
    public GenericObjectPoolConfig redisPool() {
        return new GenericObjectPoolConfig<>();
    }


    @Bean("dataFactory1")
    @Primary
    public LettuceConnectionFactory dataFactory1(@Value("${spring.redis.host}") String host,
                                                 @Value("${spring.redis.port}") int port,
                                                 @Value("${spring.redis.password}") String password,
                                                 GenericObjectPoolConfig config) {
        RedisStandaloneConfiguration redisDataConfig = new RedisStandaloneConfiguration(host, port);
        redisDataConfig.setPassword(RedisPassword.of(password));
        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
        LettuceConnectionFactory factory = new LettuceConnectionFactory(redisDataConfig, clientConfiguration);
        return factory;
    }

    //第二个数据源的连接工厂
    @Bean("dataFactory2")
    public LettuceConnectionFactory dataFactory2(@Value("${spring.redis2.host}") String host,
                                                 @Value("${spring.redis2.port}") int port,
                                                 @Value("${spring.redis2.password}") String password,
                                                 GenericObjectPoolConfig config) {
        RedisStandaloneConfiguration redisDataConfig = new RedisStandaloneConfiguration(host, port);
        redisDataConfig.setPassword(RedisPassword.of(password));
        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
        LettuceConnectionFactory factory = new LettuceConnectionFactory(redisDataConfig, clientConfiguration);
        return factory;
    }

    @Bean("redisTemplate")
    @Primary
    public RedisTemplate<String, String> getRedisTemplate(@Qualifier("dataFactory1") RedisConnectionFactory factory) {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        // String 的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(stringRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(stringRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean("redisTemplate2")
    public RedisTemplate<String, String> getRedisTemplate2(@Qualifier("dataFactory2") RedisConnectionFactory factory) {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        // Json序列化配置
        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);
        // String 的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}

后面使用

@Autowired
@Qualifier("redisTemplate2")//@Qualifier("redisTemplate1") 指定使用哪个
private RedisTemplate redisTemplate;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值