How to configure spring-data-mongodb to use replica set through properties

I am currently writing an application that should use the MongoDB replica set. This is a Spring Boot application, and the following properties work great for connecting to a single server:

spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=demo 

This is absolutely normal for my local environment. But later it should work against the MongoDB replica set, so I have to provide at least 2, better than 3 replica sample sets, but how to do it with the properties?

I looked at this page: http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html , but there is no explicit property for the mentioned replica sets. Providing a comma separated list of addresses:

 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 by one.)

This also does not work (in fact, it throws an exception that allows Spring to use the default configuration).

I also tried using the following config.xml, with no luck:

 <?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 configs above are slightly different, but what I'm trying to do now is to get an exception that shows me that the node replica set is not available.

Any ideas, tips?

+14
java spring-boot spring-data-mongodb mongodb
source share
3 answers

There is no clear support for this, no. But you should be able to configure this simply with the uri parameter.

We recently updated the documentation .

+13
source share

I had a similar problem and I MongoProperties::createMongoClient() in MongoProperties::createMongoClient() and found that the code ignored 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 .

If I put all this information in a URI (and deleted all the other spring.data.mongodb.* from the properties file), the connection code worked.

Setting the URI property in the end looked like this:

 mongodb://username: mypasswd@hostname1 :27017,hostname2:27017,hostname3:27017/dbname 

The documents for formatting your URI value are here .

+11
source share

Modify application.properties from this:

 spring.data.mongodb.host=server1 spring.data.mongodb.port=27017 spring.data.mongodb.authentication-database=system spring.data.mongodb.database=database 

...to that:

 spring.data.mongodb.uri=mongodb://username: password@server1 :port,server2:port/database 
0
source share

All Articles