JedisPool连接redis出现NOAUTH Authentication required 异常的解决方法
在项目中使用JedisPool连接redis出现NOAUTH Authentication required 的异常:[2021-07-18 01:53:34] [http-nio-80-exec-9] ERROR jedis.JedisFactory:216: Error while validating pooled Jedis object.redis.clients.jedis.excep
·
在项目中使用JedisPool连接redis出现NOAUTH Authentication required 的异常:
[2021-07-18 01:53:34] [http-nio-80-exec-9] ERROR jedis.JedisFactory:216: Error while validating pooled Jedis object.
redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.
at redis.clients.jedis.Protocol.processError(Protocol.java:135) ~[jedis-3.6.1.jar:?]
at redis.clients.jedis.Protocol.process(Protocol.java:169) ~[jedis-3.6.1.jar:?]
at redis.clients.jedis.Protocol.read(Protocol.java:223) ~[jedis-3.6.1.jar:?]
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:352) ~[jedis-3.6.1.jar:?]
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:270) ~[jedis-3.6.1.jar:?]
at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:381) ~[jedis-3.6.1.jar:?]
at redis.clients.jedis.JedisFactory.validateObject(JedisFactory.java:214) [jedis-3.6.1.jar:?]
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:479) [commons-pool2-2.9.0.jar:2.9.0]
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:356) [commons-pool2-2.9.0.jar:2.9.0]
at redis.clients.jedis.util.Pool.getResource(Pool.java:75) [jedis-3.6.1.jar:?]
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:366) [jedis-3.6.1.jar:?]
at ltd.qiuhonghao.justice.league.shopping.controller.TestController.getMessage(TestController.java:66) [classes/:?]
....
出现异常的代码:
@GetMapping("/redis")
public String getMessage(){
log.info(jedisPoolConfig.toString());
log.info(jedisPool.toString());
// 在try的括号种进行创建资源连接,会在try语句结束后自动释放,前提是这些可关闭的资源必须实现 java.lang.AutoCloseable 接口。
try (Jedis jedis = jedisPool.getResource()){
coffeeService.getAllCoffee().forEach(c ->{
jedis.hset("springbucks-menu",c.getName(),Long.toString(c.getPrice().getAmountMinorLong()));
});
Map<String, String> menu = jedis.hgetAll("springbucks-menu");
log.info("Menu:{}",menu);
String price = jedis.hget("springbucks-menu", "espresso");
log.info("espresso - {}", Money.ofMinor(CurrencyUnit.of("CNY"), Long.parseLong(price)));
}
return jedisPool.toString();
}
原因: 根据报错信息提示,是需要进行权限的验证。而联想到redis设置了密码,极有可能是没有Jedis连接池没有设置密码导致的。看了一下配置类,new JedisPool(jedisPoolConfig(),host) 这里根本没有设置密码:
@Slf4j
@Configuration
public class JedisConfiguration {
@Bean
@ConfigurationProperties(value = "diy.redis")
public JedisPoolConfig jedisPoolConfig(){
return new JedisPoolConfig();
}
@Bean(destroyMethod = "close")
public JedisPool jedisPool(@Value("${diy.redis.host}") String host){
return new JedisPool(jedisPoolConfig(),host);
}
}
解决:
在application.properties 配置好参数,构造JedisPool 的时候传参即可。
@Slf4j
@Configuration
public class JedisConfiguration {
@Value("${diy.redis.password}")
private String password;
@Bean
@ConfigurationProperties(value = "diy.redis")
public JedisPoolConfig jedisPoolConfig(){
return new JedisPoolConfig();
}
@Bean(destroyMethod = "close")
public JedisPool jedisPool(@Value("${diy.redis.host}") String host){
return new JedisPool(jedisPoolConfig(),host,6379,2000,password);
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)