系统环境:centos-7 64位
问题描述:java客户端jedis访问centos中redis数据库报如下错误
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:40)
at com.firstelite.util.RedisClient.<init>(RedisClient.java:27)
at com.firstelite.util.Test.main(Test.java:7)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
at redis.clients.jedis.Connection.connect(Connection.java:137)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:65)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1706)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:28)
at
解决办法:
centos防火墙默认打开状态,远程访问时将会超时,将防火墙关闭即可解决。
关闭防火墙命令:systemctl stop firewalld.service
ps:启用防火墙命令:systemctl start firewalld.service
测试:
@Test
//获得单一的jedis对象操作数据库
public void test1(){
//1、获得连接对象
Jedis jedis = new Jedis("192.168.1.128", 6379);
//2、存储数据
jedis.set("addr", "北京");
//3、获得数据
String addr= jedis.get("addr");
System.out.println(addr);
}
单元测试结果:北京