java - MongoSocketReadTimeoutException:接收訊息時逾時
为情所困
为情所困 2017-05-31 10:35:10
0
2
3844

1.最近在使用spring-boot和用spring-data-mongo操作mongodb

2.每天早上查看伺服器日誌都發現報了同一個錯,估計是在一整晚沒人訪問伺服器,第二天一旦有人訪問就會發生錯誤

3.錯誤日誌為:

07:51:35.185 [cluster-ClusterId{value='59295e6c3532f223626a2ca0', description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017 
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
    at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:477)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:228)
    at com.mongodb.connection.CommandHelper.receiveReply(CommandHelper.java:134)
    at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:121)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:85)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:45)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at com.mongodb.connection.SocketStream.read(SocketStream.java:85)
    at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224)
    ... 8 common frames omitted
07:51:31.683 [cluster-ClusterId{value='59295e583532f22362cf3b92', description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017 
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
    at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:477)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:228)
    at com.mongodb.connection.CommandHelper.receiveReply(CommandHelper.java:134)
    at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:121)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:85)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:45)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at com.mongodb.connection.SocketStream.read(SocketStream.java:85)
    at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224)
    ... 8 common frames omitted
07:51:35.780 [cluster-ClusterId{value='59295e403532f2236234b535', description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017 
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
    at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:477)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:228)
    at com.mongodb.connection.CommandHelper.receiveReply(CommandHelper.java:134)
    at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:121)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:85)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:45)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:171)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at com.mongodb.connection.SocketStream.read(SocketStream.java:85)
    at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494)
    at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224)
    ... 8 common frames omitted
08:01:39.239 [cluster-ClusterId{value='59295e583532f22362cf3b92', description='null'}-localhost:27017] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:9, serverValue:828}] to localhost:27017 
08:01:39.248 [cluster-ClusterId{value='59295e583532f22362cf3b92', description='null'}-localhost:27017] INFO  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, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=340319} 
08:01:39.273 [cluster-ClusterId{value='59295e6c3532f223626a2ca0', description='null'}-localhost:27017] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:7, serverValue:827}] to localhost:27017 
08:01:39.274 [cluster-ClusterId{value='59295e403532f2236234b535', description='null'}-localhost:27017] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:7, serverValue:826}] to localhost:27017 
08:01:39.274 [cluster-ClusterId{value='59295e403532f2236234b535', description='null'}-localhost:27017] INFO  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, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=105791} 
08:01:39.274 [cluster-ClusterId{value='59295e6c3532f223626a2ca0', description='null'}-localhost:27017] INFO  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, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=532381}

4.嘗試過一些解決方法,把連結mongodb的程式碼改成:

new MongoClient(new ServerAddress(env.getProperty("mongo.host"), Integer.valueOf(env.getProperty("mongo.port"))), Arrays.asList(credential), MongoClientOptions.builder()
                .socketTimeout(10000)
                .minHeartbeatFrequency(25)
                .heartbeatSocketTimeout(3000)
                .socketKeepAlive(true)
                .build());
                

也把伺服器(Ubuntu)ipv4的keep alive時間改成了120秒。

但是錯誤還在,希望大家指教指教,謝謝!

为情所困
为情所困

全部回覆(2)
伊谢尔伦

多半是MongoDB把Java保持的連線踢掉了(狀態變成CLOSE_WAIT,只能發資料但無法再收到),可以從兩個地方想想辦法:

  1. 把連接池配置好(這個貌似很複雜),或者乾脆不用連接池,即socketKeepAlive設成false

  2. 因為你的問題都出現在早上,可以檢查MongoDB有沒有凌晨重置連線的策略。

左手右手慢动作

1、可能是遇到了查詢時間過長的Read,不妨看看profiling或者server log日誌的信息,找到查詢時間比較長的語句,例如超過10秒的語句,看是否能優化;

2、適當提高socketTimeout一些,看是否能緩解這種出錯的情況。

供參考。

Love MongoDB! Have fun!


2017MongoDB中文社群北京用戶群組大會在即,2017年6月3日 13:00-18:00

還有T恤等小禮物等!呵呵!

報名請入

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板