redis报错JedisException: Could not get a resource from the pool

使用Java操作redis的时候报错,但是我的redis启动了,那么究竟是神马原因导致连接失败呢?

可能得方式有三种,密码,防火墙,网络

1.网络畅通,没毛病,能ping通

2.密码:redis还需要密码来登录,通过redis.conf中的

#requirepass foobared  

修改为requirepass root

重新启动redis 

./redis-server ../redis.conf

3.防火墙:因为使用centos7,那么关闭防火墙命令systemctl disable firewall.service

这轮走下来,应该没问题了吧,因为我使用Java操作redis,还是报错,那么我就查看代码,发现没有带密码,导致连接失败,上代码

import java.util.Iterator;

import redis.clients.jedis.Jedis;

/**
 * redis操作api
 * @author ALL
 *
 */
public class RedisClient {
	/**
	 * 保存数据
	 */
	@SuppressWarnings("deprecation")
	public static void setDataToRedis(String setName,String[] datas){
 		Jedis redisClient = null;
		try {
			redisClient=RedisClientPool.jedisPool.getResource();
			for (String data:datas) {
				redisClient.sadd(setName, data);
			}
			System.out.println("save data success-------------------------"+setName);
		} catch (Exception e) {
			e.printStackTrace();
			 // 销毁对象  
            RedisClientPool.jedisPool.returnBrokenResource(redisClient);  
		}finally{
			// 还原到连接池  
            RedisClientPool.jedisPool.returnResource(redisClient);  
		}
	}
	/**
	 * 获取set集合中的数据
	 * @param setName
	 * @param values
	 */
	public static boolean getDataFromRedis(String setName,String values){
		Jedis redisClient = null;
		try {
			redisClient=RedisClientPool.jedisPool.getResource();
			return redisClient.sismember(setName, values);//判断 who 是否是user集合的元素  
			//System.out.println(redisClient.srandmember(setName));  
			//System.out.println(redisClient.scard(setName));//返回集合的元素个数  
		} catch (Exception e) {
			 // 销毁对象  
            RedisClientPool.jedisPool.returnBrokenResource(redisClient);  
		}finally{
			// 还原到连接池  
            RedisClientPool.jedisPool.returnResource(redisClient);  
		}
		return false;
		
	}
	public static void flushAllData(){
		Jedis redisClient = null;
		try {
			redisClient=RedisClientPool.jedisPool.getResource();
			redisClient.flushAll();
		} catch (Exception e) {
			 // 销毁对象  
            RedisClientPool.jedisPool.returnBrokenResource(redisClient);  
		}finally{
			// 还原到连接池  
            RedisClientPool.jedisPool.returnResource(redisClient);  
		}
	}
}

import java.io.File;
import java.io.FileReader;
import java.util.Properties;



import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * redis连接池
 * @author ALL
 *
 */
public class RedisClientPool {
	
	
	public static JedisPool jedisPool;
	
	private static String redisHost;
	private static String redisPort;
	static{
		Properties properties=new Properties();
		try {
			String path=RedisClientPool.class.getResource("/resource.properties").getPath();
			properties.load(new FileReader(new File(path)));
			redisHost=properties.getProperty("redisHost");
			redisPort=properties.getProperty("redisPort");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static RedisClientPool redisClientPool=getInstance();
	
	public static void main(String[] args) {
		System.out.println(redisHost);
	}
	public static synchronized RedisClientPool getInstance() {
		if(null==redisClientPool){
			redisClientPool=new RedisClientPool();
		}
		return redisClientPool;
	}
	public RedisClientPool(){
		if(null==jedisPool){
			init();
		}
	}
	/**
	 * 初始化jedis连接池
	 */
	public static void init() {
		JedisPoolConfig jedisPoolConfig=initJedisPoolConfig();
		String host=redisHost;
		System.out.println(redisPort);
		int port=Integer.valueOf(redisPort);
		int timeout=1000;
		//构造连接池,如果redis设置密码,那么在此处也把密码加上
		jedisPool=new JedisPool(jedisPoolConfig,host,port,timeout,"");
	}
	/**
	 * 初始化jedis
	 * @return
	 */
	private static JedisPoolConfig initJedisPoolConfig() {
		JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
		//控制一个pool最多有多少个状态为idle的jedis的实力
		jedisPoolConfig.setMaxIdle(1000);
		//最大能够保持空闲状态的对象数目
		jedisPoolConfig.setMaxTotal(300);;
		
		//超时时间
		jedisPoolConfig.setMaxWaitMillis(1000);
		// 在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;  
        jedisPoolConfig.setTestOnBorrow(true);  
        // 在还会给pool时,是否提前进行validate操作  
        jedisPoolConfig.setTestOnReturn(true);  
		return jedisPoolConfig;
	}
	
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值