一、链接池配置

  <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. Object-c中的单例

    #import <UIKit/UIKit.h> @interface UniAudioPlayer:NSObject{ } +(UniAudioPlayer*) getInstance; ...

  2. 有3D效果的进度条

    // The Unofficial Newsletter of Delphi Users - Issue #12 - February 23rd, 1996 unit Percnt3d; (* TPe ...

  3. DataFrame 取值

    通过 DataFrame[ ]方式,取得得都是行, [ ] 中,添加过滤条件 data = pd.DataFrame( np.arange(16).reshape(4,4), index=['OP', ...

  4. 吴裕雄 数据挖掘与分析案例实战(4)——python数据处理工具:Pandas

    # 导入模块import pandas as pdimport numpy as np # 构造序列gdp1 = pd.Series([2.8,3.01,8.99,8.59,5.18])print(g ...

  5. scala 建模

    // train multinomial logistic regression val lr = new LogisticRegressionWithLBFGS() .setIntercept(tr ...

  6. apply和call用法

    资料来源:http://blog.csdn.net/business122/article/details/8000676 Js apply方法详解 我在一开始看到javascript的函数apply ...

  7. 大型运输行业实战_day03_2_使用ajax将请求页面与请求数据分离

    1.引入jquery 1.添加jquery包 2.在要使用jquery的页面中引入jquery 引入jquery后必须检查是否引入正确,这里值得注意的是 springMVC默认情况先会拦截 js文件, ...

  8. jquery读取本地文件,Windows上报错。XMLHttpRequest cannot load xxx. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.k.cors.a.c

    问题: 测试报告,使用本地的json.txt文件,结果文件读取失败,报错如下: XMLHttpRequest cannot load xxx. Cross origin requests are on ...

  9. ReactNative手势解锁(react-native-ok-gesture-password)

    在大前端的趋势之下,我也慢慢开始从事React Native相关的开发.但是奈何React Native生态相对于Android来说还是太小了.许多开源的库早早就已经不再维护.之前项目中需要用到手势解 ...

  10. hdoj1087 (DP--LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 思路:这题很简单了,纯LIS的解法,没有一点变形,由于数据小,使用O(n^2)LIS解法就足够了 ...