解决使⽤redisTemplate⾼并发下连接池满的问题
⽤JMeter进⾏⾼并发测试的时候,发现报错:
org.dis.RedisConnectionFailureException: Cannot get Jedis connection;
nested exception is redis.ptions.JedisException: Could not get a resource from the pool
连不上redis,是因为连接池不够⽤了
我⽤的是redisTemplate来操作redis,⽽redisTemplate并不会⾃动释放连接
有⼀个⽅法,就是加⼤最⼤连接数,但是治标不治本,加到redis.maxIdle=1000了,看似够⼤了,但连接数⼀直在增加,迟早会崩
了很久,最后发现这个⽅法可⽤
在使⽤redisTemplate的部分⽤try-catch-finally包起来
在catch-finally中加上,⼿动断开连接,现在就不会报错了
RedisConnectionUtils.ConnectionFactory());
现在设置最⼤连接数redis.maxIdle=100也没事了
在redis-cli中输⼊ info clients 现在的连接数⼤概在⼆三⼗左右
补充知识:Redis 配置连接池,redisTemplate 操作多个db数据库,切换多个db,解决JedisConnectionFactory的设置连接⽅法过时问题。
环境
1、springmvc
2、jdk1.8
3、maven
redis.properties配置⽂件
#redis setting
redis.host=localhost
redis.port=6379
redis.password=
redis.maxIdle=200
redis.minIdle=0
redis.maxActive=50
redis.maxWait=10000
redis.timeout=100000
#定义需要使⽤的db
//#sessionCode DB
sessionCodeDb = 0
//#车辆基本信息 DB
bicycleInfoDb = 15
//#当前位置信息 DB
currentLocationDb = 14
//#锁车/解锁 DB
lockDb = 13
//#根据车牌获取电⼦车牌 DB
ebikeNoDb = 12
//#根据电⼦车牌获取车牌 DB
bikeNoDb = 11
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.1.2.RELEASE</spring.version>
</properties>
<!-- spring4 start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mvnrepository/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.3</version>
</dependency>
<!-- mvnrepository/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.14.RELEASE</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.11</version>
</dependency>
<!-- json-lib -->
<!-- <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier>  </dependency> -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
RedisConfig.java 配置类初始化redis连接池
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.tion.RedisConnectionFactory;
import org.tion.RedisPassword;
import org.tion.RedisStandaloneConfiguration;
import org.tion.jedis.JedisClientConfiguration;
import org.tion.jedis.JedisConnectionFactory;
import org.RedisTemplate;
import org.ValueOperations;
import org.dis.serializer.Jackson2JsonRedisSerializer;
import org.dis.serializer.RedisSerializer;
import org.dis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPoolConfig;
import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import urrent.TimeUnit;
@Component
@Slf4j
public class RedisConfig {
@Value("${redis.host}")
private String hostName;
@Value("${redis.port}")
private int port;
@Value("${redis.password}")
private String passWord;
@Value("${redis.maxIdle}")
private int maxIdl;
@Value("${redis.minIdle}")
private int minIdl;
@Value("${redis.timeout}")
private int timeout;
@Value("${sessionCodeDb}")
private int sessionCodeDb;
@Value("${bicycleInfoDb}")
private int bicycleInfoDb;
@Value("${currentLocationDb}")
private int currentLocationDb;
@Value("${lockDb}")
private int lockDb;
@Value("${ebikeNoDb}")
private int ebikeNoDb;
@Value("${bikeNoDb}")
private int bikeNoDb;
public static Map<Integer,RedisTemplate<Serializable, Object>> redisTemplateMap = new HashMap<>(); @PostConstruct
public void initRedisTemp() throws Exception{
log.info("># START 初始化 Redis 连接池 START >#");
redisTemplateMap.put(sessionCodeDb,redisTemplateObject(sessionCodeDb));
redisTemplateMap.put(bicycleInfoDb,redisTemplateObject(bicycleInfoDb));
redisTemplateMap.put(currentLocationDb,redisTemplateObject(currentLocationDb));
redisTemplateMap.put(lockDb,redisTemplateObject(lockDb));
redisTemplateMap.put(ebikeNoDb,redisTemplateObject(ebikeNoDb));
redisTemplateMap.put(bikeNoDb,redisTemplateObject(bikeNoDb));
log.info("># END 初始化 Redis 连接池 END >#");
}
public RedisTemplate<Serializable, Object> redisTemplateObject(Integer dbIndex) throws Exception {
RedisTemplate<Serializable, Object> redisTemplateObject = new RedisTemplate<Serializable, Object>();  redisTemplateObject.setConnectionFactory(redisConnectionFactory(jedisPoolConfig(),dbIndex));
setSerializer(redisTemplateObject);
redisTemplateObject.afterPropertiesSet();
return redisTemplateObject;
}
/**
* 连接池配置信息
* @return
*/
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig poolConfig=new JedisPoolConfig();
//最⼤连接数
poolConfig.setMaxIdle(maxIdl);
//最⼩空闲连接数
poolConfig.setMinIdle(minIdl);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setNumTestsPerEvictionRun(10);
poolConfig.setTimeBetweenEvictionRunsMillis(60000);
//当池内没有可⽤的连接时,最⼤等待时间
poolConfig.setMaxWaitMillis(10000);
//------其他属性根据需要⾃⾏添加-------------
return poolConfig;
}
/**
* jedis连接⼯⼚
* @param jedisPoolConfig
* @return
*/
public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig,int db) {
//单机版jedis
RedisStandaloneConfiguration redisStandaloneConfiguration =
new RedisStandaloneConfiguration();
//设置redis服务器的host或者ip地址
redisStandaloneConfiguration.setHostName(hostName);
//设置默认使⽤的数据库
redisStandaloneConfiguration.setDatabase(db);
//设置密码
redisStandaloneConfiguration.setPassword(RedisPassword.of(passWord));
//设置redis的服务的端⼝号
redisStandaloneConfiguration.setPort(port);
//获得默认的连接池构造器(怎么设计的,为什么不抽象出单独类,供⽤户使⽤呢)
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =
(JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
//指定jedisPoolConifig来修改默认的连接池构造器(真⿇烦,滥⽤设计模式!)
jpcb.poolConfig(jedisPoolConfig);
//通过构造器来构造jedis客户端配置
JedisClientConfiguration jedisClientConfiguration = jpcb.build();
//单机配置 + 客户端配置 = jedis连接⼯⼚
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
private void setSerializer(RedisTemplate<Serializable, Object> template) {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(    Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.StringSerializer());
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
//在使⽤String的数据结构的时候使⽤这个来更改序列化⽅式
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
template.setKeySerializer(stringSerializer );
template.setValueSerializer(stringSerializer );
template.setHashKeySerializer(stringSerializer );
template.setHashValueSerializer(stringSerializer );
}
/**
* 删除对应的value
* @param key
*/
public void remove(final String key,int db) {
RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
if (exists(key,redisTemplate)) {
redisTemplate.delete(key);
}
}
/**
* 判断缓存中是否有对应的value
* @param key
* @return
*/
public boolean exists(final String key,RedisTemplate<Serializable, Object> redisTemplate) {
return redisTemplate.hasKey(key);
}
/**
* 读取缓存
* @param key
* @return
*/
public Object get(final String key,int db) {
RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = (key);
return result;
}
/**
* 写⼊缓存
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value,int db) {
RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
boolean result = false;
try {git使用详解
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
<("set cache error", e);
}
return result;
}
/**
* 根据key 获取过期时间
* @param key 键不能为null
* @return 返回0代表为永久有效
*/
public long getExpire(String key,TimeUnit unit,int db) {
RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
Expire(key, unit);
}
/
**
* 根据db 获取对应的redisTemplate实例
* @param db
* @return
*/
public RedisTemplate<Serializable, Object> getRedisTemplateByDb(int db){
(db);
}
}
在其他类中的使⽤
@Autowired
private RedisConfig redisUtil;
//#获取db0 数据库的数据
public static Integer sessionCodeDb = 0;
/**
* 根据sessionCode获取userId
* @param sessionCode
* @return
*/
public String getUserIdBySessionCode(String sessionCode){
try {
Object obj = (sessionCode,sessionCodeDb);
if(obj!=null) {
String();
}else{
return null;
}
}catch (Exception e){
e.printStackTrace();
return null;
}
}
以上这篇解决使⽤redisTemplate⾼并发下连接池满的问题就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论