I am using spring boot web application which connects to mongo db which is working out of the box. I just use the following properties:
spring.data.mongodb.host=myHost
spring.data.mongodb.port=27017
spring.data.mongodb.database=myDatabase
spring.data.mongo.repositories.enabled=true
spring.data.mongodb.username=myUser
spring.data.mongodb.password=myPassword
The default timeout to MongoDB is 10 seconds. I would like to configure the timeout. I tried doing
spring.data.mongodb.socketTimeout=2 or spring.data.mongodb.connectionTimeout=2
None of the properties work. Is it something that I can specify in the properties and the Spring framework will take care of it or can someone give example of doing it by declaring the Bean.
解决方案
This will override the Spring Boot autoconfiguration:
@Configuration
public class MongoDbSettings {
@Bean
public MongoClientOptions mongoOptions() {
return MongoClientOptions.builder().socketTimeout(2000).build();
}
}