lettuce是redis连接池未来的发展趋势,在spring中集成lettuce需要引入两个依赖,lettuce和spring-data-redis,引入是注意版本的关系,根据maven中央仓库的版本提示选取即可。
因为项目依赖版本的原因,网上的资料五花八门,我使用的是lettuce3.3.Final和spring-data-redis1.6.2.RELEASE依赖,因为版本原因不少API都不同,导致网上大多数资料都有问题,以下配置方式是我根据源码整理出的配置,欢迎指导。
下面为在spring中集成lettuce的代码,这里使用的是java配置的方式来集成lettuce。

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.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.DefaultLettucePool;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePool;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * description:
 * 使用spring-data-redis
 * 连接池为lettuce
 * 包括lettuce连接池的配置以及Redistemplate和StringRedistemplate
 *
 * @author wkGui
 */
@Configuration
public class RedisConfiguration {

    @Value("${redis.hostname}")
    private String hostname;
    @Value("${redis.port}")
    private int port;
    @Value("${redis.password}")
    private String password;
    @Value("${redis.maxTotal}")
    private int maxTotal;
    @Value("${redis.maxIdle}")
    private int maxIdle;
    @Value("${redis.minIdle}")
    private int minIdle;
    @Value("${redis.blockWhenExhausted}")
    private boolean isBlockWhenExhausted;
    @Value("${redis.maxWait}")
    private int maxWait;
    @Value("${redis.testOnBorrow}")
    private boolean isTestOnBorrow;
    @Value("${redis.testOnReturn}")
    private boolean isTestOnReturn;
    @Value("${redis.testWhileIdle}")
    private boolean isTestWhileIdle;
    @Value("${redis.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;
    @Value("${redis.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;
    @Value("${redis.dbIndex}")
    private int dbIndex;

    @Bean
    public StringRedisSerializer getStringRedisSerializer(){
        return new StringRedisSerializer();
    }

    /**
     * 配置连接池参数
     *
     * @return GenericObjectPool
     */
    @Bean
    public GenericObjectPoolConfig getRedisConfig() {
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(maxIdle);
        genericObjectPoolConfig.setMaxTotal(maxTotal);
        genericObjectPoolConfig.setMinIdle(minIdle);
        //连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
        genericObjectPoolConfig.setBlockWhenExhausted(isBlockWhenExhausted);
        genericObjectPoolConfig.setMaxWaitMillis(maxWait);
        //在borrow一个实例时,是否提前进行alidate操作;如果为true,则得到的实例均是可用的
        genericObjectPoolConfig.setTestOnBorrow(isTestOnBorrow);
        //调用returnObject方法时,是否进行有效检查
        genericObjectPoolConfig.setTestOnReturn(isTestOnReturn);
        //在空闲时检查有效性, 默认false
        genericObjectPoolConfig.setTestWhileIdle(isTestWhileIdle);
        //表示idle object evitor两次扫描之间要sleep的毫秒数;
        genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        //表示一个对象至少停留在idle状态的最短时间,
        //然后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;
        genericObjectPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

        return genericObjectPoolConfig;
    }

    /**
     * 生成连接池
     *
     * @param poolConfig 连接池配置
     * @return DefaultLettucePool
     */
    @Bean
    public DefaultLettucePool getDefaultLettucePool(GenericObjectPoolConfig poolConfig) {
        DefaultLettucePool defaultLettucePool = new DefaultLettucePool(hostname, port, poolConfig);
        defaultLettucePool.setPassword(password);
        defaultLettucePool.afterPropertiesSet();
        return defaultLettucePool;
    }

    /**
     * lettuce 连接工厂配置
     *
     * @return LettuceConnectionFactory implement RedisConnectionFactory
     */
    @Bean
    public LettuceConnectionFactory getLettuceConnectionFactory(LettucePool pool) {
        LettuceConnectionFactory factory = new LettuceConnectionFactory(pool);
        //校验连接是否有效
        factory.setValidateConnection(true);
        //选择数据库
        factory.setDatabase(dbIndex);
        factory.setTimeout(maxWait);
        factory.afterPropertiesSet();
        return factory;
    }

    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory factory, StringRedisSerializer stringRedisSerializer) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //对key采用String的序列化方式--统一
        redisTemplate.setKeySerializer(stringRedisSerializer);
        //事务支持
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    @Bean(name = "stringRedisTemplate")
    public StringRedisTemplate getStringRedisTemplate(RedisConnectionFactory factory, StringRedisSerializer stringRedisSerializer) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        //对key采用String的序列化方式--统一
        stringRedisTemplate.setKeySerializer(stringRedisSerializer);
        //事务支持
        stringRedisTemplate.setEnableTransactionSupport(true);
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }

    /**
     * 缓存管理器 使用redisTemplate操作
     */
    @Bean
    public RedisCacheManager getRedisCacheManager(RedisTemplate<String, Object> redisTemplate){
        return new RedisCacheManager(redisTemplate);
    }
}

redis.properties配置文件

redis.hostname=www.xxxxx.com
redis.port=6379
redis.password=xxxxxx
redis.maxTotal=16
redis.maxIdle=8
redis.minIdle=8
redis.dbIndex=2
#连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
redis.blockWhenExhausted=true
#获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
redis.maxWait=10000
#在borrow一个实例时,是否提前进行alidate操作;如果为true,则得到的实例均是可用的;
redis.testOnBorrow=true
#调用returnObject方法时,是否进行有效检查 #
redis.testOnReturn=true
#在空闲时检查有效性, 默认false
redis.testWhileIdle=true
#表示idle object evitor两次扫描之间要sleep的毫秒数;
redis.timeBetweenEvictionRunsMillis=30000
#表示一个对象至少停留在idle状态的最短时间,
#然后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义;
redis.minEvictableIdleTimeMillis=10000