Spring Boot 2配置反应式Mongo数据库存储库而无需EmbeddedMongo

时间:2018-11-27 17:02:37

标签: spring mongodb spring-boot

我正在尝试使用Mongo DB建立一个Spring Boot 2项目,但我面临的问题是,即使我已配置为使用外部mongo,它仍在不断旋转嵌入式mongo DB

日志

2018-11-27 18:56:05.725  INFO 73687 --- [       Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo     : note: noprealloc may hurt performance in many applications
2018-11-27 18:56:05.742  INFO 73687 --- [       Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-11-27T18:56:05.741+0200 I CONTROL  [initandlisten] MongoDB starting : pid=73690 port=50303 dbpath=/var/folders/pf/qp_pv2xn7xb7ysnltp6tc97cstys_v/T/embedmongo-db-c2738896-198f-4934-87c4-3bd773508af7 64-bit host=MBP15-N5AXG8WP
2018-11-27 18:56:05.742  INFO 73687 --- [       Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-11-27T18:56:05.741+0200 I CONTROL  [initandlisten] db version v3.5.5
2018-11-27 18:56:05.742  INFO 73687 --- [       Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-11-27T18:56:05.741+0200 I CONTROL  [initandlisten] git version: 98515c812b6fa893613f063dae568ff8319cbfbd
2018-11-27 18:56:05.742  INFO 73687 --- [       Thread-2] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-11-27T18:56:05.741+0200 I CONTROL  [initandlisten] allocator: system

配置

@EnableReactiveMongoRepositories
public class MongoReactiveApplication  {

    @Value("${spring.data.mongodb.uri}")
    private String mongoUri;

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

}

yaml spring.data.mongodb.uri: mongodb://localhost:27017/mongotest

如何更改配置以使用外部mongo?

2 个答案:

答案 0 :(得分:0)

添加这些bean有助于我连接到外部mongo

@Value("${spring.data.mongodb.uri}")
private String mongoUri;

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

@Bean
public ReactiveMongoTemplate reactiveMongoTemplate() {
    return new ReactiveMongoTemplate(reactiveMongoDatabaseFactory());
}

@Bean
public ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory() {
    return new SimpleReactiveMongoDatabaseFactory(mongoClient(), "mongotest");
}

答案 1 :(得分:-1)

尝试一下

var mongoose = require('mongoose');
var itemSchema = new mongoose.Schema({ name: {type: String, required: true}});
module.exports = mongoose.model('W8DBItem', itemSchema);