关于Redis配置哨兵时,默认连接池参数不生效的配置

package com.lexue.coupon.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
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.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: ShuYuLiu
 * @Description: 实例化redisTemplete
 * @Date: 11:19 2019/7/04
 *
 **/
@Configuration
public class RedisConfig{
    @Autowired
    RedisProperties  redisProperties;
    @Value("${spring.redis.pool.max-idle}")
    private Integer maxIdle;
    @Value("${spring.redis.pool.min-idle}")
    private Integer minIdle;
    @Value("${spring.redis.pool.max-wait}")
    private Integer  maxWait;

    @Value("${spring.redis.pool.max-active}")
    private Integer  maxActive;

    @Bean(name = "jedisPoolConfig")
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle);
        config.setMaxTotal(maxActive);
        config.setMaxWaitMillis(maxWait);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(true);
        config.setTestWhileIdle(true);
        config.setNumTestsPerEvictionRun(2);
        config.setTimeBetweenEvictionRunsMillis(30000);
        config.setMinEvictableIdleTimeMillis(60000);
        config.setSoftMinEvictableIdleTimeMillis(60000);
        return config;
    }

    @Bean(name = "redisSentinelConfiguration")
    public RedisSentinelConfiguration getRedisSentinelConfiguration(){
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        //配置redis的sentinel
        Set redisNodeSet = new HashSet<>();
        List nodes = redisProperties.getSentinel().getNodes();
        if(nodes!=null && nodes.size()>0){
            nodes.forEach(redisNode -> {
                String[] split = redisNode.split(":");
                RedisNode redisNodes = new RedisNode(split[0],Integer.parseInt(split[1]));
                redisNodeSet.add(redisNodes);
            });
        }
        redisSentinelConfiguration.setSentinels(redisNodeSet);
        redisSentinelConfiguration.setMaster(redisProperties.getSentinel().getMaster());
        return redisSentinelConfiguration;

    }

    @Bean(name = "jedisConnectionFactory")
    public JedisConnectionFactory  redisConnectionFactory(@Qualifier("redisSentinelConfiguration")RedisSentinelConfiguration redisSentinelConfiguration,
                                                         @Qualifier("jedisPoolConfig")JedisPoolConfig  jedisPoolConfig){
        JedisConnectionFactory jedisConnectionFactory=new JedisConnectionFactory(redisSentinelConfiguration,jedisPoolConfig);
        jedisConnectionFactory.setDatabase(redisProperties.getDatabase());
        jedisConnectionFactory.setHostName(redisProperties.getHost());
        jedisConnectionFactory.setPort(redisProperties.getPort());
        jedisConnectionFactory.setPassword(redisProperties.getPassword());
        jedisConnectionFactory.setTimeout(0);
        jedisConnectionFactory.setUsePool(true);
        return jedisConnectionFactory;
    }


    @Bean
    public RedisTemplate redisTemplate(@Qualifier("jedisConnectionFactory")JedisConnectionFactory jedisConnectionFactory)
    {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(jedisConnectionFactory);
        template.setKeySerializer(jackson2JsonRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}
 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(Redis)