本文共 6210 字,大约阅读时间需要 20 分钟。
异常情况描述com.mongodb.MongoSocketReadException: Prematurely reached end of stream
当使用mongodb的一主一从一备节点构建的集群,使用java代码连接集群时候,测试主节点master和备用节点slave切换的时候;
2019-10-23 20:21:05,972 WARN [org.mongodb.driver.connection] - Got socket exception on connection [connectionId{ localValue:4, serverValue:15}] to 172.19.32.142:27017. All connections to 172.19.32.142:27017 will be closed.com.mongodb.MongoSocketReadException: Prematurely reached end of stream at com.mongodb.connection.SocketStream.read(SocketStream.java:88) at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494) at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224) at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:96) at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:440) at com.mongodb.connection.WriteCommandProtocol.receiveMessage(WriteCommandProtocol.java:262) at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:104) at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:67) at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:37) at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168) at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289) at com.mongodb.connection.DefaultServerConnection.insertCommand(DefaultServerConnection.java:118) at com.mongodb.operation.MixedBulkWriteOperation$Run$2.executeWriteCommandProtocol(MixedBulkWriteOperation.java:465) at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:656) at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:411) at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:177) at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:168) at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:426) at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:417) at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168) at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74) at com.mongodb.Mongo.execute(Mongo.java:845) at com.mongodb.Mongo$2.execute(Mongo.java:828) at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:550) at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:317) at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:307) at mongotest.TestMongoDBReplSet.main(TestMongoDBReplSet.java:39)
Got socket exception on connection [connectionId{ localValue:4, serverValue:15}] to 172.19.32.142:27017. All connections to 172.19.32.142:27017 will be closed.
这段代码错误提示表明:主备容灾切换之后,原来的master不再提供写服务,写服务转移到另外一台机器中,此时java驱动连接元信息中没有感知到这种变化,集群中改变的元信息没有同步到当前的mongodb连接池中的连接中,导致连接失败!
原始连接的java代码,没有加上try-catch{},导致主备切换之后出现错误,现在加上之后,一斤可以正常的了解上集群了。
package mongotest;import com.mongodb.*;import com.mongodb.client.FindIterable;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoCursor;import com.mongodb.client.MongoDatabase;import org.bson.Document;import java.util.ArrayList;import java.util.List;/** * 描述: java连接mongdb集群;测试高可用性 * @author: fangchangtan * @version 创建时间:2018年11月26日 下午7:45:29 */public class TestMongoDBReplSet1 { public static void main(String[] args) { ArrayListarrayList = new ArrayList<>(); //mongpdb的java连接配置为一个集群模式 List addresses = new ArrayList (); ServerAddress address1 = new ServerAddress("172.19.32.141" , 27017); ServerAddress address2 = new ServerAddress("172.19.32.141" , 27027); ServerAddress address3 = new ServerAddress("172.19.32.141" , 27037); addresses.add(address1); addresses.add(address2); addresses.add(address3); // 3.用来做mongo复制集的基本配置 MongoClientOptions.Builder builder = new MongoClientOptions.Builder();// //线程等待连接变为可用的最长时间.// builder.maxWaitTime(20000);// //连接超时时间,必须大于0// builder.connectTimeout(1000 * 5);// //设置服务器选择超时(以毫秒为单位),它定义驱动程序在抛出异常之前等待服务器选择成功的时间// //值为0表示如果没有可用的服务器,它将立即超时。 负值意味着无限期等待// builder.serverSelectionTimeout(1000 * 30);// // 4.自动重连// // 7.每个连接上的线程数// builder.threadsAllowedToBlockForConnectionMultiplier(50);// builder.maxConnectionIdleTime(5000);//set the max wait time in (ms) MongoClient mongoClient = new MongoClient(addresses,builder.build()); MongoDatabase database = mongoClient.getDatabase("test"); MongoCollection collection = database.getCollection("testdb"); for (int i = 0; i < 1000; i++) { System.out.println("=========================="); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } //向集群中插入文档数据// collection.insertOne(new Document("name","dog"+i)); try { collection.deleteMany(new BasicDBObject("_id", new BasicDBObject("$ne", null))); collection.insertOne(new Document("name","dog"+i)); }catch (MongoSocketException e){ System.out.println("++++++++++++++++++++++"); System.out.println(e); mongoClient = new MongoClient(addresses,builder.build()); } //查询集群中的数据记录 FindIterable find = collection.find(); MongoCursor iterator = find.iterator(); while (iterator.hasNext()) { final Document document = iterator.next(); System.out.println(document); } } }}
小伙伴们:如果解决了你的问题,麻烦给点个赞! 本人最新需要你的鼓励!-V-
转载地址:http://emvlf.baihongyu.com/