Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Microservices: An Example With Docker, Go, and MongoDB

DZone's Guide to

Microservices: An Example With Docker, Go, and MongoDB

See how you can set up your own system using a combination of microservices, containers, Go, and MongoDB. In this scenario, see a fictional movie theater come to life.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

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.

Microservices - Exposed ports

Screenshots

Starting services

Microservices - Starting services


Restoring Database Information

Microservices - Restoring database information

Traefik Dashboard

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)


Download A Buyer's Guide to Application and Data Integration, your one-stop-shop for research, checklists, and explanations for an application and data integration solution.

Topics:
container ,source code ,service ,database ,backend ,api ,docker ,mongodb ,go

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}