jedis pool的配置其实是采用 org.apache.commons.pool2.impl.GenericObjectPoolConfig类的配置项。

jedis 2.9版本代码如下:

package redis.clients.jedis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public class JedisPoolConfig extends GenericObjectPoolConfig {
public JedisPoolConfig() {
// defaults to make your life with connection pool easier :)
setTestWhileIdle(true);
setMinEvictableIdleTimeMillis(60000);
setTimeBetweenEvictionRunsMillis(30000);
setNumTestsPerEvictionRun(-1);
}
}

而springboot的自动装配中对redis连接池的配置:

代码位置:org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool

/**
* Pool properties.
*/
public static class Pool { /**
* Max number of "idle" connections in the pool. Use a negative value to indicate
* an unlimited number of idle connections.
*/
private int maxIdle = 8; /**
* Target for the minimum number of idle connections to maintain in the pool. This
* setting only has an effect if it is positive.
*/
private int minIdle = 0; /**
* Max number of connections that can be allocated by the pool at a given time.
* Use a negative value for no limit.
*/
private int maxActive = 8; /**
* Maximum amount of time (in milliseconds) a connection allocation should block
* before throwing an exception when the pool is exhausted. Use a negative value
* to block indefinitely.
*/
private int maxWait = -1; public int getMaxIdle() {
return this.maxIdle;
} public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
} public int getMinIdle() {
return this.minIdle;
} public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
} public int getMaxActive() {
return this.maxActive;
} public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
} public int getMaxWait() {
return this.maxWait;
} public void setMaxWait(int maxWait) {
this.maxWait = maxWait;
} }

问题就出现了:通过spring.redis.pool.xxx的自动装配的配置key其实就比jedis的pool的配置key要少很多,当redis服务端设置了连接空闲的最大时间时,redis服务会kill掉符合条件的空闲的链接,此时客户端的连接池并不会感知连接被kill,当有代码调用pool获取连接时可能会返回一个失效的连接对象,从而导致代码报错。

解决方案:不使用默认的装配。

JedisPoolConfig config = new JedisPoolConfig();

// 最小空闲连接数
config.setMinIdle(props.getMinIdle());
// 最大空闲连接数
config.setMaxIdle(props.getMaxIdle());
// 连接池最大连接数
config.setMaxTotal(props.getMaxActive());
// 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
config.setMaxWaitMillis(props.getMaxWait());
// 在空闲时检查有效性
config.setTestWhileIdle(props.isTestWhileIdle());
// 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
config.setTestOnBorrow(props.isTestOnBorrow());
// 在return给pool时,是否提前进行validate操作
config.setTestOnReturn(props.isTestOnReturn()); // 表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
// 该值参考redis server的配置来进行配置
config.setMinEvictableIdleTimeMillis(props.getTimeout());
// 表示idle object evitor两次扫描之间的间隔时间,-1表示不开启这个task
config.setTimeBetweenEvictionRunsMillis(props.getTimeout()/2);
// 表示idle object evitor每次扫描的最多的对象数
// 建议设置和maxTotal一样大,这样每次可以有效检查所有的链接.
config.setNumTestsPerEvictionRun(props.getNumTestsPerEvictionRun());

注意:timeBetweenEvictionRunsMillis和minEvictableIdleTimeMillis的总和应小于 数据库设置的 超时空闲失效时间

如果将 testOnBorrow 和 testOnReturn设置为true将会加重服务的负担,降低服务的性能,最好是通过合理的配置 testWhileIdle、minEvictableIdleTimeMillis、timeBetweenEvictionRunsMillis、numTestsPerEvictionRun来达到达到失效连接的清除工作。

springboot自动装配redis在pool下偶尔出现连接异常的问题的更多相关文章

  1. springboot自动装配原理,写一个自己的start

    springboot自动装配原理 第一次使用springboot的时候,都感觉很神奇.只要加入一个maven的依赖,写几行配置,就能注入redisTemple,rabbitmqTemple等对象. 这 ...

  2. SpringBoot自动装配-自定义Start

    SpringBoot自动装配 在没有使用SpringBoot之前,使用ssm时配置redis需要在XML中配置端口号,地址,账号密码,连接池等等,而使用了SpringBoot后只需要在applicat ...

  3. springboot自动装配

    Spring Boot自动配置原理 springboot自动装配 springboot配置文件 Spring Boot的出现,得益于“习惯优于配置”的理念,没有繁琐的配置.难以集成的内容(大多数流行第 ...

  4. 一步步从Spring Framework装配掌握SpringBoot自动装配

    目录 Spring Framework模式注解 Spring Framework@Enable模块装配 Spring Framework条件装配 SpringBoot 自动装配 本章总结 Spring ...

  5. SpringBoot启动流程分析(五):SpringBoot自动装配原理实现

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  6. springboot自动装配(3)---条件注解@Conditional

    之前有说到springboot自动装配的时候,都是去寻找一个XXXAutoConfiguration的配置类,然而我们的springboot的spring.factories文件中有各种组件的自动装配 ...

  7. SpringBoot自动装配原理解析

    本文包含:SpringBoot的自动配置原理及如何自定义SpringBootStar等 我们知道,在使用SpringBoot的时候,我们只需要如下方式即可直接启动一个Web程序: @SpringBoo ...

  8. Spring Boot之从Spring Framework装配掌握SpringBoot自动装配

    Spring Framework模式注解 模式注解是一种用于声明在应用中扮演“组件”角色的注解.如 Spring Framework 中的 @Repository 标注在任何类上 ,用于扮演仓储角色的 ...

  9. springboot自动装配原理

    最近开始学习spring源码,看各种文章的时候看到了springboot自动装配实现原理.用自己的话简单概括下. 首先打开一个基本的springboot项目,点进去@SpringBootApplica ...

随机推荐

  1. day 26作业

    作业 1.整理TCP三次握手.四次挥手图 三次握手 起初A和B都处于CLOSED状态--B创建TCB,处于LISTEN状态,等待A请求--A创建TCB,发送连接请求(SYN=1,seq=x),进入SY ...

  2. MySQL高可用架构应该考虑什么? 你认为应该如何设计?

    一.MySQL高可用架构应该考虑什么? 对业务的了解,需要考虑业务对数据库一致性要求的敏感程度,切换过程中是否有事务会丢失 对于基础设施的了解,需要了解基础设施的高可用的架构.例如 单网线,单电源等情 ...

  3. H3C 帧聚合

  4. Python并发编程-GIL全局解释器锁

    Python并发编程-GIL全局解释器锁 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.GIL全局解释器锁概述 CPython 在解释器进程级别有一把锁,叫做GIL,即全局解释 ...

  5. go语言笔记2

    上接Go语言学习笔记(一) 11    Go错误处理11.1    nil函数通常在最后的返回值中返回错误信息.使用errors.New 可返回一个错误信息: package main         ...

  6. 基于Java+Selenium的WebUI自动化测试框架(一)---页面元素定位器

    对于自动化测试,尤其是UI的自动化测试.是很多做黑盒功能测试的同学,入门自动化测试一个最为直观的或者说最容易理解的途径之一. 对于手工测试和自动化测试的优劣,网上有很多论述,在这里不作展开讨论.但是, ...

  7. docker镜像里的tag那些事--alpine,slim,stretch,jessie

    https://stackoverflow.com/questions/54954187/docker-images-types-slim-vs-slim-stretch-vs-stretch-vs- ...

  8. P3375 模板 KMP字符串匹配

    P3375 [模板]KMP字符串匹配 来一道模板题,直接上代码. #include <bits/stdc++.h> using namespace std; typedef long lo ...

  9. Celery(异步任务,定时任务,周期任务)

    1.什么是Celery Celery是基于Python实现的模块,用于异步.定时.周期任务的. 组成结构: 1.用户任务 app 2.管道broker 用于存储任务 官方推荐 redis/rabbit ...

  10. pipy配置镜像源

    新电脑第一次使用使用pip命令下载贼慢 我们需要使用国内pipy镜像,参考如下 https://mirrors.tuna.tsinghua.edu.cn/help/pypi/ 所以只要设置一下就行了: ...