首页 > 解决方案 > 连接到服务器 localhost:27017 时,在监视器线程中连接远程 MongoDB 异常时出错

问题描述

我正在尝试连接到远程地图集银行,但看起来我被重定向到本地主机。我像往常一样配置了 spring.data.mongodb.uri,同一个项目几天前还在工作,其他具有相同结构的项目工作正常。使用相同的连接 uri,我可以通过 mongoDB 客户端进行连接。我正在使用 spring boot 2.6,下面是通过 gradle 使用的依赖项

    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
    compile group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.39.1'
    compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.17'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '2.1.3.RELEASE'
    compile group: 'io.github.openfeign.form', name: 'feign-form', version: '3.8.0'
    compile group: 'io.github.openfeign', name: 'feign-jaxb', version: '10.7.4'
    compile ('io.springfox:springfox-swagger2:2.8.0')
    compile ('io.springfox:springfox-swagger-ui:2.8.0')
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.3.1'
    compile group: 'com.github.mpkorstanje', name: 'simmetrics-core', version: '4.1.1'
    compile group: 'org.apache.poi', name: 'poi', version: '4.1.1'
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.1'
    compile group: 'org.apache.poi', name: 'poi-scratchpad', version: '4.1.1'
    compile group: 'org.apache.tika', name: 'tika-parsers', version: '1.23'
    compile group: 'org.apache.tika', name: 'tika-core', version: '1.23'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    testCompile group: 'com.google.caliper', name: 'caliper', version: '0.5-rc1'

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}```

This log error: "Exception in monitor thread while connecting to server localhost:27017"

I saw some comments telling me to disable @EnableAutoConfiguration (exclude = {MongoAutoConfiguration.class}), however, it didn't work, after all I'm trying to connect to a remote mongo and don't disable it.

It seems to me something related to the configuration of routes / dns something like that, I don't know, I'm confused about that. Would any good soul know how to help me?

标签: mongodbspring-boot

解决方案


使用自动配置“ON”,它使用指向 localhost:27017 的默认值。这就是为什么您在 SpringBootApplication/EnableAutoConfiguration 注释中看到注释告诉排除 MongoAutoConfiguration 和 MongoDataAutoConfiguration 的原因。

我建议您以编程方式(而不是自动)配置 mongodb 并覆盖这些道具。

spring.data.mongodb.host=mongoserver.example.com
spring.data.mongodb.port=27017
spring.data.mongodb.database=test
spring.data.mongodb.username=user
spring.data.mongodb.password=secret

推荐阅读