Java 如何配置 spring-data-mongodb 以通过属性使用副本集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31839777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure spring-data-mongodb to use a replica set via properties
提问by incredibleholg
I am currently writing an application which should use a replica set of MongoDB. It is a Spring Boot based application and the following properties work perfectly fine to connect to one server:
我目前正在编写一个应用程序,它应该使用 MongoDB 的副本集。它是一个基于 Spring Boot 的应用程序,以下属性可以很好地连接到一台服务器:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=demo
This is absolutely fine for my local dev environment. But later on it should run against a MongoDB replica set, so I have to provide at least 2, better 3 replica set seeds, but how can I do this with properties?
这对于我的本地开发环境来说绝对没问题。但是稍后它应该针对 MongoDB 副本集运行,所以我必须提供至少 2 个,更好的 3 个副本集种子,但是我如何使用属性来做到这一点?
I had a look on this page: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html, but there is no explicit property for replica sets mentioned. Providing a comma separated list of addresses like this:
我在这个页面上看过: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
(I tried one after another.)
(我一个接一个地尝试。)
This is also not working (in fact, it produces an exception which lets Spring uses the default configuration).
这也不起作用(实际上,它产生了一个异常,让 Spring 使用默认配置)。
I also tried to use the following config.xml, with no luck:
我也尝试使用以下 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>
I know that the above configs are slightly different, but what I am currently trying is to get an exception which is showing me that no replica set node was reachable.
我知道上面的配置略有不同,但我目前正在尝试的是获取一个异常,该异常显示无法访问副本集节点。
Any ideas, hints?
任何想法,提示?
采纳答案by Stephane Nicoll
There is no explicit support for that, no. But you should be able to configure that just fine via the uri
parameter.
没有明确的支持,没有。但是您应该能够通过uri
参数很好地配置它。
We've actually updated the documentationrecently.
我们最近实际上更新了文档。
回答by nwolfe
I had a similar problem and I dug into the MongoProperties::createMongoClient()
code and found that the code was ignoring the uri value if there were any values configured for spring.data.mongodb.host
, spring.data.mongodb.port
, spring.data.mongodb.username
or spring.data.mongodb.password
.
我有一个类似的问题,我挖成MongoProperties::createMongoClient()
代码,发现代码被忽略的URI值,如果有配置任何值spring.data.mongodb.host
,spring.data.mongodb.port
,spring.data.mongodb.username
或spring.data.mongodb.password
。
If I put all that information in the URI (and removed all the other spring.data.mongodb.*
values from the property file), the connection code worked.
如果我将所有这些信息都放在 URI 中(并spring.data.mongodb.*
从属性文件中删除了所有其他值),则连接代码会起作用。
The URI property setting ended up looking like this:
URI 属性设置最终如下所示:
mongodb://username:mypasswd@hostname1:27017,hostname2:27017,hostname3:27017/dbname
The docs for formatting your URI value are here.
用于格式化 URI 值的文档在这里。
回答by jmojico
Change application.properties from this:
从此更改 application.properties:
spring.data.mongodb.host=server1
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=system
spring.data.mongodb.database=database
...to this:
...到这个:
spring.data.mongodb.uri=mongodb://username:password@server1:port,server2:port/database