Merge pull request #32125 from thegeekyasian
* pr/32125: Polish 'Allow multiple hosts to be set in MongoProperties' Allow multiple hosts to be set in MongoProperties Closes gh-32125
This commit is contained in:
commit
5fcc39418c
|
|
@ -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")
|
||||||
|
|
@ -58,6 +61,13 @@ public class MongoProperties {
|
||||||
*/
|
*/
|
||||||
private Integer port = null;
|
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<String> additionalHosts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mongo database URI. Overrides host, port, username, password, and database.
|
* Mongo database URI. Overrides host, port, username, password, and database.
|
||||||
*/
|
*/
|
||||||
|
|
@ -208,6 +218,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;
|
||||||
|
|
@ -24,12 +25,14 @@ import com.mongodb.MongoCredential;
|
||||||
import com.mongodb.ServerAddress;
|
import com.mongodb.ServerAddress;
|
||||||
|
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link MongoClientSettingsBuilderCustomizer} that applies properties from a
|
* A {@link MongoClientSettingsBuilderCustomizer} that applies properties from a
|
||||||
* {@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 {
|
||||||
|
|
@ -62,8 +65,12 @@ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClie
|
||||||
if (this.properties.getHost() != null || this.properties.getPort() != null) {
|
if (this.properties.getHost() != null || this.properties.getPort() != null) {
|
||||||
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);
|
List<ServerAddress> serverAddresses = new ArrayList<>();
|
||||||
settings.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress)));
|
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;
|
return;
|
||||||
}
|
}
|
||||||
settings.applyConnectionString(new ConnectionString(MongoProperties.DEFAULT_URI));
|
settings.applyConnectionString(new ConnectionString(MongoProperties.DEFAULT_URI));
|
||||||
|
|
|
||||||
|
|
@ -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");
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ You can set the configprop:spring.data.mongodb.uri[] property to change the URL
|
||||||
spring:
|
spring:
|
||||||
data:
|
data:
|
||||||
mongodb:
|
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.
|
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:
|
spring:
|
||||||
data:
|
data:
|
||||||
mongodb:
|
mongodb:
|
||||||
host: "mongoserver.example.com"
|
host: "mongoserver1.example.com"
|
||||||
port: 27017
|
port: 27017
|
||||||
|
additional-hosts:
|
||||||
|
- "mongoserver2.example.com:23456"
|
||||||
database: "test"
|
database: "test"
|
||||||
username: "user"
|
username: "user"
|
||||||
password: "secret"
|
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 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`.
|
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.
|
If you want to take complete control of establishing the MongoDB connection, you can also declare your own `MongoDatabaseFactory` or `MongoClient` bean.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue