redis连接数高居不下,怎么破?。。。。这么破
最近项目一直在使用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();
}
}
}
redis连接数高居不下,怎么破?。。。。这么破的更多相关文章
- 就publish/subscribe功能看redis集群模式下的队列技术(一)
Redis 简介 Redis 是完全开源免费的,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中 ...
- redis连接数
1.应用程序会发起多少个请求连接?1)对于php程序,以短连接为主.redis的连接数等于:所有web server接口并发请求数/redis分片的个数.2)对于java应用程序,一般使用JedisP ...
- Redis学习_01 windows下的环境搭建
一.Redis 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset( ...
- [原]Redis主从复制各种环境下测试
Redis 主从复制各种环境下测试 测试环境: Linux ubuntu 3.11.0-12-generic 2GB Mem 1 core of Intel(R) Core(TM) i5-3470 C ...
- Redis在windows环境下ThinkPHP的安装和使用
1.Redis概述: 2.Redis在windows环境下的安装: 下载地址:https://github.com/dmajkic/redis/downloads,选取其中一个zip压缩包:
- redis连接数问题
redis连接数查看 info client redis连接数满了,不会继续建立连接. 造成redis连接数满了原因有很多. 1.建立新连接不close()的话redis连接不会回归连接池. 显示所有 ...
- Redis在Windows环境下搭建
1. 下载Redis-Windows版本 Redis官网下载页面: http://redis.io/download Windows下Redis项目: https://github.com/MSOp ...
- 查看redis连接数
在redis-cli命令行使用:info clients可以查看当前的redis连接数. 如下图: config get maxclients 可以查询redis允许的最大连接数. 如下图:
- PHP如何安装redis扩展(Windows下)
PHP如何安装redis扩展(Windows下) 一.总结 一句话总结:下载扩展的dll,放入指定文件夹(php对应的扩展的目录php/ext),在配置文件php.ini中注册dll 尽量不要选择最新 ...
随机推荐
- Python - Django - ORM QuerySet 方法补充
models.py: from django.db import models class Employee2(models.Model): name = models.CharField(max_l ...
- linux信号量例子
semaphore.h 提供的是 POSIX 标准定义的 semaphore 接口,而 sys/sem.h 里 提供的是符合 System V 标准的 semaphore接口 (semget, sem ...
- 【tshark tcpdump】linux网络排查
抓包: 1.tcpdump 2.tshark是wireshark的命令行版. tshark使用示例: ,实时打印当前http请求的url # tshark -s -i eth0 -n -f 'tcp ...
- POJ 2342 Anniversary party 树形DP基础题
题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...
- 【Leetcode_easy】961. N-Repeated Element in Size 2N Array
problem 961. N-Repeated Element in Size 2N Array solution: class Solution { public: int repeatedNTim ...
- 我的Java之路
前言: 之前在学习python,刚开始的时候跟多数小白一样学习一些基础的知识,比如数据类型,用法,基本的语言结构,学了一段时间实在是学习不下去了,真是太TMD的无聊了,很多方法都记不住,也不知道学了这 ...
- android基础---->传感器的使用
现在每部Android 手机里面都会内置有许多的传感器,它们能够监测到各种发生在手机上的物理事件,而我们只要灵活运用这些事件就可以编写出很多好玩的应用程序.今天我们开始简单的传感器使用的学习. 目录导 ...
- asp.net编程基础
vs常用两个快捷键:打开即时窗口 ctrl+alt+i : 快速代码格式排版:ctrl+k+d 一:Page:页面 Page.IsPostBack 判断页面是否第一次加载用 if(Page.IsPo ...
- UN Report: Last 10 Years Likely the Hottest Decade on Record——VOA慢速英语
听力地址:UN Report: Last 10 Years Likely the Hottest Decade on Record 中英对照:联合国报告称过去十年可能是有记录以来最热的十年 Words ...
- Git操作入门
生成ssh key: ssh-keygen -t rsa -C "lkt@temp.com" 按三次回车,最后在.ssh文件夹下得到id_rsa和id_rsa.pub两个文 ...