用JMeter进行高并发测试的时候,发现报错:
org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection;
nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
连不上redis,是因为连接池不够用了
我用的是redisTemplate来操作redis,而redisTemplate并不会自动释放连接
有一个方法,就是加大最大连接数,但是治标不治本,加到redis.maxIdle=1000了,看似够大了,但连接数一直在增加,迟早会崩
找了很久,最后发现 这个方法可用
在使用redisTemplate的部分用try-catch-finally包起来
在catch-finally中加上,手动断开连接,现在就不会报错了
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
现在设置最大连接数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.testOnBorrow=true redis.timeout=100000 #定义需要使用的db //#sessionCode DB sessionCodeDb = 0 //#车辆基本信息 DB bicycleInfoDb = 15 //#当前位置信息 DB currentLocationDb = 14 //#锁车/解锁 DB lockDb = 13 //#根据车牌获取电子车牌 DB ebikeNoDb = 12 //#根据电子车牌获取车牌 DB bikeNoDb = 11
pom.xml依赖
<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> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.3</version> </dependency> <!-- https://mvnrepository.com/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; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.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 java.util.concurrent.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); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setKeySerializer(template.getStringSerializer()); 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 = operations.get(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 { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { log.error("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); return redisTemplate.getExpire(key, unit); } /** * 根据db 获取对应的redisTemplate实例 * @param db * @return */ public RedisTemplate<Serializable, Object> getRedisTemplateByDb(int db){ return redisTemplateMap.get(db); } }
在其他类中的使用
@Autowired private RedisConfig redisUtil; //#获取db0 数据库的数据 public static Integer sessionCodeDb = 0; /** * 根据sessionCode获取userId * @param sessionCode * @return */ public String getUserIdBySessionCode(String sessionCode){ try { Object obj = redisUtil.get(sessionCode,sessionCodeDb); if(obj!=null) { return obj.toString(); }else{ return null; } }catch (Exception e){ e.printStackTrace(); return null; } }
以上这篇解决使用redisTemplate高并发下连接池满的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/201269/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)