描述
这个错误产生的前提是这样的,将数据存入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地址或端口是否错误
参考博文:
https://blog.csdn.net/weixin_43376907/article/details/88782723
https://blog.csdn.net/testcs_dn/article/details/43052585