spring boot - 嵌入式mongodb连接被拒绝

时间:2018-03-20 18:11:29

标签: java spring spring-boot spring-mongodb

this example spring boot project中,我想测试MongoRepository持久层以及使用嵌入式mongodb使用存储库的Service类。但是,我收到Connection refused: connect错误(java.net.ConnectException),请参阅下面的详细堆栈跟踪。

这是我的测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes=MongoConfig.class)
@TestPropertySource(locations = "classpath:application-test.properties")
public class Test_TimezoneDao {

    private static final Logger LOG = LoggerFactory.getLogger(Test_TimezoneDao.class);

    @Autowired private TimezoneDao dao;
    @Autowired private CounterService counterService;


    @Test
    public void test() 
    { /*...*/ }
}

使用此配置类

@Profile("test")
@SpringBootConfiguration
@ComponentScan(basePackages= {"com.example.demomongodb"})
@EnableMongoRepositories(basePackages= {"com.example.demomongodb"})
@EnableAutoConfiguration
@AutoConfigureDataMongo
public class MongoConfig 
{ }

对于春天使用我导入的嵌入式mongodb

       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-mongodb</artifactId>
       </dependency>

        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>cz.jirutka.spring</groupId>
            <artifactId>embedmongo-spring</artifactId>
            <version>1.3.1</version>
            <scope>test</scope>
        </dependency>

并在application-test.properties

中配置
spring.profiles.active=test
# tried both random port (port=0) and deterministic port
spring.data.mongodb.port=27019
spring.data.mongodb.host=localhost
spring.data.mongodb.database=mongo-example

完整的错误是:

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.3.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_92]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_92]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_92]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_92]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_92]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_92]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_92]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_92]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_92]
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongodb-driver-core-3.6.3.jar:na]
    ... 3 common frames omitted

感谢您的帮助!

编辑

还尝试从所有应用程序属性中删除mongo主机和端口,同样的问题

编辑2

当我按照spring boot docs中的建议将mongodb端口更改为0时,我得到java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine。所以它真的尝试端口0并且不使用弹簧引导文档中描述的随机端口。这似乎根本不起作用..

1 个答案:

答案 0 :(得分:0)

我认为你需要在mongo config中创建这个bean。

@Bean(destroyMethod="close")
public Mongo mongo() throws IOException {
    return new EmbeddedMongoBuilder()
        .version("2.4.5")
        .bindIp("127.0.0.1")
        .port(12345)
        .build();
}

source