Skip to content

Commit fd05bc2

Browse files
committed
Expose MongoDB's replica set name
Closes gh-20391
1 parent 722d374 commit fd05bc2

File tree

5 files changed

+52
-25
lines changed

5 files changed

+52
-25
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactorySupport.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ private MongoClientSettings computeClientSettings(MongoClientSettings settings)
6868
applyUuidRepresentation(settingsBuilder);
6969
applyHostAndPort(settingsBuilder);
7070
applyCredentials(settingsBuilder);
71+
applyReplicaSet(settingsBuilder);
7172
customize(settingsBuilder);
7273
return settingsBuilder.build();
7374
}
7475

7576
private void validateConfiguration() {
76-
if (hasCustomAddress() || hasCustomCredentials()) {
77+
if (hasCustomAddress() || hasCustomCredentials() || hasReplicaSet()) {
7778
Assert.state(this.properties.getUri() == null,
78-
"Invalid mongo configuration, either uri or host/port/credentials must be specified");
79+
"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified");
7980
}
8081
}
8182

@@ -109,6 +110,13 @@ private void applyCredentials(Builder builder) {
109110
}
110111
}
111112

113+
private void applyReplicaSet(Builder builder) {
114+
if (hasReplicaSet()) {
115+
builder.applyToClusterSettings(
116+
(cluster) -> cluster.requiredReplicaSetName(this.properties.getReplicaSetName()));
117+
}
118+
}
119+
112120
private void customize(MongoClientSettings.Builder builder) {
113121
for (MongoClientSettingsBuilderCustomizer customizer : this.builderCustomizers) {
114122
customizer.customize(builder);
@@ -137,6 +145,10 @@ private boolean hasCustomCredentials() {
137145
return this.properties.getUsername() != null && this.properties.getPassword() != null;
138146
}
139147

148+
private boolean hasReplicaSet() {
149+
return this.properties.getReplicaSetName() != null;
150+
}
151+
140152
private boolean hasCustomAddress() {
141153
return this.properties.getHost() != null || this.properties.getPort() != null;
142154
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public class MongoProperties {
5959
private Integer port = null;
6060

6161
/**
62-
* Mongo database URI. Cannot be set with host, port and credentials.
62+
* Mongo database URI. Cannot be set with host, port, credentials and replica set
63+
* name.
6364
*/
6465
private String uri;
6566

@@ -81,14 +82,18 @@ public class MongoProperties {
8182
/**
8283
* Login user of the mongo server. Cannot be set with URI.
8384
*/
84-
8585
private String username;
8686

8787
/**
8888
* Login password of the mongo server. Cannot be set with URI.
8989
*/
9090
private char[] password;
9191

92+
/**
93+
* Required replica set name for the cluster. Cannot be set with URI.
94+
*/
95+
private String replicaSetName;
96+
9297
/**
9398
* Fully qualified name of the FieldNamingStrategy to use.
9499
*/
@@ -144,6 +149,14 @@ public void setPassword(char[] password) {
144149
this.password = password;
145150
}
146151

152+
public String getReplicaSetName() {
153+
return this.replicaSetName;
154+
}
155+
156+
public void setReplicaSetName(String replicaSetName) {
157+
this.replicaSetName = replicaSetName;
158+
}
159+
147160
public Class<?> getFieldNamingStrategy() {
148161
return this.fieldNamingStrategy;
149162
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactorySupportTests.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
*/
5252
abstract class MongoClientFactorySupportTests<T> {
5353

54-
private MongoProperties properties = new MongoProperties();
54+
private final MongoProperties properties = new MongoProperties();
5555

56-
private MockEnvironment environment = new MockEnvironment();
56+
private final MockEnvironment environment = new MockEnvironment();
5757

5858
@Test
5959
void canBindCharArrayPassword() {
@@ -128,6 +128,13 @@ void credentialsCanBeCustomized() {
128128
assertMongoCredential(getClientSettings(client).getCredential(), "user", "secret", "test");
129129
}
130130

131+
@Test
132+
void replicaSetCanBeCustomized() {
133+
this.properties.setReplicaSetName("test");
134+
T client = createMongoClient();
135+
assertThat(getClientSettings(client).getClusterSettings().getRequiredReplicaSetName()).isEqualTo("test");
136+
}
137+
131138
@Test
132139
void databaseCanBeCustomized() {
133140
this.properties.setDatabase("foo");
@@ -193,7 +200,15 @@ void uriCannotBeSetWithCredentials() {
193200
this.properties.setUsername("user");
194201
this.properties.setPassword("secret".toCharArray());
195202
assertThatIllegalStateException().isThrownBy(this::createMongoClient).withMessageContaining(
196-
"Invalid mongo configuration, either uri or host/port/credentials must be specified");
203+
"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified");
204+
}
205+
206+
@Test
207+
void uriCannotBeSetWithReplicaSetName() {
208+
this.properties.setUri("mongodb://127.0.0.1:1234/mydb");
209+
this.properties.setReplicaSetName("test");
210+
assertThatIllegalStateException().isThrownBy(this::createMongoClient).withMessageContaining(
211+
"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified");
197212
}
198213

199214
@Test
@@ -202,7 +217,7 @@ void uriCannotBeSetWithHostPort() {
202217
this.properties.setHost("localhost");
203218
this.properties.setPort(4567);
204219
assertThatIllegalStateException().isThrownBy(this::createMongoClient).withMessageContaining(
205-
"Invalid mongo configuration, either uri or host/port/credentials must be specified");
220+
"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified");
206221
}
207222

208223
@Test
@@ -265,6 +280,10 @@ void canBindAutoIndexCreation() {
265280
assertThat(properties.isAutoIndexCreation()).isTrue();
266281
}
267282

283+
private List<ServerAddress> getAllAddresses(T client) {
284+
return getClientSettings(client).getClusterSettings().getHosts();
285+
}
286+
268287
protected T createMongoClient() {
269288
return createMongoClient(this.properties, this.environment, null, null);
270289
}
@@ -283,8 +302,6 @@ protected abstract T createMongoClient(MongoProperties properties, Environment e
283302

284303
protected abstract MongoClientSettings getClientSettings(T client);
285304

286-
protected abstract List<ServerAddress> getAllAddresses(T client);
287-
288305
protected void assertServerAddress(ServerAddress serverAddress, String expectedHost, int expectedPort) {
289306
assertThat(serverAddress.getHost()).isEqualTo(expectedHost);
290307
assertThat(serverAddress.getPort()).isEqualTo(expectedPort);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoClientFactoryTests.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.List;
2020

2121
import com.mongodb.MongoClientSettings;
22-
import com.mongodb.ServerAddress;
2322
import com.mongodb.client.MongoClient;
2423

2524
import org.springframework.core.env.Environment;
@@ -42,11 +41,6 @@ protected MongoClient createMongoClient(MongoProperties properties, Environment
4241
return new MongoClientFactory(properties, environment, customizers).createMongoClient(settings);
4342
}
4443

45-
@Override
46-
protected List<ServerAddress> getAllAddresses(MongoClient client) {
47-
return client.getClusterDescription().getClusterSettings().getHosts();
48-
}
49-
5044
@Override
5145
protected MongoClientSettings getClientSettings(MongoClient client) {
5246
return (MongoClientSettings) ReflectionTestUtils.getField(client, "settings");

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactoryTests.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.util.List;
2020

2121
import com.mongodb.MongoClientSettings;
22-
import com.mongodb.ServerAddress;
23-
import com.mongodb.connection.ClusterSettings;
2422
import com.mongodb.internal.async.client.AsyncMongoClient;
2523
import com.mongodb.reactivestreams.client.MongoClient;
2624

@@ -42,13 +40,6 @@ protected MongoClient createMongoClient(MongoProperties properties, Environment
4240
return new ReactiveMongoClientFactory(properties, environment, customizers).createMongoClient(settings);
4341
}
4442

45-
@Override
46-
protected List<ServerAddress> getAllAddresses(MongoClient client) {
47-
MongoClientSettings settings = getClientSettings(client);
48-
ClusterSettings clusterSettings = settings.getClusterSettings();
49-
return clusterSettings.getHosts();
50-
}
51-
5243
@Override
5344
protected MongoClientSettings getClientSettings(MongoClient client) {
5445
AsyncMongoClient wrapped = (AsyncMongoClient) ReflectionTestUtils.getField(client, "wrapped");

0 commit comments

Comments
 (0)