Spring 引导连接到 MongoDB 副本集 运行 仲裁器

Spring boot connect to MongoDB replica set running with an Arbiter

我的应用程序是 Spring 启动应用程序,应用程序配置属性文件如下所示:

....
spring.data.mongodb.host=ip
spring.data.mongodb.port=27017
spring.data.mongodb.admin.database=admin
spring.data.mongodb.database=myDB
spring.data.mongodb.username=su
spring.data.mongodb.password=su1$
....

现在是为了高可用性 MongoDB 已移至 Primary-Secondary-Arbiter 设置。 我应该做哪些更改才能连接到此。尝试用逗号分隔但没有帮助。

如 Spring 引导 documentation 状态:

spring.data.mongodb.host and spring.data.mongodb.port are not supported if you’re using the Mongo 3.0 Java driver. In such cases, spring.data.mongodb.uri should be used to provide all of the configuration.

You can set spring.data.mongodb.uri property to change the URL and configure additional settings such as the replica set.

假设您有一个名为 mc 的副本集,其中 mongo1:27017 作为 Primarymongo2:27017 作为 Secondarymongo3:27017 作为 Arbiter,那么你可以使用:

spring.data.mongodb.uri=mongodb://su:su1$@mongo1:27017,mongo2:27017/myDB?replicaSet=mc

请注意that:

When connecting to a replica set it is important to give a seed list of at least two mongod instances. If you only provide the connection point of a single mongod instance, and omit the replicaSet, the client will create a standalone connection.

使用 MongoDB 连接字符串,您可以设置其他属性,例如 Read Preference。查看 MongoDB documentation 了解更多详情。