How to configure embeded mongodb to run in given port in spring boot
NickName:springbootlearner Ask DateTime:2022-02-22T07:19:07

How to configure embeded mongodb to run in given port in spring boot

I want to use embedded mongoDB with Spring boot for my development and testing. When I try to configure its getting started but always run with dynamic port instead of port which I give in URL config. Idea is I want to use same kind of MongoConfig for both my test (Unit/Integration) and real environment.

My property file looks below.

spring:
  profiles: local
  data:
    mongodb:
      uri: mongodb://localhost:62309/local_db


@Configuration
public class MongoDBConfig {
    
    private final Environment env;
    
    public MongoDBConfig(Environment env) {
        this.env = env;
    }

    @Bean
    public MongoDbFactory getMongoFactory() {
        return new SimpleMongoClientDbFactory(env.getProperty("spring.data.mongodb.uri"));
    }
    
    @Bean
    public MongoTemplate getMongoTemplate() {
        return new MongoTemplate(

> getMongoFactory

());
    }

}


pom.xml
--------

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
    </parent>

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

with this configuration app is getting started when I start it but with dynamic port every restart. Also seeing below exception trace in logs. From this seems like Spring is first try to connect given mongo instance from my local machine and its not finding any so its failing then its invoking emebeded mongodb.

Is there a way I can make spring to make use of configured port to run embedded mongoDB without that error being thrown.

Error Trace

[cluster-ClusterId{value='62141aecfb7ede3c51b3d064', description='null'}-localhost:62309] INFO  org.mongodb.driver.cluster -
                Exception in monitor thread while connecting to server localhost:62309
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70)
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128)
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64)
    at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79)
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
    ... 3 common frames omitted

Copyright Notice:Content Author:「springbootlearner」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71214143/how-to-configure-embeded-mongodb-to-run-in-given-port-in-spring-boot

More about “How to configure embeded mongodb to run in given port in spring boot” related questions

How to configure embeded mongodb to run in given port in spring boot

I want to use embedded mongoDB with Spring boot for my development and testing. When I try to configure its getting started but always run with dynamic port instead of port which I give in URL conf...

Show Detail

Spring Boot and how to configure connection details to MongoDB?

Being new to Spring Boot I am wondering on how I can configure connection details for MongoDB. I have tried the normal examples but none covers the connection details. I want to specify the data...

Show Detail

Configure MongoDB in Spring Boot using environment variables

I'm new to Spring Boot and am trying to configure a connection to MongoDB using environment variables - i.e. I have followed an example online showing how to configure my mongo database/host/port via

Show Detail

How to configure port for a Spring Boot application

How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080.

Show Detail

How to configure port for a Spring Boot application

How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080.

Show Detail

How to configure port for a Spring Boot application

How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080.

Show Detail

Configure mongodb property maxWaitQueueSize in Spring boot application?

I get the error com.mongodb.MongoWaitQueueFullException: Too many threads are already waiting for a connection. Max number of threads (maxWaitQueueSize) of 500 has been exceeded. while doing a stress

Show Detail

How to configure Tomcat shutdown port in Spring Boot?

Looking for way how to configure shutdown port in Spring boot app.

Show Detail

Spring Boot with MongoDB on Docker

In these days, I am trying to deploy my Spring Boot OAuth2 project. It has 3 different modules.(Authentication Server, Resource Server and Front-end) Authentication and Resource servers have own *....

Show Detail

Java: How to change mongodb port configured with spring boot yml

With java I can do the following to change the tomcat port: java -jar spring-5.jar --server.port=8083 That works for changing the default 8080 port in my yml file but what if I also want to chang...

Show Detail