Spring Boot Starter-Web尝试在启动时连接到Mongo [英] Spring Boot Starter-Web tries to connect to Mongo at startup

查看:154
本文介绍了Spring Boot Starter-Web尝试在启动时连接到Mongo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Spring Boot和MongoDB外部驱动程序时遇到了一些问题。我不能使用项目Spring Data MongoDB,因为我需要使用Mongo给出的ufficial异步驱动程序。但是,我需要使用Spring Boot,因为我正在开发的模块是使用这个库的更大项目的一部分。

I am experiencing some problems using Spring Boot and MongoDB external driver. I can't use the project Spring Data MongoDB, because I need to use the ufficial async driver given by Mongo. However, I need to use Spring Boot, because the module I am developing is part of a bigger project using this library.

这是我的 pom .xml file。

<?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>

详细说明,我使用的是Spring Boot 1.4.1和Mongo Async Driver 3.2.2。

In detail, I am using Spring Boot 1.4.1 and Mongo Async Driver 3.2.2.

这是我的申请。

@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

如您所见,我已正确插入 exclude 子句 SpringBootApplication 注释,以阻止Spring Boot尝试处理自己与Mongo的连接,如 Mongo尝试自动连接到端口27017(lo calhost)

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).

我还注意到在添加到 pom.xml后我开始出错了依赖于 spring-boot-starter-web

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尝试在启动时自动连接到Mongo?同步版本的MongoDB驱动程序也存在同样的问题。

How can I inhibit Spring Boot to try to connect automatically to Mongo at startup? The same problem is present with the synchronous version of MongoDB driver.

---编辑---

我还尝试以这种方式围绕 async.MongoClient 对象构建一个包装器:

I have also try to build a wrapper around the async.MongoClient object, in this way:

public class MongoWrapper {
    private final MongoClient mongo;

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

    public MongoClient getMongo() {
        return mongo;
    }
}

配置已相应更改。

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

不幸的是,没有任何改变。 Spring Boot似乎也以这种方式拦截了 MongoClient 对象:(

Unfortunately, nothing had changed. Spring Boot seems to intercept the MongoClient object also in this way :(

非常感谢。

推荐答案

你自己的配置中有一个 MongoClient bean,这对你没有任何意义我,如果你已经排除了自动配置。

You have a MongoClient bean in your own configuration which does not make any sense to me if you've excluded the auto-configuration.

我已经注释掉你的 @Bean 定义了现在已经执行了自己的配置,并且没有尝试连接到Mongo。我不确定我是否回答了你的问题,你可能正在寻找其他的东西,但如果你不想使用mongo,请不要定义<$ c您自己配置中的$ c> MongoClient !

I've commented out the @Bean definition in your own config and no attempt to connect to Mongo is performed now. 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!

这篇关于Spring Boot Starter-Web尝试在启动时连接到Mongo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆