博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
阅读量:2058 次
发布时间:2019-04-29

本文共 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) {
ArrayList
arrayList = 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/

你可能感兴趣的文章
使用Maven构建的简单的单模块SSM项目
查看>>
Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题
查看>>
Redis学习笔记(四)—— redis的常用命令和五大数据类型的简单使用
查看>>
使用 GDB + Qemu 调试 Linux 内核
查看>>
介绍一个小工具:SSL-exporter
查看>>
深入理解 tc ebpf 的 direct-action (da) 模式(2020)
查看>>
为容器时代设计的高级 eBPF 内核特性(FOSDEM, 2021)
查看>>
Loki系列文章
查看>>
使用 Prometheus 监控 WireGuard
查看>>
cgroup 挂载失败是什么鬼???
查看>>
深入 kubernetes API 的源码实现
查看>>
真香!使用 Goland 网页版实现真正的云开发
查看>>
又超时了!Etcd分布式锁你用明白了吗?
查看>>
工程师应该怎么学习
查看>>
记一次 Kubernetes 机器内核问题排查
查看>>
记一次 Kubernetes 中严重的安全问题
查看>>
拥抱云原生,基于 eBPF 技术实现 Serverless 节点访问 K8S Service
查看>>
张磊大神的《深入剖析Kubernetes》终于出书啦!
查看>>
KubeSphere 团队(青云QingCloud) 全职开源职位等你加入!
查看>>
真棒!3 种方法限制 Pod 磁盘容量,瞬间豁然开朗
查看>>