欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作

 更新時(shí)間:2020年12月02日 16:46:46   作者:幕紫  
這篇文章主要介紹了基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

SpringBoot2.0默認(rèn)采用Lettuce客戶端來(lái)連接Redis服務(wù)端的

默認(rèn)是不使用連接池的,只有配置 redis.lettuce.pool下的屬性的時(shí)候才可以使用到redis連接池

 redis:
  cluster:
   nodes: ${redis.host.cluster}
  password: ${redis.password}
  lettuce:
   shutdown-timeout: 100 # 關(guān)閉超時(shí)時(shí)間
   pool:
    max-active: 8 # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
    max-idle: 8 # 連接池中的最大空閑連接
    max-wait: 30 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
    min-idle: 0 # 連接池中的最小空閑連接

沒(méi)有這個(gè)配置時(shí)

增加這個(gè)配置時(shí)

同時(shí),使用連接池,要依賴commons-pool2

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

如果沒(méi)有引入,會(huì)報(bào)錯(cuò)

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

 redis:
  cluster:
   nodes: ${redis.host.cluster}
  password: ${redis.password}
  jedis:
   pool:
    max-active: 8 # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
    max-idle: 8 # 連接池中的最大空閑連接
    max-wait: 30 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
    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客戶端

同時(shí)jedis的客戶端默認(rèn)增加了pool的連接池依賴包,所以Jedis默認(rèn)你配置與否都會(huì)有連接池,而lettuce則需要配置文件中配置一下

補(bǔ)充知識(shí):解決springboot2 RedisTemplate使用lettuce連接池配置不生效的問(wèn)題

springboot2 redis默認(rèn)使用lettuce,使用連接池根據(jù)網(wǎng)上的內(nèi)容,進(jìn)行如下配置:

# 連接池最大連接數(shù) 使用負(fù)值表示沒(méi)有限制
spring.redis.lettuce.pool.max-active=8
# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
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

但是啟動(dòng)后,多線程調(diào)用查詢r(jià)edis,通過(guò)redis-cli的info clients。

發(fā)現(xiàn)連接數(shù)并沒(méi)有變多。

經(jīng)過(guò)翻閱資料和源碼發(fā)現(xiàn),LettuceConnectionFactory類里面有個(gè)shareNativeConnection變量,默認(rèn)為true。

說(shuō)明共享本地連接,這樣的話就不會(huì)創(chuàng)建多個(gè)連接了,連接池也就沒(méi)用了。因此需要把這個(gè)值設(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);
 }

上面兩個(gè)函數(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連接池的配置操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中@ConfigurationProperties實(shí)現(xiàn)自定義配置綁定問(wèn)題分析

    Java中@ConfigurationProperties實(shí)現(xiàn)自定義配置綁定問(wèn)題分析

    這篇文章主要介紹了@ConfigurationProperties實(shí)現(xiàn)自定義配置綁定問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • Java詳細(xì)講解不同版本的接口語(yǔ)法和抽象類與接口的區(qū)別

    Java詳細(xì)講解不同版本的接口語(yǔ)法和抽象類與接口的區(qū)別

    對(duì)于面向?qū)ο缶幊虂?lái)說(shuō),抽象是它的一大特征之一,在?Java?中可以通過(guò)兩種形式來(lái)體現(xiàn)OOP的抽象:接口和抽象類,下面這篇文章主要給大家介紹了關(guān)于Java入門(mén)基礎(chǔ)之抽象類與接口的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Spring Security靈活的PasswordEncoder加密方式解析

    Spring Security靈活的PasswordEncoder加密方式解析

    這篇文章主要介紹了Spring Security靈活的PasswordEncoder加密方式解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 詳解idea從git上拉取maven項(xiàng)目詳細(xì)步驟

    詳解idea從git上拉取maven項(xiàng)目詳細(xì)步驟

    這篇文章主要介紹了詳解idea從git上拉取maven項(xiàng)目詳細(xì)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的緩存方法

    Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的緩存方法

    本篇文章主要介紹了Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的緩存方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • 關(guān)于單例模式懶漢式和餓漢式的區(qū)別及說(shuō)明

    關(guān)于單例模式懶漢式和餓漢式的區(qū)別及說(shuō)明

    這篇文章主要介紹了關(guān)于單例模式懶漢式和餓漢式的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 你知道Java中的注解可以繼承嗎?

    你知道Java中的注解可以繼承嗎?

    注解想必大家都用過(guò),也叫元數(shù)據(jù),是一種代碼級(jí)別的注釋,可以對(duì)類或者方法等元素做標(biāo)記說(shuō)明。那么今天我想問(wèn)大家的是類被繼承了,注解能否繼承呢?可能會(huì)和大家想的不一樣,感興趣的可以往下看
    2022-12-12
  • intellij idea中安裝、配置mybatis插件Free Mybatis plugin的教程詳解

    intellij idea中安裝、配置mybatis插件Free Mybatis plugin的教程詳解

    這篇文章主要介紹了intellij idea中安裝、配置mybatis插件Free Mybatis plugin的教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • java關(guān)鍵字final使用方法詳解

    java關(guān)鍵字final使用方法詳解

    在程序設(shè)計(jì)中,我們有時(shí)可能希望某些數(shù)據(jù)是不能夠改變的,這個(gè)時(shí)候final就有用武之地了。final是java的關(guān)鍵字,本文就詳細(xì)說(shuō)明一下他的使用方法
    2013-11-11
  • Java數(shù)據(jù)結(jié)構(gòu)與算法之稀疏數(shù)組與隊(duì)列深入理解

    Java數(shù)據(jù)結(jié)構(gòu)與算法之稀疏數(shù)組與隊(duì)列深入理解

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)與算法之稀疏數(shù)組與隊(duì)列深入理解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09

最新評(píng)論