最近项目一直在使用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连接数高居不下,怎么破?。。。。这么破的更多相关文章

  1. 就publish/subscribe功能看redis集群模式下的队列技术(一)

    Redis 简介 Redis 是完全开源免费的,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中 ...

  2. redis连接数

    1.应用程序会发起多少个请求连接?1)对于php程序,以短连接为主.redis的连接数等于:所有web server接口并发请求数/redis分片的个数.2)对于java应用程序,一般使用JedisP ...

  3. Redis学习_01 windows下的环境搭建

    一.Redis 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset( ...

  4. [原]Redis主从复制各种环境下测试

    Redis 主从复制各种环境下测试 测试环境: Linux ubuntu 3.11.0-12-generic 2GB Mem 1 core of Intel(R) Core(TM) i5-3470 C ...

  5. Redis在windows环境下ThinkPHP的安装和使用

    1.Redis概述: 2.Redis在windows环境下的安装: 下载地址:https://github.com/dmajkic/redis/downloads,选取其中一个zip压缩包:

  6. redis连接数问题

    redis连接数查看 info client redis连接数满了,不会继续建立连接. 造成redis连接数满了原因有很多. 1.建立新连接不close()的话redis连接不会回归连接池. 显示所有 ...

  7. Redis在Windows环境下搭建

    1.  下载Redis-Windows版本 Redis官网下载页面: http://redis.io/download Windows下Redis项目: https://github.com/MSOp ...

  8. 查看redis连接数

    在redis-cli命令行使用:info clients可以查看当前的redis连接数. 如下图: config get maxclients 可以查询redis允许的最大连接数. 如下图:

  9. PHP如何安装redis扩展(Windows下)

    PHP如何安装redis扩展(Windows下) 一.总结 一句话总结:下载扩展的dll,放入指定文件夹(php对应的扩展的目录php/ext),在配置文件php.ini中注册dll 尽量不要选择最新 ...

随机推荐

  1. Spring AOP无法拦截Controller

    参考@参考文章中的评论 首先,应该打开aop代理 <aop:aspectj-autoproxy proxy-target-class="true"/> 其次,应该讲ao ...

  2. C# WinForm获取 当前执行程序路径的几种方法(转)

    1.获取和设置当前目录的完全限定路径. string str = System.Environment.CurrentDirectory; Result: C:xxxxxx 2.获取启动了应用程序的可 ...

  3. [转载] HashMap的工作原理-hashcode和equals的区别

    目录 前言 为什么需要使用Hashcode,可以从Java集合的常用需求来描述: 更深入的介绍 先来些简单的问题 HashMap的0.75负载因子 总结 我在网上看到的这篇文章,介绍的很不错,但是我看 ...

  4. Swift4.0复习基本语法简介

    1.五种类型: 包含五种类型——枚举(enum).结构体(struct).类(class).协议( protocol)以及函数类型(function types). 2.变量对象:var 3.常量对象 ...

  5. C罗是你人生中最好的健身教练和精神导师

    C罗又进球了,两场小组赛包揽全队4粒进球,一己之力帮助葡萄牙取得1胜1平,掌握出线主动权.此前三届世界杯金靴分别只有6球.5球.5球进账,C罗如果能延续火爆状态,金靴唾手可得. 之前三届世界杯,C罗7 ...

  6. JIRA+JIRA Agile敏捷项目管理工具

    jira插件下载地址 http://www.confluence.cn/pages/viewpage.action?pageId=1671327 下载GreenHopper插件 安装Jira-agil ...

  7. spring结合shiro的学习总结

    pom文件加入 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-c ...

  8. art-template 弹出编辑

    <!-- 模板 --> <script id="render-tpl" type="text/html"> <div class= ...

  9. nlp算法

    人工智能算法大体上来说可以分类两类:基于统计的机器学习算法(Machine Learning)和深度学习算法(Deep Learning) 总的来说,在sklearn中机器学习算法大概的分类如下: 1 ...

  10. AVL排序二叉树树

    AVL树第一部分,(插入) AVL树是一种自平衡二叉搜索树(BST),其中对于所有节点,左右子树的高度差不能超过1. 一个AVL树的示例 上面的树是AVL树,因为每个节点的左子树和右子树的高度之间的差 ...