Allow multiple hosts to be set in MongoProperties

Update `MongoProperties` with an `additional-hosts` property which
can be used to configure MongoDB in a multi-host environment.

See gh-32125
pull/32162/head
thegeekyasian 2 years ago committed by Phillip Webb
parent 6e1b28e6a2
commit e0d40009f3

@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.mongo; package org.springframework.boot.autoconfigure.mongo;
import java.util.List;
import com.mongodb.ConnectionString; import com.mongodb.ConnectionString;
import org.bson.UuidRepresentation; import org.bson.UuidRepresentation;
@ -33,6 +35,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* @author Nasko Vasilev * @author Nasko Vasilev
* @author Mark Paluch * @author Mark Paluch
* @author Artsiom Yudovin * @author Artsiom Yudovin
* @author Safeer Ansari
* @since 1.0.0 * @since 1.0.0
*/ */
@ConfigurationProperties(prefix = "spring.data.mongodb") @ConfigurationProperties(prefix = "spring.data.mongodb")
@ -105,6 +108,11 @@ public class MongoProperties {
*/ */
private Boolean autoIndexCreation; private Boolean autoIndexCreation;
/**
* List of additional hosts to connect with different hosts in a replica set.
*/
private List<String> additionalHosts;
public String getHost() { public String getHost() {
return this.host; return this.host;
} }
@ -208,6 +216,14 @@ public class MongoProperties {
this.autoIndexCreation = autoIndexCreation; this.autoIndexCreation = autoIndexCreation;
} }
public List<String> getAdditionalHosts() {
return this.additionalHosts;
}
public void setAdditionalHosts(List<String> additionalHosts) {
this.additionalHosts = additionalHosts;
}
public static class Gridfs { public static class Gridfs {
/** /**

@ -16,7 +16,8 @@
package org.springframework.boot.autoconfigure.mongo; 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.ConnectionString;
import com.mongodb.MongoClientSettings; import com.mongodb.MongoClientSettings;
@ -30,6 +31,7 @@ import org.springframework.core.Ordered;
* {@link MongoProperties} to a {@link MongoClientSettings}. * {@link MongoProperties} to a {@link MongoClientSettings}.
* *
* @author Scott Frederick * @author Scott Frederick
* @author Safeer Ansari
* @since 2.4.0 * @since 2.4.0
*/ */
public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClientSettingsBuilderCustomizer, Ordered { public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClientSettingsBuilderCustomizer, Ordered {
@ -63,12 +65,21 @@ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClie
String host = getOrDefault(this.properties.getHost(), "localhost"); String host = getOrDefault(this.properties.getHost(), "localhost");
int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT); int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT);
ServerAddress serverAddress = new ServerAddress(host, port); ServerAddress serverAddress = new ServerAddress(host, port);
settings.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress))); List<ServerAddress> serverAddressList = new ArrayList<>(List.of(serverAddress));
applyAdditionalHosts(serverAddressList);
settings.applyToClusterSettings((cluster) -> cluster.hosts(serverAddressList));
return; return;
} }
settings.applyConnectionString(new ConnectionString(MongoProperties.DEFAULT_URI)); settings.applyConnectionString(new ConnectionString(MongoProperties.DEFAULT_URI));
} }
private void applyAdditionalHosts(List<ServerAddress> 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) { private void applyCredentials(MongoClientSettings.Builder builder) {
if (this.properties.getUri() == null && this.properties.getUsername() != null if (this.properties.getUri() == null && this.properties.getUsername() != null
&& this.properties.getPassword() != null) { && this.properties.getPassword() != null) {

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.mongo; package org.springframework.boot.autoconfigure.mongo;
import java.util.Arrays;
import java.util.List; import java.util.List;
import com.mongodb.MongoClientSettings; import com.mongodb.MongoClientSettings;
@ -53,6 +54,18 @@ class MongoPropertiesClientSettingsBuilderCustomizerTests {
assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017); 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<ServerAddress> 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 @Test
void credentialsCanBeCustomized() { void credentialsCanBeCustomized() {
this.properties.setUsername("user"); this.properties.setUsername("user");

Loading…
Cancel
Save