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 3a8d6511c1..0386abfdf7 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 @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.mongo; +import java.util.List; + import com.mongodb.ConnectionString; import org.bson.UuidRepresentation; @@ -33,6 +35,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * @author Nasko Vasilev * @author Mark Paluch * @author Artsiom Yudovin + * @author Safeer Ansari * @since 1.0.0 */ @ConfigurationProperties(prefix = "spring.data.mongodb") @@ -105,6 +108,11 @@ 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; } @@ -208,6 +216,14 @@ public class MongoProperties { this.autoIndexCreation = autoIndexCreation; } + public List getAdditionalHosts() { + return this.additionalHosts; + } + + public void setAdditionalHosts(List additionalHosts) { + this.additionalHosts = additionalHosts; + } + public static class Gridfs { /** 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 4f12bbfcb9..b24298b158 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 @@ -16,7 +16,8 @@ package org.springframework.boot.autoconfigure.mongo; -import java.util.Collections; +import java.util.ArrayList; +import java.util.List; import com.mongodb.ConnectionString; import com.mongodb.MongoClientSettings; @@ -30,6 +31,7 @@ import org.springframework.core.Ordered; * {@link MongoProperties} to a {@link MongoClientSettings}. * * @author Scott Frederick + * @author Safeer Ansari * @since 2.4.0 */ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClientSettingsBuilderCustomizer, Ordered { @@ -63,12 +65,21 @@ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClie String host = getOrDefault(this.properties.getHost(), "localhost"); int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT); ServerAddress serverAddress = new ServerAddress(host, port); - settings.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress))); + List serverAddressList = new ArrayList<>(List.of(serverAddress)); + applyAdditionalHosts(serverAddressList); + settings.applyToClusterSettings((cluster) -> cluster.hosts(serverAddressList)); 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-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizerTests.java index 71e1c3c533..9e7199ae1e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizerTests.java @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.mongo; +import java.util.Arrays; import java.util.List; import com.mongodb.MongoClientSettings; @@ -53,6 +54,18 @@ class MongoPropertiesClientSettingsBuilderCustomizerTests { assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017); } + @Test + void additionalHostCanBeAdded() { + this.properties.setHost("mongo.example.com"); + this.properties.setAdditionalHosts(Arrays.asList("mongo.example.com:33", "mongo.example2.com")); + MongoClientSettings settings = customizeSettings(); + List allAddresses = getAllAddresses(settings); + assertThat(allAddresses).hasSize(3); + assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017); + assertServerAddress(allAddresses.get(1), "mongo.example.com", 33); + assertServerAddress(allAddresses.get(2), "mongo.example2.com", 27017); + } + @Test void credentialsCanBeCustomized() { this.properties.setUsername("user");