Use a random port with embedded Mongo by default
This commit improves the logic of the embedded Mongo support to use a random port if no custom port has been specified. This doesn't change the default if the embedded support isn't active. Closes gh-8044
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -106,8 +106,9 @@ public class EmbeddedMongoAutoConfiguration {
|
||||
@ConditionalOnMissingBean
|
||||
public MongodExecutable embeddedMongoServer(IMongodConfig mongodConfig)
|
||||
throws IOException {
|
||||
if (getPort() == 0) {
|
||||
publishPortInfo(mongodConfig.net().getPort());
|
||||
Integer configuredPort = this.properties.getPort();
|
||||
if (configuredPort == null || configuredPort == 0) {
|
||||
setEmbeddedPort(mongodConfig.net().getPort());
|
||||
}
|
||||
MongodStarter mongodStarter = getMongodStarter(this.runtimeConfig);
|
||||
return mongodStarter.prepare(mongodConfig);
|
||||
@@ -136,8 +137,9 @@ public class EmbeddedMongoAutoConfiguration {
|
||||
? this.embeddedProperties.getStorage().getOplogSize()
|
||||
: 0));
|
||||
}
|
||||
if (getPort() > 0) {
|
||||
builder.net(new Net(getHost().getHostAddress(), getPort(),
|
||||
Integer configuredPort = this.properties.getPort();
|
||||
if (configuredPort != null && configuredPort > 0) {
|
||||
builder.net(new Net(getHost().getHostAddress(), configuredPort,
|
||||
Network.localhostIsIPv6()));
|
||||
}
|
||||
else {
|
||||
@@ -147,13 +149,6 @@ public class EmbeddedMongoAutoConfiguration {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private int getPort() {
|
||||
if (this.properties.getPort() == null) {
|
||||
return MongoProperties.DEFAULT_PORT;
|
||||
}
|
||||
return this.properties.getPort();
|
||||
}
|
||||
|
||||
private InetAddress getHost() throws UnknownHostException {
|
||||
if (this.properties.getHost() == null) {
|
||||
return InetAddress.getByAddress(Network.localhostIsIPv6()
|
||||
@@ -162,7 +157,8 @@ public class EmbeddedMongoAutoConfiguration {
|
||||
return InetAddress.getByName(this.properties.getHost());
|
||||
}
|
||||
|
||||
private void publishPortInfo(int port) {
|
||||
private void setEmbeddedPort(int port) {
|
||||
this.properties.setPort(port);
|
||||
setPortProperty(this.context, port);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -47,6 +47,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Henryk Konsek
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class EmbeddedMongoAutoConfigurationTests {
|
||||
|
||||
@@ -71,28 +72,38 @@ public class EmbeddedMongoAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void customFeatures() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
int mongoPort = SocketUtils.findAvailableTcpPort();
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.data.mongodb.port=" + mongoPort,
|
||||
"spring.mongodb.embedded.features=TEXT_SEARCH, SYNC_DELAY");
|
||||
this.context.register(EmbeddedMongoAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
load("spring.mongodb.embedded.features=TEXT_SEARCH, SYNC_DELAY");
|
||||
assertThat(this.context.getBean(EmbeddedMongoProperties.class).getFeatures())
|
||||
.contains(Feature.TEXT_SEARCH, Feature.SYNC_DELAY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useRandomPortByDefault() {
|
||||
load();
|
||||
assertThat(this.context.getBeansOfType(MongoClient.class)).hasSize(1);
|
||||
MongoClient client = this.context.getBean(MongoClient.class);
|
||||
Integer mongoPort = Integer.valueOf(
|
||||
this.context.getEnvironment().getProperty("local.mongo.port"));
|
||||
assertThat(client.getAddress().getPort()).isEqualTo(mongoPort);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void specifyPortToZeroAllocateRandomPort() {
|
||||
load("spring.data.mongodb.port=0");
|
||||
assertThat(this.context.getBeansOfType(MongoClient.class)).hasSize(1);
|
||||
MongoClient client = this.context.getBean(MongoClient.class);
|
||||
Integer mongoPort = Integer.valueOf(
|
||||
this.context.getEnvironment().getProperty("local.mongo.port"));
|
||||
assertThat(client.getAddress().getPort()).isEqualTo(mongoPort);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomlyAllocatedPortIsAvailableWhenCreatingMongoClient() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0");
|
||||
this.context.register(EmbeddedMongoAutoConfiguration.class,
|
||||
MongoClientConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(MongoClient.class).getAddress().getPort())
|
||||
.isEqualTo(Integer.valueOf(
|
||||
this.context.getEnvironment().getProperty("local.mongo.port")));
|
||||
load(MongoClientConfiguration.class);
|
||||
MongoClient client = this.context.getBean(MongoClient.class);
|
||||
Integer mongoPort = Integer.valueOf(
|
||||
this.context.getEnvironment().getProperty("local.mongo.port"));
|
||||
assertThat(client.getAddress().getPort()).isEqualTo(mongoPort);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -102,11 +113,8 @@ public class EmbeddedMongoAutoConfigurationTests {
|
||||
try {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.setParent(parent);
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.data.mongodb.port=0");
|
||||
this.context.register(EmbeddedMongoAutoConfiguration.class,
|
||||
MongoClientConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
MongoClientConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(parent.getEnvironment().getProperty("local.mongo.port"))
|
||||
.isNotNull();
|
||||
@@ -118,12 +126,7 @@ public class EmbeddedMongoAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void defaultStorageConfiguration() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0");
|
||||
this.context.register(EmbeddedMongoAutoConfiguration.class,
|
||||
MongoClientConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
load(MongoClientConfiguration.class);
|
||||
Storage replication = this.context.getBean(IMongodConfig.class).replication();
|
||||
assertThat(replication.getOplogSize()).isEqualTo(0);
|
||||
assertThat(replication.getDatabaseDir()).isNull();
|
||||
@@ -134,40 +137,22 @@ public class EmbeddedMongoAutoConfigurationTests {
|
||||
public void mongoWritesToCustomDatabaseDir() {
|
||||
File customDatabaseDir = new File("target/custom-database-dir");
|
||||
FileSystemUtils.deleteRecursively(customDatabaseDir);
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0",
|
||||
"spring.mongodb.embedded.storage.databaseDir="
|
||||
+ customDatabaseDir.getPath());
|
||||
this.context.register(EmbeddedMongoAutoConfiguration.class,
|
||||
MongoClientConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
load("spring.mongodb.embedded.storage.databaseDir="
|
||||
+ customDatabaseDir.getPath());
|
||||
assertThat(customDatabaseDir).isDirectory();
|
||||
assertThat(customDatabaseDir.listFiles()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customOpLogSizeIsAppliedToConfiguration() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0",
|
||||
"spring.mongodb.embedded.storage.oplogSize=10");
|
||||
this.context.register(EmbeddedMongoAutoConfiguration.class,
|
||||
MongoClientConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
load("spring.mongodb.embedded.storage.oplogSize=10");
|
||||
assertThat(this.context.getBean(IMongodConfig.class).replication().getOplogSize())
|
||||
.isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customReplicaSetNameIsAppliedToConfiguration() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=0",
|
||||
"spring.mongodb.embedded.storage.replSetName=testing");
|
||||
this.context.register(EmbeddedMongoAutoConfiguration.class,
|
||||
MongoClientConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
load("spring.mongodb.embedded.storage.replSetName=testing");
|
||||
assertThat(
|
||||
this.context.getBean(IMongodConfig.class).replication().getReplSetName())
|
||||
.isEqualTo("testing");
|
||||
@@ -192,6 +177,23 @@ public class EmbeddedMongoAutoConfigurationTests {
|
||||
assertThat(buildInfo.getString("version")).isEqualTo(expectedVersion);
|
||||
}
|
||||
|
||||
private void load(String... environment) {
|
||||
load(null, environment);
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
if (config != null) {
|
||||
ctx.register(config);
|
||||
}
|
||||
EnvironmentTestUtils.addEnvironment(ctx, environment);
|
||||
ctx.register(EmbeddedMongoAutoConfiguration.class,
|
||||
MongoAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class MongoClientConfiguration {
|
||||
|
||||
|
||||
@@ -3446,6 +3446,9 @@ property. To use a randomly allocated free port use a value of zero. The `MongoC
|
||||
created by `MongoAutoConfiguration` will be automatically configured to use the randomly
|
||||
allocated port.
|
||||
|
||||
NOTE: If you do not configure a custom port, the embedded support will use a random port
|
||||
by default (rather than 27017).
|
||||
|
||||
If you have SLF4J on the classpath, output produced by Mongo will be automatically routed
|
||||
to a logger named `org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo`.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user