您好,欢迎来到五一七教育网。
搜索
您的当前位置:首页基于SpringBoot2.0默认使用Redis连接池的配置操作

基于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 # 连接池中的最⼩空闲连接

没有这个配置时

增加这个配置时

同时,使⽤连接池,要依赖commons-pool2

org.apache.commons commons-pool2

如果没有引⼊,会报错

同时如果你想使⽤jedis客户端,则需要配置

redis: cluster:

nodes: ${redis.host.cluster} password: ${redis.password} jedis: pool:

max-active: 8 # 连接池最⼤连接数(使⽤负值表⽰没有) max-idle: 8 # 连接池中的最⼤空闲连接

max-wait: 30 # 连接池最⼤阻塞等待时间(使⽤负值表⽰没有) min-idle: 0 # 连接池中的最⼩空闲连接

当然你也可以不配置,⾛默认的连接池配置,但是有⼀点要注意

org.springframework.boot

spring-boot-starter-data-redis

io.lettuce

lettuce-core

redis.clients jedis

依赖包的引⽤⾥,要去掉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# 连接池中的最⼤空闲连接 默认 8spring.redis.lettuce.pool.max-idle=8# 连接池中的最⼩空闲连接 默认 0spring.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; }

以上这篇基于SpringBoot2.0默认使⽤Redis连接池的配置操作就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 517ttc.cn 版权所有 赣ICP备2024042791号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务