解决jedis异常JedisConnectionException:Could not get a resource from the pool

服务器上启动了redis之后,用jedis连接发现报错

 

异常的意思是获取不到jedis的连接池,网上查了下,可能是因为连接不上redis服务器导致的。

在stackoverflow上看到很多人说需要修改下redis配置文件, 配置文件里有一行bind 127.0.0.1,意思是只允许127.0.0.1(回环地址)访问redis服务,也就是不允许外界访问,为了允许外网访问,将127.0.0.1改成0.0.0.0,然后重启redis服务(具体可参考博客https://www.cnblogs.com/liusxg/p/5712493.html)。修改之后理论上外网可以访问了,但是一运行还是报同样的异常,说明还是不行。

然后猜测可能是到服务器的网络不通导致,于是在命令行里使用telnet命令去远程连接服务器的6379端口,发现不通,看来报错的大致原因找到了!! 

 

 服务器经过ping测试可以ping通,说明应该是6379端口被封了,那应该是防火墙的原因。(之前我只在阿里云后台控制台防火墙列表那里开放了6379端口,服务器操作系统上应该没有开放,因为阿里云的服务器貌似是有2层防火墙,一层需要在服务器上开,一层去阿里云控制台开放)。

然后用 firewall-cmd --zone=public --add-port=6379/tcp --permanent 开启端口,重启防火墙(firewall-cmd --reload ),

再次使用telnet就能通了。

(我的服务器系统是centOS7,其它系统开防火墙的命令可能不同。)

阅读更多

JedisConnectionException: Could not get a resource from the pool

12-02

前辈们,您们好:rn 硬件:win7 64bit webloginc 10.3 (32bit)/java 1.6(安装weblogic使用的自带Java版本)rn 我使用weblogic部署wap环境后登录(在进入debug之前就会异常),就会出现“不能从连接池获取资源”异常,具体如下:rnat redis.clients.util.Pool.getResource(Pool.java:22)rn at com.ai.log.util.redis.RedisManager.getJedis(RedisManager.java:64)rn at com.ai.log.client.RedisLogAppend.getJedis(RedisLogAppend.java:87)rn at com.ai.log.client.RedisLogAppend.append(RedisLogAppend.java:48)rn at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:221)rn at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:57)rn at org.apache.log4j.Category.callAppenders(Category.java:187)rn at org.apache.log4j.Category.forcedLog(Category.java:372)rn at org.apache.log4j.Category.error(Category.java:286)rn……(此处省略更多行)rnCaused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed outrn at redis.clients.jedis.Connection.connect(Connection.java:134)rn at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:69)rn at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1665)rn at redis.clients.jedis.JedisPool$JedisFactory.makeObject(JedisPool.java:72)rn at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:840)rn at redis.clients.util.Pool.getResource(Pool.java:20)rn ... 79 morernCaused by: java.net.SocketTimeoutException: connect timed outrn at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)rn at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)rn at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)rn at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)rn at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)rn at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)rn at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)rn at java.net.Socket.connect(Socket.java:579)rn at redis.clients.jedis.Connection.connect(Connection.java:129)rn ... 84 morern我使用过的方法:1、更改redis.properties配置(非-1、大数字也试过)rnMaxActive=-1rnMaxIdle=-1rnMaxWait=10000rn2、试过安装官网通用版weblogic 10.X;rn3、考虑更换Tomcat部署,但是配置文件太多(15个),不知道如何部署。rn4、修改过weblogic里一个什么=64 的;//适用于64位系统rn5、防火墙已关闭,Jedis不知如何判断是否启动;rn6、中国移动wap的jedis版本不可能低于2.4.2吧;rnrn 发梢挠到后脑勺,异常坚强如磐石。愁~ 瞅 ~ 愁~rn

使用redis会报Could not get a resource from the pool

03-24

使用spring data redis的时候 会报Could not get a resource from the pool 百度试了下发现不能解决问题rnrn配置如下[code=text]rn rn rn rn rn rn rn rn rn rn [/code]rnrn[code=java]ApplicationContext applicationContext = applicationContext = new ClassPathXmlApplicationContext("applicationContext-redis.xml");rn StringRedisTemplate stringRedisTemplate = applicationContext.getBean(StringRedisTemplate.class);rn RedisTemplate redisTemplate = (RedisTemplate)applicationContext.getBean("redisTemplate");rn ValueOperations redis = redisTemplate.opsForValue();rn System.out.println(redis.get("test"));rnrn for (int i=0; i<3000; i++) rn ThreadT t = new ThreadT();rn t.setRedisTemplate(redisTemplate);rn t.start();rn [/code]rnrnrn[code=java]class ThreadT extends Thread rnrn RedisTemplate redisTemplate;rn rn public void run() rn while(true) rn ValueOperations redis = redisTemplate.opsForValue();rn System.out.println(redis.get("test"));rn rn rnrn public void setRedisTemplate(RedisTemplate redisTemplate) rn this.redisTemplate = redisTemplate;rn rn[/code]rnrn求大神解释一下rnrn[code=text]Caused by: java.net.ConnectException: Connection refused: connectrn at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)rn at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)rn at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)rn at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)rn at java.net.AbstractPlainSocketImpl.connect(Unknown Source)rn at java.net.PlainSocketImpl.connect(Unknown Source)rn at java.net.SocksSocketImpl.connect(Unknown Source)rn at java.net.Socket.connect(Unknown Source)rn at redis.clients.jedis.Connection.connect(Connection.java:136)rn ... 18 morernException in thread "Thread-1781" org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poolrn2017-03-24 18:08:49,400 [Thread-2244] DEBUG [RedisConnectionUtils:204] - Closing Redis Connectionrn2017-03-24 18:08:49,400 [Thread-1454] DEBUG [RedisConnectionUtils:125] - Opening RedisConnectionrn at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:162)rn at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:251)rn at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:58)rn at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)rn at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)rn at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)rn at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:178)rn at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153)rn at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:86)rn2017-03-24 18:08:49,400 [Thread-2516] DEBUG [RedisConnectionUtils:204] - Closing Redis Connectionrn at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)rn at cn.slimsmart.redis.ThreadT.run(TestMain.java:34)rnCaused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poolrn at redis.clients.util.Pool.getResource(Pool.java:42)rn at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:155)rn ... 10 morern2017-03-24 18:08:49,400 [Thread-2272] DEBUG [RedisConnectionUtils:204] - Closing Redis ConnectionrnCaused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connectrn at redis.clients.jedis.Connection.connect(Connection.java:141)rn at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:75)rn at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1716)rn at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:65)rn at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:819)rn at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:429)rn at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:360)rn at redis.clients.util.Pool.getResource(Pool.java:40)rn ... 11 more[/code]

没有更多推荐了,返回首页