我目前正在写一个应用程序,它应该使用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>
我知道上面的配置略有不同,但我目前尝试的是获得一个异常,显示没有可到达的副本集节点。
你有什么想法或提示吗?