解决springboot2 RedisTemplate使用lettuce连接池配置不生效的问题

springboot2 redis默认使用lettuce,使用连接池根据网上的内容,进行如下配置:

# 连接池最大连接数 使用负值表示没有限制
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1
# 连接池中的最大空闲连接 默认 8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接 默认 0
spring.redis.lettuce.pool.min-idle=0

但是启动后,多线程调用查询redis,通过redis-cli的info clients。

发现连接数并没有变多。

经过翻阅资料和源码发现,LettuceConnectionFactory类里面有个shareNativeConnection变量,默认为true。

说明共享本地连接,这样的话就不会创建多个连接了,连接池也就没用了。因此需要把这个值设为false。


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.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;


@Configuration
public class RedisConfig {
    @Resource
    private LettuceConnectionFactory lqlcfactory;
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(connectionFactory);
        return template;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        lqlcfactory.setShareNativeConnection(false);
        stringRedisTemplate.setConnectionFactory(lqlcfactory);
        return stringRedisTemplate;
    }



}

这样lettuce连接池就设置成功了。

为什么改这里就可以了呢?从LettuceConnectionFactory的源码分析

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.redis.connection.RedisConnectionFactory#getConnection()
	 */
	public RedisConnection getConnection() {

		if (isClusterAware()) {
			return getClusterConnection();
		}

		LettuceConnection connection;
		connection = doCreateLettuceConnection(getSharedConnection(), connectionProvider, getTimeout(), getDatabase());
		connection.setConvertPipelineAndTxResults(convertPipelineAndTxResults);
		return connection;
	}
protected LettuceConnection doCreateLettuceConnection(
			@Nullable StatefulRedisConnection sharedConnection, LettuceConnectionProvider connectionProvider,
			long timeout, int database) {

		return new LettuceConnection(sharedConnection, connectionProvider, timeout, database);
	}

上面两个函数是获取connection连接,可以看到getSharedConnection()和shareNativeConnection配置有关了

@Nullable
	protected StatefulRedisConnection getSharedConnection() {
		return shareNativeConnection ? (StatefulRedisConnection) getOrCreateSharedConnection().getConnection() : null;
	}

如果是true,就是返回已经存在的连接,如果是false,返回null。那就只能创建新的连接了,如下

LettuceConnection(@Nullable StatefulConnection sharedConnection,
			LettuceConnectionProvider connectionProvider, long timeout, int defaultDbIndex) {

		Assert.notNull(connectionProvider, "LettuceConnectionProvider must not be null.");

		this.asyncSharedConn = sharedConnection;
		this.connectionProvider = connectionProvider;
		this.timeout = timeout;
		this.defaultDbIndex = defaultDbIndex;
		this.dbIndex = this.defaultDbIndex;
	}

 

你可能感兴趣的:(缓存)