如何使`org.mongodb.driver.cluster`在spring boot中使用嵌入式mongodb?

Posted

技术标签:

【中文标题】如何使`org.mongodb.driver.cluster`在spring boot中使用嵌入式mongodb?【英文标题】:How to make `org.mongodb.driver.cluster` use embedded mongodb within spring boot? 【发布时间】:2020-07-01 07:19:05 【问题描述】:

尝试将嵌入式 mongodb 用于我的 spring local 配置文件。这是MongoTemplate的配置

@Configuration
@Profile("local")
public class LocalMongoConfig 
    private static final String MONGO_DB_URL = "localhost";
    private static final String MONGO_DB_NAME = "embeded_db";
    @Bean
    @Primary
    public MongoTemplate mongoTemplate() throws IOException 
        EmbeddedMongoFactoryBean mongo = new EmbeddedMongoFactoryBean();
        mongo.setBindIp(MONGO_DB_URL);
        MongoClient mongoClient = mongo.getObject();
        MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, MONGO_DB_NAME);
        return mongoTemplate;
    

这是application-local.yml

spring:
  data:
    mongodb:
      uri: mongodb://127.0.0.1:27017/embeded_db

但是从日志中运行应用程序时,我可以看到嵌入式 mongodb 正在随机端口启动。

13:53:49.849 [Thread-7] INFO  org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongo - 2020-03-20T13:53:49.849+0800 I NETWORK  [thread1] waiting for connections on port 51564

然后我收到以下错误提示 connection refused 这是正确的,因为它尝试连接到不同的端口(27017)

 13:53:50.953 [restartedMain] INFO  org.mongodb.driver.cluster - Cluster description not yet available. Waiting for 30000 ms before timing out
13:53:51.639 [cluster-ClusterIdvalue='5e745a6e973cbd4bd45d073e', description='null'-127.0.0.1:27017] INFO  org.mongodb.driver.cluster - Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67)
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126)
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused: connect

如何确保mongodb.driver 使用嵌入式 mongodb?

【问题讨论】:

【参考方案1】:

EmbeddedMongoFactoryBean 有一个方法可以使用它来设置所需的端口。

/**
 * The port MongoDB should run on. When no port is provided, then some free
 * server port is automatically assigned. The value must be between 0 and 65535.
 */
public void setPort(int port) 
    builder.port(port);

试试这个:

@Bean
@Primary
public MongoTemplate mongoTemplate() throws IOException 
    EmbeddedMongoFactoryBean mongo = new EmbeddedMongoFactoryBean();
    mongo.setPort(27017); //here
    mongo.setBindIp(MONGO_DB_URL);
    MongoClient mongoClient = mongo.getObject();
    MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, MONGO_DB_NAME);
    return mongoTemplate;

但根据文档,您应该这样做:

@Bean(destroyMethod = "close")
@Profile("local")
MongoClient mongo()
    return new EmbeddedMongoBuilder().port().bindIp().build();

【讨论】:

我试过了。但嵌入式 mongo 仍然从随机端口开始

以上是关于如何使`org.mongodb.driver.cluster`在spring boot中使用嵌入式mongodb?的主要内容,如果未能解决你的问题,请参考以下文章

如何使 RelativeLayout 半透明但不使活动

如何用word使图片上下居中

如何使图像自动调整大小,使宽度为 100% 并相应调整高度?

如何使 UISegmentedcontrol 透明?

如何使 textarea 填充 div 块?

如何使 UITableViewCell 显示为禁用?