Microservices: An Example With Docker, Go, and MongoDB
introduction
"cinema" is an example project which demonstrates the use of microservices for a fictional movie theater. the cinema backend is powered by for microservices, all of which happen to be written in go, using mongodb for manage the database and docker to isolate and deploy the ecosystem.
- users service : provides users information.
- movie service : provides information like movie ratings, title, etc.
- show times service : provides showtime information.
- booking service : provides booking information.
the cinema use case is based on the project written in python by umer mansoor .
the project structure is based on the knowledge learned in the book web development with go by shiju varghese , isbn 978-1-4842-1053-6
prerequisite
- docker 18.06.1-ce
- docker compose 1.23.1
we must add virtual domains in order to use each api entry point . by default we are using movies.local , bookings.local , users.local, showtimes.local and monitor.local .
virtual domains have been defined in the docker-compose.yml
file and configured in the /etc/hosts
file. add the following line in your /etc/hosts
file:
127.0.0.1 movies.local bookings.local users.local showtimes.local monitor.local
monitor.local will be used to see the dashboard created by traefik.
source code
you can download the source code using this link .
services diagram
starting services
docker-compose up -d
stopping services
docker-compose stop
including new changes
if you need to change some source code you can deploy it typing:
docker-compose build
you can start using an empty database for all microservices, but if you want you can restore a preconfigured data execute this step:
restore mongodb data typing:
docker-compose exec db /bin/bash /backup/restore.sh
documentation
user service
this service returns information about the users of cinema.
routes:
- get - http://users.local/users : get all users.
- post - http://users.local/users : create user.
- delete - http://users.local/users/{id} : remove user by id.
movie service
this service is used to get information about a movie. it provides the movie title, rating on a 1-10 scale, director, and other information.
routes:
- get - http://movies.local/movies : get all movies.
- post - http://movies.local/movies : create movie.
- get - http://movies.local/movies/{id} : get movie by id.
- delete - http://movies.local/movies/{id} : remove movie by id.
showtimes service
this service is used get a list of movies playing on a certain date.
routes:
- get - http://showtimes.local/showtimes : get all showtimes.
- post - http://showtimes.local/showtimes : create showtime.
- get - http://showtimes.local/showtimes/{id} : get showtime by id.
- delete - http://showtimes.local/showtimes/{id} : remove showtime by id.
booking service
used to lookup booking information for users.
routes:
- get - http://bookings.local/bookings : get all bookings.
- post - http://bookings.local/bookings : create booking.
exposed ports
the port 27017
is exposed to be consulted by the robomongo system. port 80
is exposed to be consulted by devices, web browsers, or others microservices.
screenshots
starting services
restoring database information
traefik dashboard
service: get all users (postman)
service: get all movies (postman)
service: get all showtimes (postman)
service: get all bookings (postman)
database big picture (robomongo)