redis报错汇总
实例化报错->连接报错->权限报错。此报错是有顺序的:例如,若连接报错,说明实例化完成,即配置文件配对了。若权限报错,说明连接通了,但密码错误。若实例化就报错,说明配置文件配错了,没法启动redis客户端,更别说去连接了。
具体报错如下:
1.实例化报错
Failed to load ApplicationContext.
Error creating bean with name 'jedisPool' defined in class path resource [applicationContext-redis.xml]:
Unsatisfied dependency expressed through constructor parameter 0:
Ambiguous argument values for parameter of type [org.apache.commons.pool2.impl.GenericObjectPoolConfig]
- did you specify the correct bean references as arguments?
出现此错误,通常是配置文件出错:配置JedisPool出错。
2.连接报错
connect timed out
出现此错误,通常是网络问题。一般在公司里,内网外网防火墙等各种网络情况。记得切换网络。
3.权限报错
1.没有配password(访问需要密码(redis服务端设置了密码))
NOAUTH Authentication required.
出现此错误,说明jedis客户端配置文件没有配password。
ERR invalid password
需要注意:
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.100.12"/>
<constructor-arg name="port" value="6379"/>
<constructor-arg name="password" value="xxx"/>
</bean>
查看jedis源码,发现设置密码,JedisPool的构造参数如下:
public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password) {
this(poolConfig, host, port, timeout, password, 0, (String)null);
}
<bean class="redis.clients.jedis.JedisPool" id="jedisPool" >
<constructor-arg name="host" value="192.168.100.12"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
<constructor-arg name="password" value="xxx"></constructor-arg>
<constructor-arg name="timeout" value="3000"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig">
<property name="maxIdle" value="300" />
<property name="maxTotal" value="1000" />
<property name="maxWaitMillis" value="1000" />
<property name="testOnBorrow" value="false" />
<property name="blockWhenExhausted" value="false" />
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.100.12"/>
<constructor-arg name="port" value="6379"/>
</bean>
因为JedisPool提供了只需要ip地址和端口的构造参数,如下:
public JedisPool(String host, int port) {
this(new GenericObjectPoolConfig(), host, port, 2000, (String)null, 0, (String)null);
}
补充:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 这中间写bean -->
<!-- <bean id="jedisPool" class="redis.clients.jedis.JedisPool">-->
<!-- <constructor-arg name="host" value="192.168.100.12"/>-->
<!-- <constructor-arg name="port" value="6379"/>-->
<!-- </bean>-->
</beans>
单元测试类:
@RunWith(SpringRunner.class)//spring整合JUnit4
@ContextConfiguration(locations={"classpath:applicationContext-redis.xml"})//加载spring配置文件
public class BaseRedisTest {
}
==================分割线====================
Failed to load ApplicationContext.
Error creating bean with name ‘jedisPool’ defined in class path resource [applicationContext–redis.xml]:
Unsatisfied dependency expressed through constructor parameter 0:
Ambiguous argument values for parameter of type [org.apache.commons.pool2.impl.GenericObjectPoolConfig]
– did you specify the correct bean references as arguments?
NOAUTH Authentication required.
<bean id=”jedisPool” class=”redis.clients.jedis.JedisPool”>
<constructor-arg name=”host” value=”192.168.100.12″/>
<constructor-arg name=”port” value=”6379″/>
<constructor-arg name=”password” value=”xxx“/>
</bean>
public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password) {
this(poolConfig, host, port, timeout, password, 0, (String)null);
}
<bean class=”redis.clients.jedis.JedisPool” id=”jedisPool” >
<constructor-arg name=”host” value=”192.168.100.12″></constructor-arg>
<constructor-arg name=”port” value=”6379″></constructor-arg>
<constructor-arg name=”password” value=”xxx“></constructor-arg>
<constructor-arg name=”timeout” value=”3000″></constructor-arg>
<constructor-arg name=”poolConfig” ref=”jedisPoolConfig”></constructor-arg>
</bean>
<bean class=”redis.clients.jedis.JedisPoolConfig” id=”jedisPoolConfig”>
<property name=”maxIdle” value=”300″ />
<property name=”maxTotal” value=”1000″ />
<property name=”maxWaitMillis” value=”1000″ />
<property name=”testOnBorrow” value=”false” />
<property name=”blockWhenExhausted” value=”false” />
</bean>
<bean id=”jedisPool” class=”redis.clients.jedis.JedisPool”>
<constructor-arg name=”host” value=”192.168.100.12″/>
<constructor-arg name=”port” value=”6379″/>
</bean>
public JedisPool(String host, int port) {
this(new GenericObjectPoolConfig(), host, port, 2000, (String)null, 0, (String)null);
}
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans“
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd“>
<!– 这中间写bean –>
<!– <bean id=”jedisPool” class=”redis.clients.jedis.JedisPool”>–>
<!– <constructor-arg name=”host” value=”192.168.100.12″/>–>
<!– <constructor-arg name=”port” value=”6379″/>–>
<!– </bean>–>
</beans>
@RunWith(SpringRunner.class)//spring整合JUnit4
@ContextConfiguration(locations={“classpath:applicationContext–redis.xml”})//加载spring配置文件
public class BaseRedisTest {
}
原文地址:https://blog.csdn.net/u011149152/article/details/132476709
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_35940.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!