一、链接池配置

  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- ${redis.max_total:64}会优先查找是否已经配置redis.max_total的属性,如果没有配置,则为64 -->
<property name="maxTotal" value="${redis.max_total:300}" />
<property name="maxIdle" value="${redis.max_idle:8}" />
<property name="maxWaitMillis" value="${redis.max_wait_millis:30000}" />
<property name="testOnBorrow" value="true" />
<!-- whenExhaustedAction为1表示当链接池的实例用完时,阻塞,直到有可用链接资源 -->
<property name="blockWhenExhausted" value="true"></property>
</bean> <bean id="jedisSentinelPoolConfig" class="com.chinacloud.monitoring.api.util.JedisSentinelPoolConfig">
<constructor-arg index="0" value="${redis.master.name}" />
<constructor-arg index="1" value="${redis.sentinels}" />
<constructor-arg index="2" ref="jedisPoolConfig" />
</bean> <bean id="jedisHelper" class="com.chinacloud.monitoring.api.util.JedisHelper" destroy-method="destroy">
<!--对应于JedisHelper类里面的的构造函数 -->
<constructor-arg index="0" ref="jedisSentinelPoolConfig" />
</bean>

二、JedisSentinelPoolConfig类

package com.chinacloud.monitoring.api.util;

import java.util.HashSet;
import java.util.Set; import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool; public class JedisSentinelPoolConfig {
//在集群中的主机maste-slave模式下
private String masterName;
private String sentinels;
private JedisPoolConfig jedisConfig; public JedisSentinelPoolConfig(String masterName, String sentinels, JedisPoolConfig jedisConfig) {
//TODO: validation
this.masterName = masterName;
this.sentinels = sentinels;
this.jedisConfig = jedisConfig; } //JedisSentinelPool支持集群的链接池
public JedisSentinelPool getJedisSentinelPool() {
String[] sentinelstrs = sentinels.split(",");
Set<String> sentinelsSet = new HashSet<String>(sentinelstrs.length);
for (String st : sentinelstrs) {
sentinelsSet.add(st);
}
//30000表示超时为30秒
return new JedisSentinelPool(masterName, sentinelsSet, jedisConfig,30000);
}
}

三、JedisHelper类

    public <T> List<T> listRange(String listName, Class<T> elementClazz, long start, long end) throws IOException {
Assert.notNull(listName);
Assert.notNull(elementClazz);
Jedis jedis = null;
boolean borrowOrOprSuccess = true;
List<T> objects = new ArrayList<>();
jedis=getJedis();
try {
//同步锁解决高并发情况下的address in use异常
synchronized (this) {
if(jedis!=null){
List<String> elements = jedis.lrange(listName, start, end);
for (String element : elements) {
objects.add(objectMapper.readValue(element, elementClazz));
}
}
}
} catch (Exception e) {
borrowOrOprSuccess = false;
if (jedis != null)
jedisPool.returnBrokenResource(jedis); } finally {
if (borrowOrOprSuccess){
jedisPool.returnResource(jedis);
}
}
return objects;
} public Jedis getJedis()
{
int timeoutCount = 0;
while (true) { //如果第一次取不到,尝试取三次链接
try {
Jedis jedis = jedisPool.getResource();
return jedis;
} catch (Exception e) {
timeoutCount++;
if (timeoutCount > 3)
{
break;
}
}
}
return null;
}

解决Jedis链接报超时异常和connection reset异常的方法的更多相关文章

  1. 一次SocketException:Connection reset 异常排查

    问题描述 上一期的需求上线之后,线上多了一个异常:Connection reset.如下: [2017-03-22 00:45:00 ERROR] [creativeAuditTaskSchedule ...

  2. 异常记录 Connection reset

    连接重置Connection reset 异常java.net.SocketException: Connection reset 详细信息 java.net.SocketException: Con ...

  3. HttpClient遭遇Connection Reset异常,如何正确配置?

    最近工作中使用的HttpClient工具遇到的Connection Reset异常.在客户端和服务端配置不对的时候容易出现问题,下面就是记录一下如何解决这个问题的过程. 出现Connection Re ...

  4. apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))

    apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))   今天用apache 自带的ab工具测试,当并发量达到1000多的时 ...

  5. [未解决]报错:ssh_exchange_identification: read: Connection reset by peer

    报错代码: ssh_exchange_identification: read: Connection reset by peer fatal: 无法读取远程仓库. 请确认您有正确的访问权限并且仓库存 ...

  6. [转载] apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))

    遇见相同的问题. https://www.cnblogs.com/felixzh/p/8295471.html -------------------------------------------- ...

  7. celery使用rabbitmq报错[Errno 104] Connection reset by peer.

    写好celery任务文件,使用celery -A app worker --loglevel=info启动时,报告如下错误: [2019-01-29 01:19:26,680: ERROR/MainP ...

  8. 解决ssh链接服务器超时自动断开的问题

    为了安全性:ssh默认的连接超时时间很短:经常就是发个呆就断开了:事实上是可以修改超时时间的. 示例环境: 服务器:centos6.5 1:[root@iZ28qa8jt4uZ /]cp /etc/s ...

  9. 最近纠结致死的一个java报错java.net.SocketException: Connection reset 终于得到解决

    自从SEOTcs系统11月份24日更新了一下SEO得分算法以来,一直困扰我的一个问题出现了,java的数据job任务,在执行过程中会经常报以下的错误: “2011-12-03 18:00:32 Def ...

随机推荐

  1. Activity服务类-7 RepositoryService服务类

    Activity服务类-1 RepositoryService服务类一共47个接口1.创建部署//开始创建一个新的部署.DeploymentBuilder createDeployment(); 2. ...

  2. VBA 编写类

    一.初识类 现在,请打开你的VBE,主菜单-插入-类模块. 插入了一个类模块,也就建立了一个类.类模块的名字就是类的名字.你现在看到的,她的名字叫“类1”,这是VBA按她姐妹排行给她取的的,是的,VB ...

  3. 机器学习入门-贝叶斯构造LDA主题模型,构造word2vec 1.gensim.corpora.Dictionary(构造映射字典) 2.dictionary.doc2vec(做映射) 3.gensim.model.ldamodel.LdaModel(构建主题模型)4lda.print_topics(打印主题).

    1.dictionary = gensim.corpora.Dictionary(clean_content)  对输入的列表做一个数字映射字典, 2. corpus = [dictionary,do ...

  4. as3的全屏功能的实现主要是舞台stage的displayState属性

    StageDisplayState.NORMAL                                               正常 StageDisplayState.FULL_SCR ...

  5. 修改默认的inout输入框背景颜色

    https://www.cnblogs.com/beileixinqing/p/6119690.html

  6. 根据class操作div显示与隐藏

    <div class="otherComment" > <!-- style="display:none" --> 测试 </di ...

  7. C++17尝鲜:类模板中的模板参数自动推导

    模板参数自动推导 在C++17之前,类模板构造器的模板参数是不能像函数模板的模板参数那样被自动推导的,比如我们无法写 std::pair a{1, "a"s}; // C++17 ...

  8. delphi常用函数和方法

     uses ShellApi, ActiveX, ComObj, ShlObj; function HasText(Text: string; const Values: array of strin ...

  9. Linux Shell中有三种引号的用法

    Linux Shell中有三种引号,分别为双引号(" ").单引号(' ')以及反引号(` `). 其中双引号对字符串中出现的$.''.`和\进行替换:单引号不进行替换,将字符串中 ...

  10. with as (转)

    sql with as 用法(适用sqlserver,好像oracle也适用) Server 2005中提供了公用表表达式(CTE),使用CTE,可以使SQL语句的可维护性,同时,CTE要比表变量的效 ...