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
This commit is contained in:
committed by
Phillip Webb
parent
6e1b28e6a2
commit
e0d40009f3
@@ -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<String> additionalHosts;
|
||||
|
||||
public String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
@@ -208,6 +216,14 @@ public class MongoProperties {
|
||||
this.autoIndexCreation = autoIndexCreation;
|
||||
}
|
||||
|
||||
public List<String> getAdditionalHosts() {
|
||||
return this.additionalHosts;
|
||||
}
|
||||
|
||||
public void setAdditionalHosts(List<String> additionalHosts) {
|
||||
this.additionalHosts = additionalHosts;
|
||||
}
|
||||
|
||||
public static class Gridfs {
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<ServerAddress> 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<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) {
|
||||
if (this.properties.getUri() == null && this.properties.getUsername() != null
|
||||
&& this.properties.getPassword() != null) {
|
||||
|
||||
@@ -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<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
|
||||
void credentialsCanBeCustomized() {
|
||||
this.properties.setUsername("user");
|
||||
|
||||
Reference in New Issue
Block a user