How to configure spring-data-mongodb to use a replica set via properties
Asked Answered
V

4

32

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:

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?

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:

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

I also tried to use 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 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?

Vinegarroon answered 5/8, 2015 at 18:7 Comment(1)
If you are using mongo atlas, this post will be helpful - opencodez.com/java/use-mongodb-atlas-with-spring-boot.htmTeddie
S
23

There is no explicit support for that, no. But you should be able to configure that just fine via the uri parameter.

We've actually updated the documentation recently.

Snowshed answered 6/8, 2015 at 15:25 Comment(2)
Thanks for your answer Stephane. Which version is of Spring Data Mongo is supporting this sort of configuration property?Vinegarroon
Ok, I finally got some time to check this. It seems to work. But the log messages are not very clear about clusters (hopefully I will find some time to investigate and log an issue) Anyway, thanks for your quick help!Vinegarroon
W
23

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.

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.

The URI property setting ended up looking like this:

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

The docs for formatting your URI value are here.

Way answered 7/1, 2016 at 1:28 Comment(4)
Thanks for commenting, I completely forgot to add this. Indeed, if any of the mentioned values is set, the URI is ignored.Vinegarroon
can you please tell where to add authentication database name in here?Mccrae
when connecting to a replica set reads by default will be done from primary node unless explicitly specified to read from secondary right?Decanal
does the replset option flag has to be specified?. What happens if not specified?Decanal
P
9

Change 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 this:

spring.data.mongodb.uri=mongodb://username:password@server1:port,server2:port/database
Pettigrew answered 14/1, 2019 at 0:31 Comment(0)
P
0

additional-hosts property added since spring boot 3.0.0-M5 as per this github issue.

spring:
  data:
    mongodb:
      host: 0.0.0.1
      port: 27017
      additional-hosts: [0.0.0.2:12345, 0.0.0.3]

See description in spring boot common app properties.

Phenomena answered 12/7 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.