简体   繁体   English

Spring Boot Starter-Web尝试在启动时连接到Mongo

[英]Spring Boot Starter-Web tries to connect to Mongo at startup

I am experiencing some problems using Spring Boot and MongoDB external driver. 我在使用Spring Boot和MongoDB外部驱动程序时遇到了一些问题。 I can't use the project Spring Data MongoDB, because I need to use the ufficial async driver given by Mongo. 我不能使用Spring Data MongoDB项目,因为我需要使用Mongo给出的ufficial异步驱动程序。 However, I need to use Spring Boot, because the module I am developing is part of a bigger project using this library. 但是,我需要使用Spring Boot,因为我正在开发的模块是使用该库的更大项目的一部分。

Here is my pom.xml file. 这是我的pom.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>tx-view</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- omissis -->

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-async</artifactId>
            <version>${mongodb.version}</version>
        </dependency>
     </dependencies>

    <build>
        <plugins>
            <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

In detail, I am using Spring Boot 1.4.1 and Mongo Async Driver 3.2.2. 详细地说,我使用的是Spring Boot 1.4.1和Mongo Async Driver 3.2.2。

Here is my application. 这是我的申请。

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
public class TxViewApplication {
    public static void main(String[] args) {
        SpringApplication.run(TxViewApplication.class, args);
    }

    @Value("${mongo.uri}")
    private String mongoUri;

    @Bean
    public MongoClient mongoClient() {
        return MongoClients.create(mongoUri);
    }
}

It follows the only (empty) test I have at the moment. 它遵循我目前唯一的(空)测试。

@SpringBootTest
@RunWith(SpringRunner.class)
public class ApplicationTest {
    @Test
    public void loadContext() throws Exception {}
}

I have no other code in this project. 我在这个项目中没有其他代码 When I run the test, I have the following error: 当我运行测试时,我有以下错误:

2016-11-22 15:43:58.597  INFO 4572 --- [null'}-db:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server db:27017

com.mongodb.MongoException: java.io.IOException: Il computer remoto ha rifiutato la connessione di rete.

at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:125) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128) ~[mongodb-driver-core-3.2.2.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]
Caused by: java.io.IOException: Il computer remoto ha rifiutato la connessione di rete.

at sun.nio.ch.Iocp.translateErrorToIOException(Iocp.java:309) ~[na:1.8.0_101]
at sun.nio.ch.Iocp.access$700(Iocp.java:46) ~[na:1.8.0_101]
at sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:399) ~[na:1.8.0_101]
at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) ~[na:1.8.0_101]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_101]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_101]
... 1 common frames omitted

As you can see, I have properly inserted the exclude clause in the SpringBootApplication annotation in order to stop Spring Boot to try handle its own the connection to Mongo, as suggested in Mongo tries to connect automatically to port 27017(localhost) . 正如您所看到的,我已经在SpringBootApplication注释中正确插入了exclude子句,以便阻止Spring Boot尝试处理自己与Mongo的连接,正如Mongo中建议的那样, 尝试自动连接到端口27017(localhost)

I have also noticed that I started to have the error after the addition to the pom.xml of the dependency to spring-boot-starter-web . 我还注意到,在添加到spring-boot-starter-web的依赖项的pom.xml之后,我开始出现错误。

How can I inhibit Spring Boot to try to connect automatically to Mongo at startup? 如何禁止Spring Boot尝试在启动时自动连接到Mongo? The same problem is present with the synchronous version of MongoDB driver. MongoDB驱动程序的同步版本存在同样的问题。

--- EDIT --- ---编辑---

I have also try to build a wrapper around the async.MongoClient object, in this way: 我还尝试以这种方式围绕async.MongoClient对象构建一个包装器:

public class MongoWrapper {
    private final MongoClient mongo;

    public MongoWrapper() {
        mongo = MongoClients.create();
    }

    public MongoClient getMongo() {
        return mongo;
    }
}

The configuration was changed accordingly. 配置已相应更改。

@Bean
public MongoWrapper mongo() {
    return new MongoWrapper();
}

Unfortunately, nothing had changed. 不幸的是,没有任何改变。 Spring Boot seems to intercept the MongoClient object also in this way :( Spring Boot似乎也以这种方式拦截MongoClient对象:(

Thanks a lot. 非常感谢。

You have a MongoClient bean in your own configuration which does not make any sense to me if you've excluded the auto-configuration. 你有自己配置的MongoClient bean,如果你已经排除了自动配置,那对我没有任何意义。

I've commented out the @Bean definition in your own config and no attempt to connect to Mongo is performed now. 我已经在你自己的配置中注释掉了@Bean定义,现在没有尝试连接到Mongo。 I am not sure I answer to your question and you're probably looking for something else but if you don't want to use mongo, don't define a MongoClient in your own config! 我不确定我是否回答了你的问题而且你可能正在寻找其他东西,但如果你不想使用mongo,不要在你自己的配置中定义MongoClient

这有助于我们禁用MongoDB的异步java驱动程序以使用默认配置:

@EnableAutoConfiguration(exclude = {MongoReactiveAutoConfiguration.class})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM