When using ReplicaSet, access to the Mongo cluster by multiple hosts. i'd like to write below property.
spring:
data:
mongodb:
host: 0.0.0.1,0.0.0.1,0.0.0.1
port: 27017
username: username
password: '{cipher}encrypted_password'
database: testdb
authentication-database: admin
BUT it is not valid because AutoConfigure does not support list of host. it support only single instance. https://github.com/spring-projects/spring-boot/blob/c41609d01d813a765cf04ef06ae267[…]igure/mongo/MongoPropertiesClientSettingsBuilderCustomizer.java
i know below property.
spring:
data:
mongodb:
uri: "mongodb://username:plain_password@0.0.0.1,0.0.0.1,0.0.0.1:27017/testdb?authSource=admin"
it is workaround.
We need more various setting.
Comment From: philwebb
It looks like ClusterSettings can accept a collection of com.mongodb.ServerAddress instance, but those are host/port pairs. I don't think we can simply convert host to be a list because the related port can't be configured.
Perhaps we could add something like:
spring:
data:
mongodb:
host: 0.0.0.1
port: 27017
additional-hosts:
- host: 0.0.0.2
port: 27017
- host: 0.0.0.3
port: 27017
username: username
password: '{cipher}encrypted_password'
database: testdb
authentication-database: admin
Either that or consider changing the existing properties to:
spring:
data:
mongodb:
hosts:
- host: 0.0.0.1
- port: 27017
- host: 0.0.0.2
port: 12345
- host: 0.0.0.3
port: 27017
username: username
password: '{cipher}encrypted_password'
database: testdb
authentication-database: admin
That unfortunately makes the 90% use-case of a single host a little harder to configure. It will also cause upgrade pain.
Rather confusingly, the ServerAddress class also lets you specify the port as part of the host string as long as the default port is being used. In other words, host: 0.0.0.2:12345 would be get parsed as long as port: was missing or set to 27017. We might be able to use that to simplify things to:
spring:
data:
mongodb:
host: 0.0.0.1
port: 27017
additional-hosts: [0.0.0.2:12345, 0.0.0.3]
username: username
password: '{cipher}encrypted_password'
database: testdb
authentication-database: admin
Comment From: hongjunan
@philwebb , much better!! your solution regarding compatibility is great . thank you.
Comment From: philwebb
Closing in favor of PR #32125. Thanks @thegeekyasian