最近因为jedis 2.1.0接口的一些问题,升级版本了版本,结果出现了一个诡异的问题;时常能正常启动,时常出现jedisPool初始化时 java.lang.IllegalArgumentException: hostname can’t be null
原因出在spring JedisPool构造参数上,之前spring的配置如下
<bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg ref="jedisConfig" /> <constructor-arg value="${redis.host}" /> <constructor-arg type="int" value="${redis.port}" /> </bean>
明显是因为host没有注入,但时常又可以,诡异!!!之后弄了jedis源码,加了日志,才定位到原因,查看JedisPool源码,其中的两构造函数如下
public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, final int port) { this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, null); } public JedisPool(final GenericObjectPoolConfig poolConfig, final URI uri, final int timeout) { super(poolConfig, new JedisFactory(uri.getHost(), uri.getPort(), timeout, JedisURIHelper.getPassword(uri), JedisURIHelper.getDBIndex(uri) != null ? JedisURIHelper.getDBIndex(uri) : 0, null)); }
我们调用的是第一个构造函数,但事实是可能会调用第二个构造函数;port当做了timeout,host当做了URI。
不是很明白spring为什么会把value是字符串的参数匹配到URI上,那就明确指定类型吧
<bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg ref="jedisConfig" /> <constructor-arg type="java.lang.String" value="${redis.host}" /> <constructor-arg type="int" value="${redis.port}" /> </bean>
jedis之前的异常为,方面遇到同问题的同学
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at redis.clients.util.Pool.getResource(Pool.java:50) at redis.clients.jedis.JedisPool.getResource(JedisPool.java:88) at com.renhenet.modules.redis.RedisService.getHash(RedisService.java:364) at com.renhenet.modules.member.MemberRedisService.getMemberIndustryByMemberId(MemberRedisService.java:240) at com.renhenet.modules.member.MemberService.getMemberIndustry(MemberService.java:236) at com.renhenet.modules.member.MemberService$$FastClassByCGLIB$$b34212a.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631) at com.renhenet.modules.member.MemberService$$EnhancerByCGLIB$$1638ea72.getMemberIndustry(<generated>) at com.renhenet.fw.struts2.interceptor.LoginRenheInterceptor.intercept(LoginRenheInterceptor.java:68) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at com.renhenet.fw.struts2.interceptor.WebRenheInterceptor.intercept(WebRenheInterceptor.java:27) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563) at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99) at com.renhenet.fw.struts2.filter.RenheStruts2Filter.doFilter(RenheStruts2Filter.java:32) at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:87) at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:187) at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:265) at com.caucho.server.hmux.HmuxRequest.handleRequest(HmuxRequest.java:436) at com.caucho.server.port.TcpConnection.run(TcpConnection.java:682) at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:730) at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:649) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.IllegalArgumentException: hostname can't be null at java.net.InetSocketAddress.checkHost(InetSocketAddress.java:149) at java.net.InetSocketAddress.<init>(InetSocketAddress.java:216) at redis.clients.jedis.Connection.connect(Connection.java:142) at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:75) at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1572) at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:69)
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索函数
null
jedis redis 版本、redis和jedis的关系、jedis 密码访问redis、jedis和redis的区别、redisson jedis 比较,以便于您获取更多的相关知识。
时间: 2024-09-25 06:11:56