gpt4 book ai didi

java - Spring boot 连接到使用 Arbiter 运行的 MongoDB 副本集

转载 作者:可可西里 更新时间:2023-11-01 09:56:37 28 4
gpt4 key购买 nike

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

....
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 Boot 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 作为 < em>Secondary 和 mongo3: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 连接字符串,您可以设置其他属性,例如读取首选项。看看 MongoDB documentation了解更多详情。

关于java - Spring boot 连接到使用 Arbiter 运行的 MongoDB 副本集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39342401/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com