redis中Could not get a resource from the pool異常及解決方案
描述
這個錯誤產生的前提是這樣的,將數據存入redis(新安裝的)中,在通過連接池獲取jedis實例時,產生如下錯誤( Could not get a resource from the pool)。
分析
由於是新安裝的redis,配置文件沒有更改,低級的錯誤浪費不少時間,寫一篇文章,讓同樣犯這種錯誤的朋友別再浪費時間。
解決
更改配置文件 redis.conf 中兩處
1.將 bind 127.0.0.1 註釋掉
2.將 protected-mode 改為 no
bind 127.0.0.1 | protected-mode |
---|---|
默認情況bind=127.0.0.1隻能接受本機的訪問請求不寫的情況下,無限制接受任何ip地址的訪問 | 如果開啟瞭protected-mode,那麼在沒有設定bind ip且沒有設密碼的情況下,Redis隻允許接受本機的相應 |
運行代碼
//創建jedis配置文件對象 val jedisPoolConfig = new JedisPoolConfig() //添加配置信息 jedisPoolConfig.setMaxTotal(100) jedisPoolConfig.setMaxIdle(20) jedisPoolConfig.setMinIdle(0) jedisPoolConfig.setBlockWhenExhausted(true) jedisPoolConfig.setMaxWaitMillis(500) jedisPoolConfig.setTestOnBorrow(true) //創建連接池對象 var jedisPool:JedisPool = new JedisPool(jedisPoolConfig,"linux1",6379) //獲取實例 jedisPool.getResource
報錯內容
ERROR — [ Executor task launch worker for task 15] org.apache.spark.executor.Executor (line: 91) : Exception in task 7.0 in stage 2.0 (TID 15)
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:53)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
at com.atguigu.bigdata.sparkmall.common.RedisUtil$.getJedisClient(RedisUtil.scala:26)
at com.atguigu.bigdata.sparkmall.realtime.Req7TimeAdvertClickTrendApplication$$anonfun$main$1$$anonfun$apply$2.apply(Req7TimeAdvertClickTrendApplication.scala:54)
at com.atguigu.bigdata.sparkmall.realtime.Req7TimeAdvertClickTrendApplication$$anonfun$main$1$$anonfun$apply$2.apply(Req7TimeAdvertClickTrendApplication.scala:53)
at org.apache.spark.rdd.RDD$$anonfun$foreachPartition$1$$anonfun$apply$29.apply(RDD.scala:926)
at org.apache.spark.rdd.RDD$$anonfun$foreachPartition$1$$anonfun$apply$29.apply(RDD.scala:926)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1951)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1951)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
at org.apache.spark.scheduler.Task.run(Task.scala:99)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:322)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
at redis.clients.jedis.Connection.connect(Connection.java:207)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
at redis.clients.util.Pool.getResource(Pool.java:49)
… 14 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.Connection.connect(Connection.java:184)
… 21 more
其他原因
1.最大鏈接數量超標,資源未回收jedis.close();//釋放資源
2.壓根redis服務沒有起 (很多人是這種)
3.可能是linux防火墻問題造成的 檢查網絡是否暢通
4.檢查IP地址或端口是否錯誤
總結
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- j2Cache線上異常排查問題解決記錄分析
- Java連接Redis全過程講解
- SpringBoot集成Redis並實現主從架構的實踐
- 一文搞懂Java中對象池的實現
- SpringBoot集成redis實現分佈式鎖的示例代碼