SpringBoot 2.x JedisConnectionFactory(redis.clients.jedis.JedisShardInfo)过时修正

SpringBoot 2.x Redis过时配置的修改方式

在SpringBoox 1.x 中,使用Jedis来配置Redis时可以通过如下方式:

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Value("${spring.redis.timeout}")
    private int redisTimeout;

    @Value("${spring.redis.password}")
    private String redisAuth;

    @Value("${spring.redis.database}")
    private int redisDb;

    @Value("${spring.redis.jedis.pool.max-active}")
    private int maxActive = 20;

    @Value("${spring.redis.jedis.pool.max-wait}")
    private int maxWait = -1;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle = 10;

    @Value("${spring.redis.jedis.pool.min-idle}")
    private int minIdle = 10;

    @Primary
    @Bean("ReidsTemplateDB3")
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
        return setRedisTemplate(redisDb);
    }

    public RedisTemplate setRedisTemplate(int redisDb) {
        // 使用jackson序列化替代jdk序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =
                new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(connectionFactory(redisDb));
        template.setKeySerializer(jackson2JsonRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    /**
     * ehcache默认过期时间
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {

        // 设置缓存有效期一小时
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(1));
        // 开启使用缓存名称最为key前缀
        return RedisCacheManager
                .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
                .cacheDefaults(redisCacheConfiguration).build();
    }

    public RedisConnectionFactory connectionFactory(int redisDb) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMaxWaitMillis(maxWait);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(false);
        poolConfig.setTestWhileIdle(true);
        JedisShardInfo shardInfo = new JedisShardInfo(redisHost, redisPort);
        shardInfo.setPassword(redisAuth);
        shardInfo.setConnectionTimeout(redisTimeout);
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(shardInfo);
        jedisConnectionFactory.setPoolConfig(poolConfig);
        jedisConnectionFactory.setHostName(redisHost);
        jedisConnectionFactory.setPassword(redisAuth);
        jedisConnectionFactory.setPort(redisPort);
        jedisConnectionFactory.setTimeout(redisTimeout);
        jedisConnectionFactory.setDatabase(redisDb);
        return jedisConnectionFactory;
    }

} 
  

而升级到SpringBoot 2.x 的时候会提示JedisConnectionFactory(redis.clients.jedis.JedisShardInfo)已经过时。
在SpringBoot 2.x 中我们可以通过修改上面代码中的connectionFactory(int redisDb)为如下方式

public RedisConnectionFactory connectionFactory(int redisDb) {
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    poolConfig.setMaxTotal(maxActive);
    poolConfig.setMaxIdle(maxIdle);
    poolConfig.setMaxWaitMillis(maxWait);
    poolConfig.setMinIdle(minIdle);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestOnReturn(false);
    poolConfig.setTestWhileIdle(true);

    JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling().poolConfig(poolConfig).and().readTimeout(Duration.ofMillis(redisTimeout)).build();
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    redisStandaloneConfiguration.setDatabase(redisDb);
    redisStandaloneConfiguration.setPort(redisPort);
    redisStandaloneConfiguration.setPassword(RedisPassword.of(redisAuth));
    redisStandaloneConfiguration.setHostName(redisHost);
    return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}

你可能感兴趣的:(SpringBoot,2.x)