diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java index 0386abfdf7d..d8c96fb4382 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java @@ -61,6 +61,13 @@ public class MongoProperties { */ private Integer port = null; + /** + * Additional server hosts. Cannot be set with URI or if 'host' is not specified. + * Additional hosts will use the default mongo port of 27017, if you want to use a + * different port you can use the "host:port" syntax. + */ + private List additionalHosts; + /** * Mongo database URI. Overrides host, port, username, password, and database. */ @@ -108,11 +115,6 @@ public class MongoProperties { */ private Boolean autoIndexCreation; - /** - * List of additional hosts to connect with different hosts in a replica set. - */ - private List additionalHosts; - public String getHost() { return this.host; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizer.java index b24298b158b..df645ae8374 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizer.java @@ -25,6 +25,7 @@ import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import org.springframework.core.Ordered; +import org.springframework.util.CollectionUtils; /** * A {@link MongoClientSettingsBuilderCustomizer} that applies properties from a @@ -64,22 +65,17 @@ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClie if (this.properties.getHost() != null || this.properties.getPort() != null) { String host = getOrDefault(this.properties.getHost(), "localhost"); int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT); - ServerAddress serverAddress = new ServerAddress(host, port); - List serverAddressList = new ArrayList<>(List.of(serverAddress)); - applyAdditionalHosts(serverAddressList); - settings.applyToClusterSettings((cluster) -> cluster.hosts(serverAddressList)); + List serverAddresses = new ArrayList<>(); + serverAddresses.add(new ServerAddress(host, port)); + if (!CollectionUtils.isEmpty(this.properties.getAdditionalHosts())) { + this.properties.getAdditionalHosts().stream().map(ServerAddress::new).forEach(serverAddresses::add); + } + settings.applyToClusterSettings((cluster) -> cluster.hosts(serverAddresses)); return; } settings.applyConnectionString(new ConnectionString(MongoProperties.DEFAULT_URI)); } - private void applyAdditionalHosts(List serverAddressList) { - if (this.properties.getAdditionalHosts() != null && !this.properties.getAdditionalHosts().isEmpty()) { - this.properties.getAdditionalHosts() - .forEach((additionalHost) -> serverAddressList.add(new ServerAddress(additionalHost))); - } - } - private void applyCredentials(MongoClientSettings.Builder builder) { if (this.properties.getUri() == null && this.properties.getUsername() != null && this.properties.getPassword() != null) { diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/nosql.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/nosql.adoc index 0cca88c5828..bd7fc8dd598 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/nosql.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/nosql.adoc @@ -80,7 +80,7 @@ You can set the configprop:spring.data.mongodb.uri[] property to change the URL spring: data: mongodb: - uri: "mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test" + uri: "mongodb://user:secret@mongoserver1.example.com:27017,mongoserver2.example.com:23456/test" ---- Alternatively, you can specify connection details using discrete properties. @@ -91,16 +91,24 @@ For example, you might declare the following settings in your `application.prope spring: data: mongodb: - host: "mongoserver.example.com" + host: "mongoserver1.example.com" port: 27017 + additional-hosts: + - "mongoserver2.example.com:23456" database: "test" username: "user" password: "secret" ---- -TIP: If `spring.data.mongodb.port` is not specified, the default of `27017` is used. +[TIP] +==== +If `spring.data.mongodb.port` is not specified, the default of `27017` is used. You could delete this line from the example shown earlier. +You can also specify the port as part of the host address by using the `host:port` syntax. +This format should be used if you need to change the port of an `additional-hosts` entry. +==== + TIP: If you do not use Spring Data MongoDB, you can inject a `MongoClient` bean instead of using `MongoDatabaseFactory`. If you want to take complete control of establishing the MongoDB connection, you can also declare your own `MongoDatabaseFactory` or `MongoClient` bean.