springboot使用lettuce连接池
springboot对连接池的使用非常智能,配置文件中添加lettuce.pool相关配置,则会使用到lettuce连接池,并将相关配置设置为连接池相关参数,(前提是这些参数是springboot配置文件中内置的,使用自定义参数应该也是可以的,有时间在研究),否则不使用,通过断点调试查看
如过使用redis连接池(无论lettuce还是jedis客户端,都需要),则需要导入如下依赖
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
一、使用lettuce连接池
server.ip=192.168.102.186 spring.redis.host=${server.ip} spring.redis.port=6379 spring.redis.timeout=20000 spring.redis.lettuce.pool.max-idle=10 spring.redis.lettuce.pool.min-idle=10 spring.redis.lettuce.pool.max-active=20 spring.redis.lettuce.pool.max-wait=10000
查看connectionFactory中相关参数
maxIdle和minIdle均为10,maxTotal为20(对应配置中max-active),maxWaitMillis为10000(对应配置中max-wait),与设置一致
查看org.apache.commons.pool2.impl.GenericObjectPoolConfig源码,如下几个字段需要注意
public class GenericObjectPoolConfig<T> extends BaseObjectPoolConfig<T> { /** * The default value for the {@code maxTotal} configuration attribute. * @see GenericObjectPool#getMaxTotal() */ public static final int DEFAULT_MAX_TOTAL = 8; /** * The default value for the {@code maxIdle} configuration attribute. * @see GenericObjectPool#getMaxIdle() */ public static final int DEFAULT_MAX_IDLE = 8; /** * The default value for the {@code minIdle} configuration attribute. * @see GenericObjectPool#getMinIdle() */ public static final int DEFAULT_MIN_IDLE = 0; private int maxTotal = DEFAULT_MAX_TOTAL; private int maxIdle = DEFAULT_MAX_IDLE; private int minIdle = DEFAULT_MIN_IDLE; /** * Get the value for the {@code maxTotal} configuration attribute * for pools created with this configuration instance. * * @return The current setting of {@code maxTotal} for this * configuration instance * * @see GenericObjectPool#getMaxTotal() */ public int getMaxTotal() { return maxTotal; } /** * Set the value for the {@code maxTotal} configuration attribute for * pools created with this configuration instance. * * @param maxTotal The new setting of {@code maxTotal} * for this configuration instance * * @see GenericObjectPool#setMaxTotal(int) */ public void setMaxTotal(final int maxTotal) { this.maxTotal = maxTotal; } .....
默认maxIdle为8,minIdle为0,maxTotal为8
查看GenericObjectPoolConfig父类BaseObjectPoolConfig
public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Cloneable { /** * The default value for the {@code lifo} configuration attribute. * @see GenericObjectPool#getLifo() * @see GenericKeyedObjectPool#getLifo() */ public static final boolean DEFAULT_LIFO = true; /** * The default value for the {@code fairness} configuration attribute. * @see GenericObjectPool#getFairness() * @see GenericKeyedObjectPool#getFairness() */ public static final boolean DEFAULT_FAIRNESS = false; /** * The default value for the {@code maxWait} configuration attribute. * @see GenericObjectPool#getMaxWaitMillis() * @see GenericKeyedObjectPool#getMaxWaitMillis() */ public static final long DEFAULT_MAX_WAIT_MILLIS = -1L; ......
maxWaitMillis默认值为-1
二、去掉lettuce pool相关配置
connectionProvider下已经不存在poolConfig,说明未使用连接池