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 3a8d6511c1de..399e330997ba 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 @@ -33,6 +33,7 @@ * @author Nasko Vasilev * @author Mark Paluch * @author Artsiom Yudovin + * @author Safeer Ansari * @since 1.0.0 */ @ConfigurationProperties(prefix = "spring.data.mongodb") @@ -105,6 +106,8 @@ public class MongoProperties { */ private Boolean autoIndexCreation; + private String additionalHosts; + public String getHost() { return this.host; } @@ -208,6 +211,14 @@ public void setAutoIndexCreation(Boolean autoIndexCreation) { this.autoIndexCreation = autoIndexCreation; } + public String getAdditionalHosts() { + return additionalHosts; + } + + public void setAdditionalHosts(String 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 4f12bbfcb9a3..e2073a73f9f3 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,11 @@ package org.springframework.boot.autoconfigure.mongo; -import java.util.Collections; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.mongodb.ConnectionString; import com.mongodb.MongoClientSettings; @@ -30,12 +34,16 @@ * {@link MongoProperties} to a {@link MongoClientSettings}. * * @author Scott Frederick + * @author Safeer Ansari * @since 2.4.0 */ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClientSettingsBuilderCustomizer, Ordered { private final MongoProperties properties; + private static final Pattern ADDITIONAL_HOSTS_PATTERN = Pattern + .compile("^\\[[0-9a-zA-Z]+(.)*(:)*(,[0-9a-zA-Z]+(.)*(:)*)*]$"); + private int order = 0; public MongoPropertiesClientSettingsBuilderCustomizer(MongoProperties properties) { @@ -63,12 +71,28 @@ private void applyHostAndPort(MongoClientSettings.Builder settings) { 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()) { + Matcher matcher = ADDITIONAL_HOSTS_PATTERN.matcher((this.properties.getAdditionalHosts())); + if (!matcher.matches()) { + return; + } + + String[] additionalHosts = this.properties.getAdditionalHosts() + .substring(1, this.properties.getAdditionalHosts().length() - 1).split(","); + Arrays.stream(additionalHosts) + .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 71e1c3c533dd..94d6eb159287 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 @@ -53,6 +53,18 @@ void hostCanBeCustomized() { assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017); } + @Test + void additionalHostCanBeAdded() { + this.properties.setHost("mongo.example.com"); + this.properties.setAdditionalHosts("[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");