redis连接数高居不下,怎么破?。。。。这么破
2024-08-27 05:11:45
最近项目一直在使用redis,首次用redis,随便从网上找了例子就用了,一开始用的还挺正常,后来发现,当客户端访问量一上来,redis的连接数居高不下,一开始以为是客户端没有关闭,开始怀疑redis-pool有问题,就修改了/etc/redis/6379.cnf中的timeout=5(默认是0,服务端不主动关闭连接),修改之后发现close_wait批量出现。
经过分析,肯定不是redis的问题了,肯定是自己代码有的逻辑是没有关闭redis的,经过排查,果然有很多redis因为逻辑关系没有关闭,所以建议大家如果并发量不是很大的话,还是直接在操作redis的时候重新获取redis,然后关闭redis即可,这样就不会出现没有关闭redis的情况了。
对了,使用的redis版本也要对应呦,比如服务端装的是3.0.2,引入的jedis.jar的版本就不能太低,之前我们用的2.1,就经常出现类型转换错误(其实代码中的转换没有问题),我们换成最新的2.7之后,就不报类型转换错误了。
package com.jovision.redisDao; import java.util.List;
import java.util.Map;
import java.util.Set; import org.apache.log4j.Logger; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import com.jovision.Exception.RedisException;
import com.jovision.system.ConfigBean;
/**
*
* @Title: redisFactory.java
* @Package com.jovision.redisDao
* @author Joker(张凯)
* @Description: TODO()
* @date 2015-9-30 上午11:49:09
*/
public class redisFactory {
private static Logger logger = Logger.getLogger(redisFactory.class); public static ConfigBean configBean;
public static String redisIP ;
public static String redisPort ;
public static JedisPool pool;
static
{
//连接redis,连接失败时抛异常
try
{
configBean = ConfigBean.getInstace();
redisIP = configBean.getRedisIP();
redisPort = configBean.getRedisPort();
if (pool == null)
{
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(10);
config.setMaxTotal(200);
config.setMaxWaitMillis(1000 * 10);
config.setTestOnBorrow(true);
pool = new JedisPool(config, redisIP, Integer.parseInt(redisPort),10000);
}
}
catch (Exception e)
{
e.printStackTrace();
logger.error("redis配置异常", e);
//throw new RedisException("redis异常!");
} } /**
* 初始化Redis连接池
*//*
private static void initialPool(){
try {
configBean = ConfigBean.getInstace();
redisIP = configBean.getRedisIP();
redisPort = configBean.getRedisPort();
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000*10);
config.setTestOnBorrow(true);
pool = new JedisPool(config,redisIP, Integer.parseInt(redisPort));
} catch (Exception e) {
logger.error("create JedisPool error : "+e);
}
} public synchronized static Jedis getJedis() {
if (pool == null) {
poolInit();
}
Jedis jedis = null;
try {
if (pool != null) {
jedis = pool.getResource();
}
} catch (Exception e) {
logger.error("Get jedis error : "+e);
}finally{
returnResource(pool, jedis);
}
System.out.println("pool:---------------------------------"+pool);
return jedis;
} *//**
* 在多线程环境同步初始化
*//*
private static synchronized void poolInit() {
if (pool == null) {
initialPool();
}
}*/ public static void main(String[] args) throws Exception {
/*System.out.println(redisIP);
setSingleData("123","123");
System.out.println(getSingleData("123"));*/
//put2Set(0,"test11", "123","456","789");
//System.out.println(getOneSetData("test","123"));
//dev_type dev_version dev_username dev_password
/*hset(8, "B176218924", "dev_type", "2");
hset(8, "B176218924", "dev_version", "v1.1");
hset(8, "B176218924", "dev_username", "abc");
hset(8, "B176218924", "dev_password", "123");
hset(8, "B176218924", "dev_nickname", "B176218924");*/
//setTimeOut(0, "zk", 100); /*for(int i=0;i<20;i++)
{ new Thread(new Runnable() {
public void run() { // TODO Auto-generated method stub
for(int j=0;j<100000;j++) {
try {
Jedis jedis = redisFactory.getJedis();
jedis.close();
//pool.returnResource(jedis);
System.out.println(jedis.isConnected());
} catch (Exception e) {
e.printStackTrace();
}
} }
}).start();
}
for(int i=0;i<100;i++)
{ //Jedis jedis = redisFactory.getJedis();
Jedis jedis = new Jedis(redisIP, Integer.parseInt(redisPort),10000);
System.out.println(jedis);
jedis.close();
System.out.println("jedis.isConnected()-------------"+jedis.isConnected());
}*/
System.out.println(System.currentTimeMillis());
} /**
* 返还到连接池
*
* @param pool
* @param redis
*/
public static void returnResource(JedisPool pool, Jedis redis) {
if (redis != null) {
pool.returnResource(redis);
}
} /**
* 将数据存到集合
*
* @param key
* @return
*/
public static boolean put2Set(int index,String key , String... value){
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
jedis.sadd(key, value);
return true;
} catch (Exception e) {
//失败就返回jedis
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//释放jedis资源
returnResource(pool, jedis);
} } /**
*
* @author Hellon(刘海龙)
* @param jedis
* @param index
* @param key
* @param value
* @return
*/
public static boolean put2Set(Jedis jedis,int index,String key , String... value ){
try {
jedis.select(index);
jedis.sadd(key, value);
return true;
} catch (Exception e) {
//失败就返回jedis
e.printStackTrace();
return false;
} } /**
* @Title: 带jedis和有效期
* @Package com.jovision.redisDao
* @author Joker(张凯)
* @Description: TODO()
* @date 2015-11-18 下午02:42:04
* @param jedis
* @param seconds
* @param index
* @param key
* @param value
* @return
*/
public static boolean put2Set(Jedis jedis,int seconds,int index,String key , String... value ){
try {
jedis.select(index);
jedis.sadd(key, value);
jedis.expire(key, seconds);
return true;
} catch (Exception e) {
//失败就返回jedis
e.printStackTrace();
return false;
} } /**
* 获取集合数据
*
* @param key
* @return
* @throws RedisException
*/
public static Set<String> getSet(int index,String key) throws RedisException{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
return jedis.smembers(key);
} catch (Exception e) {
//失败就返回jedis
pool.returnBrokenResource(jedis);
//e.printStackTrace();
logger.error("getSet异常", e);
throw new RedisException("redis异常!"+e.getMessage());
} finally {
//释放jedis资源
returnResource(pool, jedis);
}
} public static Set<String> getSet(Jedis jedis,int index,String key) {
try {
jedis.select(index);
return jedis.smembers(key);
} catch (Exception e) {
logger.error("getSet异常", e);
}
return null;
} /**
* @Title: hget
* @Package com.jovision.redisDao
* @author Joker(张凯)
* @Description: TODO()
* @date 2015-9-30 上午11:44:58
* @param index
* @param key
* @param field
* @return
* @throws RedisException
*/
public static String hget(int index,String key,String field) throws RedisException{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
return jedis.hget(key, field);
} catch (Exception e) {
//失败就返回jedis
pool.returnBrokenResource(jedis);
//e.printStackTrace();
logger.error("getSet异常", e);
throw new RedisException("redis异常!"+e.getMessage());
} finally {
//释放jedis资源
returnResource(pool, jedis);
}
} public static String hget(Jedis jedis,int index,String key,String field){
try {
jedis.select(index);
return jedis.hget(key, field);
} catch (Exception e) {
logger.error(e, e);
}
return null;
} /**
* @Title: hset
* @Package com.jovision.redisDao
* @author Joker(张凯)
* @Description: TODO()
* @date 2015-9-30 上午11:45:06
* @param index
* @param key
* @param field
* @param value
* @throws RedisException
*/
public static void hset(int index,String key,String field,String value) throws RedisException{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
jedis.hset(key, field,value);
} catch (Exception e) {
//失败就返回jedis
pool.returnBrokenResource(jedis);
//e.printStackTrace();
logger.error("getSet异常", e);
throw new RedisException("redis异常!"+e.getMessage());
} finally {
//释放jedis资源
returnResource(pool, jedis);
}
} public static void hset(Jedis jedis,int index,String key,String field,String value) {
try {
jedis.select(index);
jedis.hset(key, field,value);
} catch (Exception e) {
logger.error(e,e);
}
} /**
* @Title: 带jedis和seconds
* @Package com.jovision.redisDao
* @author Joker(张凯)
* @Description: TODO()
* @date 2015-11-18 下午02:45:09
* @param jedis
* @param seconds
* @param index
* @param key
* @param field
* @param value
*/
public static void hset(Jedis jedis,int seconds,int index,String key,String field,String value) {
try {
jedis.select(index);
jedis.hset(key, field,value);
jedis.expire(key, seconds);
} catch (Exception e) {
logger.error(e,e);
}
} /**
* 获取单个数据
*
* @param key
* @return
*/
public static String getSingleData(int index,String key){
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
return jedis.get(key);
} catch (Exception e) {
//失败就返回jedis
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//释放jedis资源
returnResource(pool, jedis);
}
return null; } public static String getSingleData(Jedis jedis,int index,String key){
try {
jedis = pool.getResource();
jedis.select(index);
return jedis.get(key);
} catch (Exception e) {
//失败就返回jedis
logger.error(e,e);
}
return null;
} /**
* 存入单个简单数据
*
* @param key
* @return
*/
public static boolean setSingleData(int index,String key ,String value){
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
jedis.set(key, value);
return true;
} catch (Exception e) {
//失败就返回jedis
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
//释放jedis资源
returnResource(pool, jedis);
} } public static void setSingleData(Jedis jedis,int seconds,int index,String key ,String value){
try {
jedis.select(index);
jedis.set(key, value);
jedis.expire(key, seconds);
} catch (Exception e) {
logger.error(e,e);
} } /**
* 删除set中单个value
*
* @param key
* @return
*/
public static boolean del1SetValue(int index,String key ,String value){
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
jedis.srem(key, value);
return true;
} catch (Exception e) {
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
returnResource(pool, jedis);
} } public static boolean del1SetValue(Jedis jedis,int index,String key ,String value){
try {
jedis.select(index);
jedis.srem(key, value);
return true;
} catch (Exception e) {
logger.error(e,e);
return false;
}
} /**
* 删除key对应整个set
*
* @param key
* @return
*/
public static boolean del(int index ,String key){
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
jedis.del(key);
return true;
} catch (Exception e) {
pool.returnBrokenResource(jedis);
e.printStackTrace();
return false;
} finally {
returnResource(pool, jedis);
} } public static boolean del(Jedis jedis,int index ,String key){
try {
jedis.select(index);
jedis.del(key);
return true;
} catch (Exception e) {
logger.error(e,e);
return false;
}
} /**
* 设置key失效时间
* @param key
* @param seconds
* @throws Exception
*/
public static void setTimeOut(int index,String key,int seconds) throws Exception{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
jedis.expire(key, seconds);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
throw e;
} finally {
returnResource(pool, jedis);
}
} /**
* @Title: redisFactory.java
* @Package com.jovision.redisDao
* @author Joker(张凯)
* @Description: TODO()
* @date 2015-10-29 下午03:54:21
* @param jedis
* @param index
* @param key
* @param seconds
* @throws Exception
*/
public static void setTimeOut(Jedis jedis,int index,String key,int seconds){
try {
jedis.select(index);
jedis.expire(key, seconds);
} catch (Exception e) {
logger.error(e, e);
}
} public static byte[] getBytes(int index,byte[] key) throws Exception
{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
return jedis.get(key);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
throw e;
} finally {
returnResource(pool, jedis);
}
} public static byte[] getBytes(Jedis jedis,int index,byte[] key)
{
try {
jedis.select(index);
return jedis.get(key);
} catch (Exception e) {
// pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
//throw e;
}
return null;
} public static Map hgetAll(Jedis jedis,int index,String key)
{
try {
jedis.select(index);
return jedis.hgetAll(key);
} catch (Exception e) {
// pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
}
return null;
} public static void hmset(Jedis jedis,int index,String key, Map<String,String> map)
{
try {
jedis.select(index);
jedis.hmset(key, map);
} catch (Exception e) {
//pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
}
} public static void hmset(Jedis jedis,int seconds,int index,String key, Map<String,String> map)
{
try {
jedis.select(index);
jedis.hmset(key, map);
jedis.expire(key, seconds);
} catch (Exception e) {
// pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
}
} public static void setBytes(int index,byte[] key,byte[] value) throws Exception
{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
jedis.set(key, value);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
throw e;
} finally {
returnResource(pool, jedis);
}
} public static void setBytes(Jedis jedis,int index,byte[] key,byte[] value)
{
try {
jedis.select(index);
jedis.set(key, value);
} catch (Exception e) {
//pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
}
} /**
*
* @author Hellon(刘海龙)
* @param key key值
* @return 该key值存储的长度
* @throws Exception
*/
public static Long getLLength(int index,String key) throws Exception{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
Long len = jedis.llen(key);
return len;
} catch (Exception e) {
pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
throw e;
} finally {
returnResource(pool, jedis);
}
} /**
* 从list获取数据
* @author Hellon(刘海龙)
* @param key 字节byte
* @param start 查询的开始位置
* @param end 查询的结束位置 -1 代表查询所有
* @return 返回字节list列表
* @throws Exception
*/
public static List<byte[]> lrange(int index,byte[] key,int start,int end) throws Exception{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
List<byte[]> list = jedis.lrange(key, start, end);
return list;
} catch (Exception e) {
pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
throw e;
} finally {
returnResource(pool, jedis);
}
} /**
* @Title: 是否存在
* @Package com.jovision.redisDao
* @author Joker(张凯)
* @Description: TODO()
* @date 2015-9-30 下午02:09:25
* @param index
* @param key
* @return
* @throws Exception
*/
public static boolean isExist(int index,String key) throws Exception{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
return jedis.exists(key);
} catch (Exception e) {
pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
throw e;
} finally {
returnResource(pool, jedis);
}
} public static Boolean isExist(Jedis jedis,int index,String key){
try {
jedis.select(index);
return jedis.exists(key);
} catch (Exception e) {
logger.error(e,e);
}
return null;
} /**
* 向list添加数据
* @author Hellon(刘海龙)
* @param key
* @param strings
* @return
* @throws Exception
*/
public static Long lpush(int index,byte[] key,byte[]... strings) throws Exception{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
Long len = jedis.lpush(key, strings);
return len;
} catch (Exception e) {
pool.returnBrokenResource(jedis);
logger.error("redis数据库出现异常", e);
throw e;
} finally {
returnResource(pool, jedis);
}
} /**
* 保留指定key 的值范围内的数据
* @author Hellon(刘海龙)
* @param key 指定的key值
* @param start 开始位置
* @param end 结束位置
* @throws Exception
*/
public static void ltrim(int index,byte[] key,int start,int end) throws Exception{
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.select(index);
jedis.ltrim(key, start, end);
} catch (Exception e) {
logger.error("redis数据库出现异常", e);
throw e;
} finally {
returnResource(pool, jedis);
}
} public static Jedis getJedis()
{
logger.info("publicService redis数据库连接活跃数--<"+pool.getNumActive()+
">--空闲连接数--<"+pool.getNumIdle()+
">--等待连接数--<"+pool.getNumWaiters()+">");
return pool.getResource();
} public static void releaseJedis(Jedis jedis)
{
if (jedis != null) {
jedis.close();
}
}
}
最新文章
- UOJ#213——【UNR #1】争夺圣杯
- [Node.js] 对称加密、公钥加密和RSA
- http协议.md
- 作业4-两人编程<;词频统计>;
- JAVA枚举
- vs2013专业版密钥
- Ogre内部渲染流程分析系列
- SOA——面向服务的体系架构
- QUdpSocket Class
- paip.提升用户体验---论文本编辑器的色彩方案
- redis集群添加删除节点
- lincode.41 最大子数组
- 中国IT职业培训市场经历的几波浪潮,未来的浪潮又是那一波?
- Android Studio项目用Git上传至码云(OSChina)
- map、filter、reduce函数
- 重写Distinct
- PHP 使用协同程序实现合作多任务
- Cloudera Manager Admin控制台启动不起来
- js 文件系统API操作示例
- 理解numpy exp函数
热门文章
- DDos攻击解决办法
- Spring MVC 数据转换和格式化
- 无法嵌入互操作类型";NationalInstruments.TestStand.Interop.UI.ExecutionViewOptions";。请改用适用的接口
- 原生JavaScript常用本地浏览器存储方法五(LocalStorage+userData的一个浏览器兼容类)
- linux 软连接【转】
- EscaperWrapper 转义和反转义
- 配置jdbc问题 mysql与IDEA
- 汉字转拼音js工具:
- Windows 下配置 ApacheBench (AB) 压力测试
- ABP中的AutoMapper