Skip to content

Allow multiple hosts to be set in MongoProperties #32113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hongjunan opened this issue Aug 18, 2022 · 3 comments
Closed

Allow multiple hosts to be set in MongoProperties #32113

hongjunan opened this issue Aug 18, 2022 · 3 comments
Labels
status: superseded An issue that has been superseded by another

Comments

@hongjunan
Copy link

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:[email protected],0.0.0.1,0.0.0.1:27017/testdb?authSource=admin"

it is workaround.

We need more various setting.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 18, 2022
@philwebb
Copy link
Member

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  

@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 18, 2022
@philwebb philwebb added this to the General Backlog milestone Aug 18, 2022
@philwebb philwebb changed the title Need to set by host list in org.springframework.boot.autoconfigure.mongo.MongoProperties Allow multiple hosts to be set in MongoProperties Aug 18, 2022
@hongjunan
Copy link
Author

@philwebb , much better!!
your solution regarding compatibility is great .
thank you.

@philwebb philwebb added the status: ideal-for-contribution An issue that a contributor can help us with label Aug 19, 2022
thegeekyasian added a commit to thegeekyasian/spring-boot that referenced this issue Aug 21, 2022
Currently MongoProperties allow only single host to be set in the MongoProperties. 
With this commit a set of 'additional hosts' can be set along with the base host/port.

This closes spring-projects#32113
@philwebb philwebb added status: superseded An issue that has been superseded by another and removed type: enhancement A general enhancement status: ideal-for-contribution An issue that a contributor can help us with labels Aug 24, 2022
@philwebb philwebb removed this from the General Backlog milestone Aug 24, 2022
@philwebb
Copy link
Member

Closing in favor of PR #32125. Thanks @thegeekyasian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants