-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Description
Hi Team,
We are using spring boot version 2.7.1 and java version 11 and MongoDB Atlas 5 version..
Now, normally when a spring/boot application starts up, it tries to connect to dependent resources. in our case MongoDB Atlas, and if
the credentials are wrong then the startup fails. This is expected behavior.
But in our case for the given spring boot version the startup does not fails if the creds are wrong. In logs (root level info mode) it shows successfully connected to given Mongodb Atlas server and even listed down the different host information in our sharded cluster.
But when you hit the health endpoint, it will show application status as down.
Note, we have defined a custom MongoClient as the the uri for Managed MongoDB (Atlas) start as mongodb+srv://
@Bean
public MongoClient mongoClient() {
return MongoClients.create(mongoClientSettings());
}
private MongoClientSettings mongoClientSettings() {
return MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(generateConnectionURI()))
.build();
}
private String generateConnectionURI() {
return "mongodb+srv://"
+ username
+ ":"
+ password
+ "@"
+ hostname
+ "/?retryWrites="
+ true
+ "&w="
+ majority;
}
Regarding sample code,
You can simple replicate this issue by creating a demo project from start.spring.io having dependency of mongo, rest, actuator and creating a custom MongoClient as shown above and provide a valid Mongo Atlas uri and wrong creds.
Same problem is there with other version of Spring boot. You can test it with 2.7.9 version of spring boot as well