一、链接池配置

  <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. php解析url并得到url中的参数

    <?php $url = 'http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&ar ...

  2. VBA 操作 VBE

    Introduction You can write code in VBA that reads or modifies other VBA projects, modules, or proced ...

  3. tensorflow ValueError: Cannot feed value of shape (5000,) for Tensor 'output:0', which has shape '(?, 10)'

    提供的训练数据和定义的模型之间的维度不对应. 在MNIST手写数字识别时,在 mnist = input_data.read_data_sets("MNIST_data/") 中, ...

  4. CSS中不透明度继承问题的处理

    关于CSS中不透明度的设置,除了兼容方面的问题,还有不透明度继承问题,这里只讨论下后者. 那么, 什么时候会发生不透明度继承问题? 当文档结构中有父子嵌套关系的时候,并且父元素有不透明度属性设置时,会 ...

  5. VC中使用ADO操作数据库的方法

    源地址:http://blog.csdn.net/xiaobai1593/article/details/7459862 准备工作: (1).引入ADO类 #import "c:\progr ...

  6. 趣味编程:FizzBuzz(Kotlin版)

    fun toFizzBuzzIf(n: Int) = if (n % 3 == 0 && n % 5 == 0) "FizzBuzz" else if (n % 3 ...

  7. 疯狂JAVA——第二章 理解面向对象

    面向对象的三大特征:继承.封装和多态 面向对象的方式实际上由OOA(面向对象分析).OOD(面向对象设计)和OOP(面相对象编程)三个部分组成,其中OOA和OOD的结构需要用一个描述方式来描述并记录, ...

  8. 查询中mybatis的if判断里传入0

    1.传入的是long 或者 Integer类型 ,<if test="id != null "> 但是id传值为0时(前提是id对应的类型为long 或者 Intege ...

  9. 安装和使用iOS的包管理工具CocoaPods

    CocoaPods是ruby实现的,需要用ruby进行安装,mac自带ruby,如果没有ruby的需要先安装ruby.   安装CocoaPods命令 安装CocoaPods命令:sudo gem i ...

  10. U盘做启动盘后,恢复原始容量

    借助u盘进行系统安装时,可能会对u盘进行分区.u盘分区后,再连接至电脑,就有很大程度的可能是一部分区域不能显示.u盘原本的大小被占据,显示的大小是比之前少了的,并且这些少掉了的内存也无法再使用.只有对 ...