mongodbspring-bootdomain-object

Spring Boot MongoDB with shared Domain Object


I have 3 services(3 different projects eg ClientService, AggregationService, DataService) that share same domain object and only one of them(DataService) connects to MongoDB and sends back the data to other 2 services. All these services are spring boot based

When I was keeping separate java file of the domain object kept in the respective project then it all worked fine as the domain object in ClientService and AggregationService didn't have mongodb annotations eg @Document, @Field.

But when I kept the domain object in a common module so that i don't have to maintain 3 copies, ClientService and AggregationService started throwing exception during start up. Though these services do start up and returns the response correctly but the exception also comes up when these services start up. Below is the domain object:

@Document(collection = "transformed_categories")
public class Category extends ResourceSupport {

    @Field("id")
    private String customId;
    private String name;
    private String type;
}

Exception:

2017-02-27 15:10:41.098  INFO 9052 --- [           main] c.c.delivery.CleintServiceApplication     : Started CleintServiceApplication in 3.47 seconds (JVM running for 3.918)
2017-02-27 15:10:41.895  INFO 9052 --- [localhost:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongo-java-driver-3.4.1.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongo-java-driver-3.4.1.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongo-java-driver-3.4.1.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_05]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_05]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_05]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) ~[na:1.8.0_05]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_05]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_05]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_05]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_05]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_05]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongo-java-driver-3.4.1.jar:na]
at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongo-java-driver-3.4.1.jar:na]
... 3 common frames omitted

I found the exact place which is causing the issue. The issue is caused by maven dependency that I added into my common-library project where I intend to keep all my domain objects.

    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-mongodb</artifactId>
      <version>1.10.0.RELEASE</version>
    </dependency>

If I remove this dependency then the exception doesn't come. What should be the approach in keeping domain objects which are shared across the projects?


Solution

  • This is an old post but requires answers.

    First thing, Microservices should not be sharing code. When there are domain objects which need to be sent across, we should use DTO pattern which should be owned by every microservice and that code should not be shared across. This is a recommended practice.

    Coming to MongoDB exception, this is a normal behavior of a spring boot application. When it sees MongoDB in the class path and no configuration in properties file, it would look for a MongoDB instance at localhost:27017 So if we don't intend to connect to MongoDB from a Microservice then MogoDB driver should not be present in the class path.