我目前正在编写一个应该使用 MongoDB 副本集的应用程序。它是一个基于 Spring Boot 的应用程序,以下属性可以很好地连接到一台服务器:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=demo
这对我的本地开发环境来说绝对没问题。但稍后它应该针对 MongoDB 副本集运行,所以我必须提供至少 2 个,更好的 3 个副本集种子,但是我如何使用属性来做到这一点?
我看过这个页面:http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html ,但没有提到副本集的显式属性。 提供一个逗号分隔的地址列表,如下所示:
spring.data.mongodb.host=127.0.0.1,127.0.1.1,127.0.2.1
spring.data.mongodb.uri=mongo://127.0.0.1,mongo://127.0.0.1:27018
(我一个接一个地试了。)
这也不起作用(事实上,它会产生一个异常,让 Spring 使用默认配置)。
我也尝试使用以下 config.xml,但没有成功:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation=
"http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mongo:mongo id="replicaSetMongo" replica-set="127.0.0.1:27017,localhost:27018"/>
</beans>
我知道上面的配置略有不同,但我目前正在尝试获取一个异常,显示没有可访问的副本集节点。
有什么想法、提示吗?
最佳答案
没有明确的支持,没有。但是您应该可以通过 uri
参数进行配置。
我们实际上已经更新了 the documentation最近。
关于java - 如何通过属性配置 spring-data-mongodb 以使用副本集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31839777/