简体   繁体   中英

Spring Data Mongodb - single configuration for standalone mongo and replicaset

I started out with a single mongo instance as my database, configured in spring xml:

<mongo:mongo host="localhost" port="27017" />

Recently, I changed my configuration to use a 3 node replica set, configured as:

<mongo:mongo replica-set="${my.replica.set}" />

Everything works great.

My current problem is that for my dev environment I'd like to use the single localhost mongo config and for int and prod envrionments I'd like to use the replica set config. The differing values I will handle via properties files. The question is about the mongo config itself.

Something along the lines of the example below would be ideal...

<mongo:mongo uri="localhost:27017" />
<mongo:mongo uri="localhost:27017,localhost:27018" />

I ran into this example: spring-boot uriCanBeCustomized unitTest

Is there a way to do this in spring config?

I am using spring-data-mongodb-1.7.0.RELEASE.

Looks like the replica set configuration works even if you point it to a standalone mongod. I assumed that wouldn't work since it specifically sets 'replica-set' but testing shows that it does.

So in my case, the configuration would just look like

<mongo:mongo replica-set="${mongodbs}" /> 

where in the dev properties file I'd have

mongodbs=localhost:27017 

and for int/prod properties

mongodbs=host1:port1,host2:port2,host3:port3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM