Francisco Dorado

I am a software developer focused on back-end and specialized in Java and Spring.


Hits

Spring Data Mongo using Mongo Cluster with Docker

(Reading time: ~1 min)
16 Apr 2019 » spring

MongoDB Replicaset

In this post we are going to talk about how to configure a MongoDB Cluster in local using docker-compose and how to use it with Spring Data Mongo.

1. Configure the Replica Set

For our example, we are going to configure a Replica Set with 3 nodes as shown in the following docker-compose.yml file:

version: '3'

services:
  mongo1:
    image: mongo:3.6
    command: mongod --replSet rs0 --port 27017
    ports: 
      - 27017:27017
    networks: 
      - my-mongo-cluster

  mongo2:
    image: mongo:3.6
    command: mongod --replSet rs0 --port 27018
    ports: 
      - 27018:27018
    networks: 
      - my-mongo-cluster

  mongo3:
    image: mongo:3.6
    command: mongod --replSet rs0 --port 27019
    ports:  
      - 27019:27019
    networks: 
      - my-mongo-cluster
    
networks: 
  my-mongo-cluster:

Now, run the docker-compose file.

> docker-compose up

Once your Mongo containers are running, the final step is to configure the Cluster.

> docker-compose exec mongo1 mongo --eval "rs.initiate({_id : 'rs0','members' : [{_id : 0, host : 'mongo1:27017'},{_id : 1, host : 'mongo2:27018'},{_id : 2, host : 'mongo3:27019'}]})"

2. Connect to the cluster

Firsly, modify your /etc/hosts file adding:

127.0.0.1 mongo1
127.0.0.1 mongo2
127.0.0.1 mongo3

Now you can connect to the Cluster with the next url:

mongodb://mongo1:27017,mongo2:27018,mongo3:27019/?replicaSet=rs0

3. Testing in a Spring Boot Application

You could see a complete example of use in Github