赞
踩
目录
- package cn.enn.util;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import redis.clients.jedis.*;
-
- import java.io.Serializable;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
-
- public class JedisTool implements Serializable {
- protected static final Logger logger = LoggerFactory.getLogger(JedisTool.class);
- private static String strkey="someMapKey";
- private static Map<String,JedisCluster> mapSome = new HashMap<String, JedisCluster>();
- private static Map<String,Jedis> mapone = new HashMap<String, Jedis>();
- //获取集群实例
- public static JedisCluster getSome(String hostsAndPort){
- String[] arr=hostsAndPort.split(",");
- if(mapSome.containsKey(strkey)
- && mapSome.get(strkey)!=null ){
- logger.info("redis is PONG");
- }else{
- logger.warn("redis is not PONG");
- JedisPoolConfig config = new JedisPoolConfig();
- config =new JedisPoolConfig();
- config.setMaxTotal(100);//设置最大连接数
- config.setMaxIdle(100); //设置最大空闲数
- config.setMaxWaitMillis(30000);//设置超时时间
- config.setTestOnBorrow(true);
- config.setTestWhileIdle(true);
- config.setTestOnBorrow(true);
- JedisCluster jedisCluster = new JedisCluster(getHostAndPort(arr),config);
- mapSome.put(strkey,jedisCluster);
- }
- return mapSome.get(strkey);
- }
- //获取单机实例
- public static Jedis getOne(String host){
- if(mapone.containsKey(strkey)
- && mapone.get(strkey)!=null && "pong".equalsIgnoreCase(mapone.get(strkey).ping())){
- logger.info("redis is PONG");
- }else{
- logger.warn("redis is not PONG");
- if(mapone.get(strkey)!=null){
- mapone.get(strkey).close();
- }
- JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
- jedisPoolConfig.setMaxIdle(10);
- jedisPoolConfig.setMaxWaitMillis(1000);
- jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(1800000);
- jedisPoolConfig.setTestWhileIdle(true);
- jedisPoolConfig.setTestOnBorrow(true);
- JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, 6379, 100000, null,1);
- Jedis jedis = jedisPool.getResource();
- mapone.put(strkey,jedis);
- if("pong".equalsIgnoreCase(mapone.get(strkey).ping())){
- logger.warn("redis is change to PONG");
- }
-
- }
- return mapone.get(strkey);
- }
- public static Set<HostAndPort> getHostAndPort(String[] hostAndPort){
- Set<HostAndPort> hap = new HashSet<HostAndPort>();
- String[] hs = null;
- for(String host:hostAndPort){
- hs=host.split(":");
- hap.add(new HostAndPort(hs[0], Integer.parseInt(hs[1])));
- }
- return hap;
- }
-
- }
20/04/02 08:54:11 ERROR executor.Executor: Exception in task 1.2 in stage 20049.0 (TID 60154)
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)
...
Caused by: java.net.SocketException: Broken pipe (Write failed)
产生错误的原因应该是jedis从连接池获取连接时,拿到的连接是一个无效的(可能是一个已经过期的连接),这时候拿到连接并没有对连接做有效性验证,而是直接使用它,导致了上述问题。
开启了redis连接池的两个配置参数
testWhileIdle true
testOnBorrow true
分别表示 在空闲时检查有效性、在获取连接的时候检查有效性 检查到无效连接时,会清理掉无效的连接,并重新获取新的连接。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。