怎么在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 # 連接池中的最小空閑連接
同時,使用連接池,要依賴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 # 連接池最大連接數(使用負值表示沒有限制) 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客戶端
同時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, connectionProvider, timeout, database); }
上面兩個函數是獲取connection連接,可以看到getSharedConnection()和shareNativeConnection配置有關了
@Nullable protected StatefulRedisConnection<byte[], byte[]> getSharedConnection() { return shareNativeConnection ? (StatefulRedisConnection) getOrCreateSharedConnection().getConnection() : null; }
如果是true,就是返回已經存在的連接,如果是false,返回null。那就只能創建新的連接了,如下
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; }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。