简体   繁体   English

崩溃后,Mongodb java驱动程序无法重新连接到主驱动程序

[英]Mongodb java driver cannot reconnect to primary after a crash

I have an issue when trying the failover capacity of mongodb with the java driver. 使用java驱动程序尝试使用mongodb的故障转移容量时遇到问题。

I have a cluster of primary/secondary mongodb, namely server1 and server2. 我有一个主要/辅助mongodb集群,即server1和server2。

When I kill the primary server1 to simulate a failure the server2 becomes primary in a few seconds and my applications, that access mongo using the java driver, start using the new primary server2. 当我杀死主服务器1以模拟故障时,server2在几秒钟内变为主服务器,而我的应用程序(使用java驱动程序访问mongo)开始使用新的主服务器2。

When I re-start server1, it takes back it's primary role in a few seconds but then my application, instead of connecting to the server1 still tries to connect to server2 and, as it's now in secondary state failled! 当我重新启动server1时,它会在几秒钟内恢复它的主要角色但是我的应用程序,而不是连接到server1仍然尝试连接到server2,因为它现在处于辅助状态失败! All the requests are failling with this error : 所有请求都对此错误感到满意:

com.mongodb.MongoServerSelectionException: Unable to connect to any server that matches {serverSelectors=[ReadPreferenceServerSelector{readPreference=primary}, LatencyMinimizingServerSelector{acceptableLatencyDifference=15 ms}]}

I am using a mongodb 2.6 and a java driver 2.12. 我使用的是mongodb 2.6和java驱动程序2.12。 I am not passing any parameter to my MongoClient that is created using all the nodes of my cluster. 我没有将任何参数传递给使用我的集群的所有节点创建的MongoClient。

Any help will be welcome. 欢迎任何帮助。

Regards, 问候,

Loïc 卢瓦克

As the question is more than a year old, I will be using MongoDB 3.0 with Java driver 3.0.4 for my solution. 由于问题超过一年,我将使用MongoDB 3.0Java驱动程序3.0.4作为我的解决方案。

Suppose there are 3 nodes (1 primary and 2 secondary) in a replica set (which is recommended minimum no. of nodes in a replica set) on ports 27017, 27018 and 27019 respectively. 假设在端口27017,27018和27019上的副本集(建议的副本集中最小节点数)中有3个节点(1个主节点和2个辅助节点)。 So I can create a connections with the above config as follows: 所以我可以用上面的配置创建一个连接,如下所示:

MongoClient mongo = new MongoClient(asList(new ServerAddress("localhost",27017),
            new ServerAddress("localhost", 27018), new ServerAddress("localhost", 27019),
            new MongoClientOptions.builder().requiredReplicaSetName("replset_name").build()));

Note that using MongoClientOptions , the driver guarantee that the above servers are indeed members of the said replica set. 请注意,使用MongoClientOptions ,驱动程序保证上述服务器确实是所述副本集的成员。 You can skip this if you don't have a replica set. 如果您没有副本集,可以跳过此步骤。

During fail overs the driver will switch to the new primary automatically and start sending requests to it, but you have to take care of the exceptions thrown during CRUD operations . 在故障转移期间,驱动程序将自动切换到新的主服务器并开始向其发送请求,但您必须处理CRUD操作期间抛出的异常

As there as still some people arriving at this issue and trying to answer I answer it myself. 因为还有一些人到达这个问题并试图回答我自己回答。

The issue is no longer occurring, I'm not sure why. 问题不再发生,我不知道为什么。 I now update my driver to the 2.12.3 version. 我现在将我的驱动程序更新到2.12.3版本。 No update on mongodb side. mongodb方面没有更新。

Thanks for those who try to help. 感谢那些试图提供帮助的人。

We had the same problem. 我们遇到了同样的问题。 Upgrading the Mongo java driver to version 2.13.3 seems to solve the problem 将Mongo java驱动程序升级到2.13.3版似乎解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM