项目场景:
项目场景:springboot 项目连接阿里云 mongo。
问题描述:
再启动项目和查询mongo过程中报错:
No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description C
lusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=xxx:3717, type=UNKNOWN, state=CONNECTING, excepti
on={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, ServerDescription{address=xxx:3717, type=UNKNOWN, state=CONNECTING, excep
tion={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]}. Waiting for 30000 ms before timing out
[2020-10-22 09:09:01.844][ERROR][unknown_8ea74c3c095b4c689a1b989f25fc6d9f][c.o.o.c.c.CRBDataQueryController]- userMobileList error 2348162338435 null 499611240792850432
org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=xxx:3717, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, {address=xxx:3717, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=xxx:3717, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, {address=xxx:3717, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]
解决方案:
首先报错信息是非常重要的,不要视而不见
Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}.
刚开始根据这关键字去查资料,网上的解决方案以下几类:
- 设置timeout maxIdleTimeMS=3000
- mongo java 驱动版本问题
- 用户名密码、地址、端口等问题
- 防火墙问题
然后一个个分析,
对于1. 确实认为大概率是这方面问题,于是根据博文解决异常:xxx,
设置了超时参数,但结果是无用。
对于2,由于使用的springboot, 1.5.3, 找到对应的MongoDB驱动版本(3.4.2),查看MongoDB版本(3.4.6)后,去官网查看是否对应,答案是可用,所以也不是版本驱动的问题。
对于3,这个大概率不会错,但是难保万一,于是专门对了一遍用户名、密码、端口、地址,结果是没有错。
对于4,嘴不相信的,因为是运维会配置好对应的权限。结果自己去服务器尝试telnet , 发现不通, 然后找运维解决,功能正常。
结论
对于找到问题,最可以依赖的只有事实和逻辑,不要被经验主义,先入为主的观念主导,生活也是如此。

1万+

被折叠的 条评论
为什么被折叠?



