redis代码连接及遇到的问题
Posted 四季常青
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis代码连接及遇到的问题相关的知识,希望对你有一定的参考价值。
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
解决方法:
- 1.关闭防火墙
-
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
以上是关于redis代码连接及遇到的问题的主要内容,如果未能解决你的问题,请参考以下文章
虚拟机搭建redis单机版及redis-cluster,使用redis desktop manager和java(eclipse)连接redis过程遇到问题汇总