MongoDB更改流副本集恢复

时间:2018-05-02 17:29:59

标签: spring mongodb mongo-java-driver

我使用Spring实现了MongoDB更改流,当副本集主节点启动时,它可以正常工作。

@Service
public class ChangeEventService {
    private static final Logger logger = LoggerFactory.getLogger(ChangeEventService.class);
    private final MongoClient mongoClient;
    public ChangeEventService(MongoClient mongoClient) {
        this.mongoClient = mongoClient;
    }
    @PostConstruct
    public void subscribe() {
        MongoDatabase db = mongoClient.getDatabase("experiment");
        MongoCollection<Document> collection = db.getCollection("debug");
        Block<ChangeStreamDocument<Document>> printBlock = changeStreamDocument -> {
            logger.info("Received: {}", changeStreamDocument.getFullDocument().toString());
            BsonDocument resumeToken = changeStreamDocument.getResumeToken();
        };
        collection.watch().forEach(printBlock);
        logger.info("Consumer is ready to process");
    }
}

然后我关闭副本集的主节点。我期待更改流等待副本集选择新主节点并继续获取数据更改。实际行为是应用程序崩溃。

从日志中我可以看到与主服务器(27000)的连接已关闭,这是预期的,然后它似乎尝试打开与其中一个辅助服务器(27001)的连接但是因为该服务器而无法访问已关闭。

来自文档:&#34;更改流绑定到集合,更改流文档使用游标进行迭代。只要与MongoDB部署的连接保持打开且集合存在,该游标将保持打开状态,直到它被显式关闭。&#34;

2018-05-02 12:03:03.424  INFO 9560 --- [           main] c.e.m.service.ChangeEventService         : Received: Document{{_id=5ae98cd7dcc8921c94d5f9e5, _class=com.mongodb.BasicDBObject, uuid=4f836d00-efc3-4d48-956a-af4dbfed90e7, now=Wed May 02 12:03:03 CEST 2018}}
2018-05-02 12:03:06.500  WARN 9560 --- [           main] org.mongodb.driver.connection            : Got socket exception on connection [connectionId{localValue:4, serverValue:8}] to localhost:27000. All connections to localhost:27000 will be closed.
2018-05-02 12:03:06.501  INFO 9560 --- [           main] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:4, serverValue:8}] to localhost:27000 because there was a socket exception raised by this connection.
2018-05-02 12:03:07.502  INFO 9560 --- [           main] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:6}] to localhost:27000 because there was a socket exception raised by this connection.
2018-05-02 12:03:07.504  WARN 9560 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'changeEventService': Invocation of init method failed; nested exception is com.mongodb.MongoSocketOpenException: Exception opening socket
2018-05-02 12:03:07.505  INFO 9560 --- [localhost:27000] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27000
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:128) ~[mongodb-driver-core-3.6.3.jar:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_161]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_161]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_161]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_161]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_161]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_161]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_161]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_161]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_161]
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59) ~[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
2018-05-02 12:03:07.507  INFO 9560 --- [           main] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:7, serverValue:181}] to localhost:27001
2018-05-02 12:03:07.508  INFO 9560 --- [           main] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:7, serverValue:181}] to localhost:27001 because the pool has been closed.
2018-05-02 12:03:07.511  INFO 9560 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2

1 个答案:

答案 0 :(得分:1)

这里有两件事:

  

at com.mongodb.connection.SocketStream.open(SocketStream.java:57)〜[mongodb-driver-core-3.6.3.jar:na]

MongoDB Java driver v3.6.3存在错误,当试图杀死现有游标失败时,change streams游标不会恢复。这在JAVA-2821中描述,并在3.7.0版本中修复。

  

collection.watch()的forEach(printBlock);

watch()方法实际上并没有联系服务器,而应该使用迭代器方法。例如:

MongoCursor<ChangeStreamDocument<Document>> cursor = collection.watch().iterator();

ChangeStreamDocument<Document> next = cursor.next();

while(cursor.hasNext()){
    next = cursor.next();
    System.out.println(next);
}

另请参阅Spec: Resumable Error了解可被视为可恢复的错误的定义。