本文介绍了在Spring Boot中设置Mongo超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot Web应用程序,该应用程序连接到开箱即用的mongo db.我只使用以下属性:

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

MongoDB的默认超时为10秒.我想配置超时.我试着做 spring.data.mongodb.socketTimeout=2spring.data.mongodb.connectionTimeout=2

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

所有属性均无效.我可以在属性中指定它吗,Spring框架会处理它,还是有人可以通过声明Bean来举例说明.

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.

推荐答案

这将覆盖Spring Boot自动配置:

This will override the Spring Boot autoconfiguration:

@Configuration
public class MongoDbSettings {

    @Bean
    public MongoClientOptions mongoOptions() {
        return MongoClientOptions.builder().socketTimeout(2000).build();
    }

}

这篇关于在Spring Boot中设置Mongo超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:46