Spring Data Mongo using Mongo Cluster with Docker
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.
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.
1
> docker-compose up
Once your Mongo containers are running, the final step is to configure the Cluster.
1
> 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'}]})"
Connect to the cluster
Firsly, modify your /etc/hosts
file adding:
1
2
3
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:
1
mongodb://mongo1:27017,mongo2:27018,mongo3:27019/?replicaSet=rs0
Testing in a Spring Boot Application
You could see a complete example of use in Github