jedispool 连 redis
java端在使用jedispool 连接redis的时候,在高并发的时候经常卡死,或报连接异常,JedisConnectionException,或者getResource 异常等各种问题
在使用jedispool 的时候一定要注意两点
1、 在获取 jedisPool和jedis的时候加上线程同步,保证不要创建过多的jedispool 和 jedis
2、 用完Jedis实例后需要返还给JedisPool
整理了一下redis工具类,通过大量测试和高并发测试的。
package com.util;import org.apache.log4j.Logger;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;/** * Redis 工具类 * @author think */public class RedisUtil { protected static Logger logger = Logger.getLogger(RedisUtil.class); //Redis服务器IP private static String ADDR_ARRAY = FileUtil.getPropertyValue("/properties/redis.properties", "server"); //Redis的端口号 private static int PORT = FileUtil.getPropertyValueInt("/properties/redis.properties", "port"); //访问密码// private static String AUTH = FileUtil.getPropertyValue("/properties/redis.properties", "auth"); //可用连接实例的最大数目,默认值为8; //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 private static int MAX_ACTIVE = FileUtil.getPropertyValueInt("/properties/redis.properties", "max_active");; //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 private static int MAX_IDLE = FileUtil.getPropertyValueInt("/properties/redis.properties", "max_idle");; //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException; private static int MAX_WAIT = FileUtil.getPropertyValueInt("/properties/redis.properties", "max_wait");; //超时时间 private static int TIMEOUT = FileUtil.getPropertyValueInt("/properties/redis.properties", "timeout");; //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; private static boolean TEST_ON_BORROW = FileUtil.getPropertyValueBoolean("/properties/redis.properties", "test_on_borrow");; private static JedisPool jedisPool = null; /** * redis过期时间,以秒为单位 */ public final static int EXRP_HOUR = 60*60; //一小时 public final static int EXRP_DAY = 60*60*24; //一天 public final static int EXRP_MONTH = 60*60*24*30; //一个月 /** * 初始化Redis连接池 */ private static void initialPool(){ try { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(MAX_ACTIVE); config.setMaxIdle(MAX_IDLE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); jedisPool = new JedisPool(config, ADDR_ARRAY.split(",")[0], PORT, TIMEOUT); } catch (Exception e) { logger.error("First create JedisPool error : "+e); try{ //如果第一个IP异常,则访问第二个IP JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(MAX_ACTIVE); config.setMaxIdle(MAX_IDLE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); jedisPool = new JedisPool(config, ADDR_ARRAY.split(",")[1], PORT, TIMEOUT); }catch(Exception e2){ logger.error("Second create JedisPool error : "+e2); } } } /** * 在多线程环境同步初始化 */ private static synchronized void poolInit() { if (jedisPool == null) { initialPool(); } } /** * 同步获取Jedis实例 * @return Jedis */ public synchronized static Jedis getJedis() { if (jedisPool == null) { poolInit(); } Jedis jedis = null; try { if (jedisPool != null) { jedis = jedisPool.getResource(); } } catch (Exception e) { logger.error("Get jedis error : "+e); }finally{ returnResource(jedis); } return jedis; } /** * 释放jedis资源 * @param jedis */ public static void returnResource(final Jedis jedis) { if (jedis != null && jedisPool !=null) { jedisPool.returnResource(jedis); } } /** * 设置 String * @param key * @param value */ public static void setString(String key ,String value){ try { value = StringUtil.isEmpty(value) ? "" : value; getJedis().set(key,value); } catch (Exception e) { logger.error("Set key error : "+e); } } /** * 设置 过期时间 * @param key * @param seconds 以秒为单位 * @param value */ public static void setString(String key ,int seconds,String value){ try { value = StringUtil.isEmpty(value) ? "" : value; getJedis().setex(key, seconds, value); } catch (Exception e) { logger.error("Set keyex error : "+e); } } /** * 获取String值 * @param key * @return value */ public static String getString(String key){ if(getJedis() == null || !getJedis().exists(key)){ return null; } return getJedis().get(key); } }jedispool 连 redis的更多相关文章
- Jedis/JedisPool和Redis数据类型与特性
1.介绍Jedis Jedis 是 Redis 的 java 版本客户端,使用Jedis可以连接 Redis的数据库,Jedis连接方式有三种Jedis/JedisPool 连接.ShardedJed ...
- spring boot redis缓存JedisPool使用
spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...
- redis 工具类 单个redis、JedisPool 及多个redis、shardedJedisPool与spring的集成配置
http://www.cnblogs.com/edisonfeng/p/3571870.html http://javacrazyer.iteye.com/blog/1840161 http://ww ...
- 三:Redis连接池、JedisPool详解、Redisi分布式
单机模式: package com.ljq.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; ...
- redis设置密码以及jedisPool设置密码
转: redis设置密码以及jedisPool设置密码 2019年01月02日 20:24:43 宇文荒雪 阅读数:1118 版权声明:本文为博主原创文章,未经博主允许不得转载. https:// ...
- Java与redis交互、Jedis连接池JedisPool
Java与redis交互比较常用的是Jedis. 先导入jar包: commons-pool2-2.3.jar jedis-2.7.0.jar 基本使用: public class RedisTest ...
- redis连接池(JedisPool)资源归还及timeout详解
转载. https://blog.csdn.net/yaomingyang/article/details/79043019 一.连接池资源类详解都在注释上 package redis.v1.clie ...
- redis连接池——JedisPool和JedisCluster的介绍与使用
目录 Jedis使用方式的介绍 Redis连接池介绍 创建连接池配置文件 单机版的Redis连接池 集群版的Redis连接池 总结 Jedis使用方式的介绍 Jedis就是Java实现的操作Redis ...
- (二)Redis之Jedis概念和HelloWorld实现以及JedisPool的使用
一.Jedis概念 实际开发中,我们需要用Redis的连接工具连接Redis然后操作Redis, 对于主流语言,Redis都提供了对应的客户端: 官网:https://redis.io/clients ...
随机推荐
- Redis全方位讲解--主从复制(转载)
前言 前面介绍了redis持久化和容灾备份,这篇会介绍redis主从复制和redis持久化在主从复制中的一些应用.因为本人没有那么多服务器或机器,所以这里主要介绍下如何在docker容器中搭建主从复制 ...
- (一)Mina源代码解析之总体架构
Apache Mina Server 是一个网络通信应用框架.也就是说,它主要是对基于TCP/IP.UDP/IP协议栈的通信框架(当然,也能够提供JAVA 对象的序列化服务.虚拟机管道通信服务等).M ...
- tesseract中文语言文件包 下载
tesseract中文语言文件包 下载 tesseract中文语言文件包 下载 tesseract中文语言文件包 下载 下载地址是:https://github.com/tesseract-ocr/l ...
- SQL Server2005 两台服务器上的数据库互相同步(转载)
1.1测试环境 Item 发布机 A 订阅机 B OS Windows 2003 Server Windows 2003 Server SQL SQL Server 2005 企业版 SQL Serv ...
- union共用体的对齐
union DATE { char a; ]; double b; }; DATE max; cout<< sizeof(max) << endl; 这个问题很好回答,并且我把 ...
- ngBind {{}} ngBindTemplate
1.首先我们最常使用的一个绑定表达式的指令是ngBind,比如在一个div标签中我们可以这样使用: <div ng-bind="vm.info"></div> ...
- 新建一个用户,让他只能看到某一个视图(View),如何设置
新建一个用户,让他只能看到某一个视图(View),怎么设置? 新建一个用户,让他只能看到某一个视图(View),怎么设置? 如果做不到“只能看到指定视图”,最好能做到“对指定表或视图只有查询的权限”. ...
- Linux动态库开发
http://blog.csdn.net/qq_33850438/article/details/52014399 ### 导出符号------------------------------ 默认所 ...
- php html 转义
html_entity_decode($string);htmlentities($string);htmlspecialchars($string);htmlspecialchars_decode( ...
- golang Time to String
golang Time to String allenhaozi · 2016-09-02 09:00:00 · 2447 次点击 · 预计阅读时间 1 分钟 · 19分钟之前 开始浏览 这是一个创建 ...