微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

关于MongoDB读取连接超时的异常

原文链接https://blog.csdn.net/weixin_34417814/article/details/86198544

引出个问题! 

HTTP Status 500 - Read operation to server 192.168.1.110:20001 Failed on database wpdb; nested exception is com.mongodb.MongoException$Network: Read operation to server 192.168.1.110:20001 Failed on database wpdb
 
--------------------------------------------------------------------------------
 
type Exception report
 
message Read operation to server  xxxxxxx:xxxxx Failed on database wpdb; nested exception is com.mongodb.MongoException$Network: Read operation to server xxxxxx:8001 Failed on database wpdb
 
description The server encountered an internal error that prevented it from fulfilling this request.
 
exception 
 
org.springframework.dao.DataAccessResourceFailureException: Read operation to server 192.168.1.110:20001 Failed on database wpdb; nested exception is com.mongodb.MongoException$Network: Read operation to server 192.168.1.110:20001 Failed on database wpdb
	org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:56)
	org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1913)
	org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1743)
	org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1557)
	org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:497)
	com.geoway.ime.tile.dao.impl.TileDaoImpl.getTile(TileDaoImpl.java:60)
	com.geoway.ime.tile.service.impl.TileServiceImpl.getTile(TileServiceImpl.java:105)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	java.lang.reflect.Method.invoke(Method.java:597)
	org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
	org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
	org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	org.springframework.cache.interceptor.CacheInterceptor$1.invoke(CacheInterceptor.java:58)
	org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:211)
	org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
	org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
	com.sun.proxy.$Proxy50.getTile(UnkNown Source)
	com.geoway.ime.server.rest.TileServer.getTileKVP(TileServer.java:60)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	java.lang.reflect.Method.invoke(Method.java:597)
	com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
	com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethoddispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethoddispatchProvider.java:185)
	com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethoddispatcher.dispatch(ResourceJavaMethoddispatcher.java:75)
	com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
	com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
	com.sun.jersey.server.impl.uri.rules.RightHandpathRule.accept(RightHandpathRule.java:147)
	com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
	com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511)
	com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442)
	com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
	com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
	com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
	com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
	com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
 
 

 

由异常来看,是由于MongoDB连接超时造成的。于是,往MongoDB上找原因。首先上MongoDB的配置文件

mongodb.replica-set=xxxxxx:xxx,xxxxx:xxxx
mongodb.hostname=xxxxxxxxx
mongodb.port=7001
mongodb.connections-per-host=80
mongodb.threads-allowed-to-block-for-connection-multiplier=8
mongodb.connect-timeout=3000
mongodb.max-wait-time=2000
mongodb.auto-connect-retry=true
mongodb.socket-keep-alive=true
mongodb.socket-timeout=2000
mongodb.slave-ok=true
mongodb.write-number=1
mongodb.write-timeout=1000
mongodb.write-fsync=true
mongodb.dbname=xxxxx
mongodb.dbuser=xxx
mongodb.dbpassword=xxxx

连接时间2秒。然后在定位mongodb的查询,结果为6、7秒。原来是这个原因。然后定位,一个查询为什么耗时那么久呢?

经过排查,原来是查询字段没有创建索引。然后把索引创建,查询几十毫秒; 即使没有加索引 ,我们也要考虑到写的瓶颈,不能影响服务整体性能 ,因此设置个 write-timeout是很重要,当天我没有特意去压测过;读的超时也是要关注的;但一般我们是在从库上压力且一般是统计类的,不建议在服务中使用;应该建议使用跑脚本的方式;

 

关于MongoDB的Write Concern 

https://blog.csdn.net/yolohohohoho/article/details/89892294 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐