赞
踩
在Spring Boot项目中使用Jedis客户端连接Redis时,出现如下异常:
- Exception in thread "main" org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
- at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:281)
- at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:475)
- at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132)
- at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:95)
- at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:82)
- at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:215)
- at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:188)
- at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:96)
- at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:236)
- at cn.zyt.springbootlearning.config.RedisConfiguration.main(RedisConfiguration.java:59)
- Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
- at redis.clients.jedis.util.Pool.getResource(Pool.java:59)
- at redis.clients.jedis.JedisPool.getResource(JedisPool.java:234)
- at redis.clients.jedis.JedisPool.getResource(JedisPool.java:15)
- at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:271)
- ... 9 more
- Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
- at redis.clients.jedis.Protocol.processError(Protocol.java:132)
- at redis.clients.jedis.Protocol.process(Protocol.java:166)
- at redis.clients.jedis.Protocol.read(Protocol.java:220)
- at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:318)
- at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:236)
- at redis.clients.jedis.BinaryJedis.auth(BinaryJedis.java:2229)
- at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:119)
- at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:889)
- at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:424)
- at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:349)
- at redis.clients.jedis.util.Pool.getResource(Pool.java:50)
- ... 12 more
该问题的出现是由于Redis中本身没有设置密码,但连接时指定了密码则会带Auth,这就造成了如上的异常。最简单的解决方法:将连接时设置的密码删掉后,重新尝试异常即解决。
此外如果需要使用Redis的签名认证,可以使用如下两种方式设置Redis中密码:
1. 在指定配置文件启动时:
yitian@heron01:/usr/local/bin$ redis-server /etc/redis/redis.conf
在配置文件中进行如下设置,既可以指定Redis的密码(将下面foobared改为你需要设置的密码),此时在Jedis连接Redis时需要指定密码。
2. 使用命令行设置Redis密码,此时并不会改变配置文件中的设置,但也可以使密码的设置生效。设置后,客户端重新连接server则需要密码。
config set requirepass 123456
查看密码设置的情况:
config get requirepass
带密码命令行登录Server:
redis-cli -p 6379 -a 123456
注意,上述指定redis.conf配置文件启动的方式可以使配置文件中的密码生效,而默认redis-server不带配置文件(或配置文件为默认状态时),是没有设置密码的,所以命令行cli连接无需验证,而Jedis登录也不需要带password。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。