标签:list col cmd lld resource 0.12 exception from native
package com.study.util; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisUtil { private static final String host = "192.168.150.129"; private static final int port = 6379; private static final String password = "1234"; private static JedisPool jedisPool; public static Jedis getJedis() { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(20); config.setMaxIdle(5); config.setTestOnBorrow(false); jedisPool = new JedisPool(config,host, port,3000,password); Jedis jedis = jedisPool.getResource(); String code = jedis.auth(password); //如果没有配置密码验证,这注掉这句话 //System.out.println("auth code :" + code); jedis.select(1);//选择第二个数据库演示 return jedis; } public static void main(String[] args) { Jedis jedis = getJedis(); jedis.close(); } }
redis连接时遇到的问题:Caused by: java.net.ConnectException: Connection refused: connect
Exception in thread "main" 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.study.util.RedisUtil.main(RedisUtil.java:50) 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:889) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:433) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362) at redis.clients.util.Pool.getResource(Pool.java:49) ... 2 more Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at redis.clients.jedis.Connection.connect(Connection.java:184)
解决方法:
查看是否将默认只能本地访问redis改为所有IP均可以访问
bind 127.0.0.1 改为 bind 0.0.0.0
修改redis的配置文件redis.conf 将bind改为0.0.0.0
遇到的问题二:Caused by: java.net.SocketTimeoutException: connect timed out
Exception in thread "main" 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.study.util.RedisUtil.getJedis(RedisUtil.java:29) at com.study.util.RedisUtil.main(RedisUtil.java:39) Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out 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:889) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:433) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362) at redis.clients.util.Pool.getResource(Pool.java:49) ... 3 more Caused by: java.net.SocketTimeoutException: connect timed out at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at redis.clients.jedis.Connection.connect(Connection.java:184) ... 10 more
解决方法:
systemctl stop firewalld.service
2 .开启6379一个端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent
--permanent永久生效,没有此参数重启后失效
firewall-cmd --reload
代码git地址:https://gitee.com/sjcq/redis.git
标签:list col cmd lld resource 0.12 exception from native
原文地址:https://www.cnblogs.com/sjcq/p/8973176.html