使用spring-boot-starter-parent 2.0.0时出现MongoSocketOpenException

时间:2018-03-24 04:49:48

标签: java spring spring-boot spring-data-mongodb

我正在尝试构建https://spring.io/guides/gs/accessing-data-mongodb/提供的spring boot MongoDB项目,该项目使用的是spring-boot-starter-parent 2.0.0.RELEASE, 并获得

堆栈跟踪:

com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.3.jar:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]

Caused by: java.net.SocketException: Socket operation on non-socket (Error setting socket option)
at java.net.PlainSocketImpl.socketSetOption0(Native Method) ~[na:1.8.0_151]
at java.net.PlainSocketImpl.socketSetOption(PlainSocketImpl.java:88) ~[na:1.8.0_151]
at java.net.AbstractPlainSocketImpl.setOption(AbstractPlainSocketImpl.java:275) ~[na:1.8.0_151]
at java.net.Socket.setTcpNoDelay(Socket.java:980) ~[na:1.8.0_151]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:36) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongodb-driver-core-3.6.3.jar:na]
... 3 common frames omitted

当我将spring-boot-starter-parent版本更改为1.5.9.RELEASE或1.5.10.RELEASE时,相同的代码工作正常。因此在使用spring data mongo时版本2.0.0.RELEASE有任何问题。

系统详情:

mongod版本:3.4.13

openjdk version" 1.8.0_151"

1 个答案:

答案 0 :(得分:0)

我必须首先为spring-boot-starter-parent 2.0.x安装并启动mongodb

假设您使用的是MacOS

# to install mongodb
➜  ~ brew install mongodb 
# To have launchd start mongodb now and restart at login
➜  ~ brew services start mongodb 

之后一切都按预期工作:

2018-04-05 22:13:43.189  INFO 26777 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-04-05 22:13:43.229  INFO 26777 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:1}] to localhost:27017
2018-04-05 22:13:43.234  INFO 26777 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 3]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=3049505}
2018-04-05 22:13:43.352  INFO 26777 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-04-05 22:13:43.469  INFO 26777 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:2}] to localhost:27017
2018-04-05 22:13:43.471  INFO 26777 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 3]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1552605}