基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作
SpringBoot2.0默認(rèn)采用Lettuce客戶端來連接Redis服務(wù)端的
默認(rèn)是不使用連接池的,只有配置 redis.lettuce.pool下的屬性的時候才可以使用到redis連接池
redis:
cluster:
nodes: ${redis.host.cluster}
password: ${redis.password}
lettuce:
shutdown-timeout: 100 # 關(guān)閉超時時間
pool:
max-active: 8 # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
max-idle: 8 # 連接池中的最大空閑連接
max-wait: 30 # 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
min-idle: 0 # 連接池中的最小空閑連接
沒有這個配置時

增加這個配置時

同時,使用連接池,要依賴commons-pool2
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
如果沒有引入,會報錯

同時如果你想使用jedis客戶端,則需要配置
redis:
cluster:
nodes: ${redis.host.cluster}
password: ${redis.password}
jedis:
pool:
max-active: 8 # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
max-idle: 8 # 連接池中的最大空閑連接
max-wait: 30 # 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制)
min-idle: 0 # 連接池中的最小空閑連接
當(dāng)然你也可以不配置,走默認(rèn)的連接池配置,但是有一點(diǎn)要注意
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
依賴包的引用里,要去掉lettuce,并且加上jedis的依賴包,否則都是走的lettuce客戶端

同時jedis的客戶端默認(rèn)增加了pool的連接池依賴包,所以Jedis默認(rèn)你配置與否都會有連接池,而lettuce則需要配置文件中配置一下
補(bǔ)充知識:解決springboot2 RedisTemplate使用lettuce連接池配置不生效的問題
springboot2 redis默認(rèn)使用lettuce,使用連接池根據(jù)網(wǎng)上的內(nèi)容,進(jìn)行如下配置:
# 連接池最大連接數(shù) 使用負(fù)值表示沒有限制 spring.redis.lettuce.pool.max-active=8 # 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制) spring.redis.lettuce.pool.max-wait=-1 # 連接池中的最大空閑連接 默認(rèn) 8 spring.redis.lettuce.pool.max-idle=8 # 連接池中的最小空閑連接 默認(rèn) 0 spring.redis.lettuce.pool.min-idle=0
但是啟動后,多線程調(diào)用查詢redis,通過redis-cli的info clients。
發(fā)現(xiàn)連接數(shù)并沒有變多。
經(jīng)過翻閱資料和源碼發(fā)現(xiàn),LettuceConnectionFactory類里面有個shareNativeConnection變量,默認(rèn)為true。
說明共享本地連接,這樣的話就不會創(chuàng)建多個連接了,連接池也就沒用了。因此需要把這個值設(shè)為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連接池就設(shè)置成功了。
為什么改這里就可以了呢?從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<byte[], byte[]> sharedConnection, LettuceConnectionProvider connectionProvider,
long timeout, int database) {
return new LettuceConnection(sharedConnection, connectionProvider, timeout, database);
}
上面兩個函數(shù)是獲取connection連接,可以看到getSharedConnection()和shareNativeConnection配置有關(guān)了
@Nullable
protected StatefulRedisConnection<byte[], byte[]> getSharedConnection() {
return shareNativeConnection ? (StatefulRedisConnection) getOrCreateSharedConnection().getConnection() : null;
}
如果是true,就是返回已經(jīng)存在的連接,如果是false,返回null。那就只能創(chuàng)建新的連接了,如下
LettuceConnection(@Nullable StatefulConnection<byte[], byte[]> 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;
}
以上這篇基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot 快速實現(xiàn) api 加密的方法
在項目中,為了保證數(shù)據(jù)的安全,我們常常會對傳遞的數(shù)據(jù)進(jìn)行加密,常用的加密算法包括對稱加密(AES)和非對稱加密(RSA),本文給大家介紹SpringBoot 快速實現(xiàn) api 加密,感興趣的朋友一起看看吧2023-10-10
詳解Java8?CompletableFuture的并行處理用法
Java8中有一個工具非常有用,那就是CompletableFuture,本章主要講解CompletableFuture的并行處理用法,感興趣的小伙伴可以了解一下2022-04-04
SpringBoot集成FastDFS實現(xiàn)防盜鏈功能
FastDFS是一個高性能的分布式?件系統(tǒng),本文將為大家詳細(xì)介紹一下SpringBoot如何集成FastDFS實現(xiàn)防盜鏈功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決
這篇文章主要介紹了Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
myeclipse智能提示設(shè)置的實現(xiàn)方法
本篇文章介紹了,myeclipse智能提示設(shè)置的實現(xiàn)方法。需要的朋友參考下2013-05-05

