springboot自动装配redis在pool下偶尔出现连接异常的问题
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下偶尔出现连接异常的问题的更多相关文章
- springboot自动装配原理,写一个自己的start
springboot自动装配原理 第一次使用springboot的时候,都感觉很神奇.只要加入一个maven的依赖,写几行配置,就能注入redisTemple,rabbitmqTemple等对象. 这 ...
- SpringBoot自动装配-自定义Start
SpringBoot自动装配 在没有使用SpringBoot之前,使用ssm时配置redis需要在XML中配置端口号,地址,账号密码,连接池等等,而使用了SpringBoot后只需要在applicat ...
- springboot自动装配
Spring Boot自动配置原理 springboot自动装配 springboot配置文件 Spring Boot的出现,得益于“习惯优于配置”的理念,没有繁琐的配置.难以集成的内容(大多数流行第 ...
- 一步步从Spring Framework装配掌握SpringBoot自动装配
目录 Spring Framework模式注解 Spring Framework@Enable模块装配 Spring Framework条件装配 SpringBoot 自动装配 本章总结 Spring ...
- SpringBoot启动流程分析(五):SpringBoot自动装配原理实现
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- springboot自动装配(3)---条件注解@Conditional
之前有说到springboot自动装配的时候,都是去寻找一个XXXAutoConfiguration的配置类,然而我们的springboot的spring.factories文件中有各种组件的自动装配 ...
- SpringBoot自动装配原理解析
本文包含:SpringBoot的自动配置原理及如何自定义SpringBootStar等 我们知道,在使用SpringBoot的时候,我们只需要如下方式即可直接启动一个Web程序: @SpringBoo ...
- Spring Boot之从Spring Framework装配掌握SpringBoot自动装配
Spring Framework模式注解 模式注解是一种用于声明在应用中扮演“组件”角色的注解.如 Spring Framework 中的 @Repository 标注在任何类上 ,用于扮演仓储角色的 ...
- springboot自动装配原理
最近开始学习spring源码,看各种文章的时候看到了springboot自动装配实现原理.用自己的话简单概括下. 首先打开一个基本的springboot项目,点进去@SpringBootApplica ...
随机推荐
- Navicat链接数据库报错1130解决方案
1.背景 使用localhost 可以正常连接mysql服务器,但是使用ip地址连接保存如下: 2.问题原因 默认情况下root用户只允许本机访问,即使用localhost访问,如下图: 解决方案:将 ...
- vue.js 样式绑定
简单用法 <div v-bind:height="bindStyle"> 复杂用法 <div v-bind:style="bindStyle" ...
- Linux命令——getfacl、setfacl
简介 ACL是Access Control List的缩写,传统的Linux权限只能针对一个用户.一个群组及非此群组的其他人设置权限而已,无法针对单一用户或个人来设计权限.ACL可以对权限进行更细致的 ...
- Linux命令——parted
参考:8 Linux ‘Parted’ Commands to Create, Resize and Rescue Disk Partitions 简介 parted是磁盘分区操作工具,支持多种磁盘分 ...
- The Instruction Set In Cortex-M3
The Cortex-M3 supports the Thumb-2 instruction set. This is one of the most important features of th ...
- Python应用之-修改通讯录
#-*- coding:utf-8 -*- import sqlite3 #打开本地数据库用于存储用户信息 conn = sqlite3.connect('mysql_person.db') #在该数 ...
- websocket简单样例
服务端 var server = require('ws').Server; }); serv.on('connection',function(socket){ socket.send('hello ...
- django-使用类视图
视图函数views.py中 from django.shortcuts import render, redirect from django.http import HttpResponse, Js ...
- 域权限维持:Ptk(Pass The Ticket)
Kerberos协议传送门:https://www.cnblogs.com/zpchcbd/p/11707302.html 1.黄金票据 2.白银票据
- mysql udf提权
参考文章:https://blog.csdn.net/q1352483315/article/details/98483668