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,說明未使用連接池