1. 程式人生 > 程式設計 >基於SpringBoot2.0預設使用Redis連線池的配置操作

基於SpringBoot2.0預設使用Redis連線池的配置操作

SpringBoot2.0預設採用Lettuce客戶端來連線Redis服務端的

預設是不使用連線池的,只有配置 redis.lettuce.pool下的屬性的時候才可以使用到redis連線池

 redis:
  cluster:
   nodes: ${redis.host.cluster}
  password: ${redis.password}
  lettuce:
   shutdown-timeout: 100 # 關閉超時時間
   pool:
    max-active: 8 # 連線池最大連線數(使用負值表示沒有限制)
    max-idle: 8 # 連線池中的最大空閒連線
    max-wait: 30 # 連線池最大阻塞等待時間(使用負值表示沒有限制)
    min-idle: 0 # 連線池中的最小空閒連線

沒有這個配置時

基於SpringBoot2.0預設使用Redis連線池的配置操作

增加這個配置時

基於SpringBoot2.0預設使用Redis連線池的配置操作

同時,使用連線池,要依賴commons-pool2

   <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>

如果沒有引入,會報錯

基於SpringBoot2.0預設使用Redis連線池的配置操作

同時如果你想使用jedis客戶端,則需要配置

 redis:
  cluster:
   nodes: ${redis.host.cluster}
  password: ${redis.password}
  jedis:
   pool:
    max-active: 8 # 連線池最大連線數(使用負值表示沒有限制)
    max-idle: 8 # 連線池中的最大空閒連線
    max-wait: 30 # 連線池最大阻塞等待時間(使用負值表示沒有限制)
    min-idle: 0 # 連線池中的最小空閒連線

當然你也可以不配置,走預設的連線池配置,但是有一點要注意

 <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客戶端

基於SpringBoot2.0預設使用Redis連線池的配置操作

同時jedis的客戶端預設增加了pool的連線池依賴包,所以Jedis預設你配置與否都會有連線池,而lettuce則需要配置檔案中配置一下

補充知識:解決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<byte[],byte[]> sharedConnection,LettuceConnectionProvider connectionProvider,long timeout,int database) {
 
 return new LettuceConnection(sharedConnection,timeout,database);
 }

上面兩個函式是獲取connection連線,可以看到getSharedConnection()和shareNativeConnection配置有關了

@Nullable
 protected StatefulRedisConnection<byte[],byte[]> getSharedConnection() {
 return shareNativeConnection ? (StatefulRedisConnection) getOrCreateSharedConnection().getConnection() : null;
 }

如果是true,就是返回已經存在的連線,如果是false,返回null。那就只能建立新的連線了,如下

LettuceConnection(@Nullable StatefulConnection<byte[],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預設使用Redis連線池的配置操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。