Skip to content
Snippets Groups Projects
Forked from COM3014 / microservices
2 commits behind, 8 commits ahead of the upstream repository.
Name Last commit Last update
database
services
README.md
movies.py
showtimes.py

Instructions

Step 1: microservices introductory demo

Often, we want to contribute to or further develop an open-source project in isolation from the original. We achieve this by forking a project for our personal use (always check license), which has some benefits over branching. Click Fork at the top right corner of this project's home page, and then you have a personal copy of the repository which you can change to your heart's content. Clone the repository to your Azure VM (or you can also use your own laptop if you have python3 and Docker installed ** It will not work on Windows unless you have Windows server** It will likely work with MacOS and Linux.).

You will see that you are on the master branch, with two files movies.py and showtimes.py. Look through their source code to see how they function. movies.py fetches movie data from a json file in /database. showtimes.py fetches showtimes for the movies from another json file in /database. Each of them is a microservice which exposes RESTful API endpoints. You can run them as follows:

python3 movies.py

Now, the movies microservice is running. You can query it by sending HTTP GET requests. Note that we are using the port 5001 on localhost (127.0.0.1) as that is how the server is configured (see towards the bottom of the file in movies.py)

python3 movies.py

Now, the movies microservice is running. You can query it by sending HTTP GET requests. Note that we are using the port 5001 on localhost (127.0.0.1) as that is how the server is configured (see towards the bottom of the file in movies.py)

curl http://127.0.0.1:5001/ #returns list of endpoints exposed
curl http://127.0.0.1:5001/movies #returns list of all movies
curl http://127.0.0.1:5001/movies/720d006c-3a57-4b6a-b18f-9b713b073f3c #returns details of one movie

Similarly, you can run the showtimes microservice

python3 showtimes.py

You can query its endpoints too:

python3 showtimes.py

You can query its endpoints too:

$ curl 127.0.0.1:5002/      
{"uri": "/", "subresource_uris": {"showtimes": "/showtimes", "showtime": "/showtimes/<date>"}}%                                                                 
$ curl 127.0.0.1:5002/showtimes
{"20151130": ["720d006c-3a57-4b6a-b18f-9b713b073f3c", "a8034f44-aee4-44cf-b32c-74cf452aaaae", "39ab85e5-5e8e-4dc5-afea-65dc368bd7ab"], "20151201": ["267eedb8-0f5d-42d5-8f43-72426b9fb3e6", "7daf7208-be4d-4944-a3ae-c1c2f516f3e6", "39ab85e5-5e8e-4dc5-afea-65dc368bd7ab", "a8034f44-aee4-44cf-b32c-74cf452aaaae"], "20151202": ["a8034f44-aee4-44cf-b32c-74cf452aaaae", "96798c08-d19b-4986-a05d-7da856efb697", "39ab85e5-5e8e-4dc5-afea-65d

Step 2: Making microservices talk to each other

As with the movies, the showtimes microservice allows you to query one single record. For a date that it knows about (e.g., 30 Nov 2015 or 20151130), you can ask what movies were shown on that date.

$ curl 127.0.0.1:5002/showtimes/20151130 
[
    "720d006c-3a57-4b6a-b18f-9b713b073f3c",
    "a8034f44-aee4-44cf-b32c-74cf452aaaae",
    "39ab85e5-5e8e-4dc5-afea-65dc368bd7ab"
]%  

You get back a list of movie IDs. This is nice, but not wholly satisfactory. Your next task is to modify the showtimes_record function to contact the movies microservice to get the movie title given its ID, and make the showtimes service return back a more user-friendly response that looks like this:

$ curl 127.0.0.1:5002/showtimes/20151130
[
    "The Good Dinosaur",
    "The Martian",
    "Spectre"
]%               

The main change needed is simple. We just have to issue an HTTP GET request to the movies API endpoint. We do this by changing showtime_record as follows:

movies_service = "http://127.0.0.1:5001/movies/{}" #we know this is where the service is running. We replace the {} with the actual ID of the movie we want.

def showtimes_record(date):
    if date not in showtimes:
        raise NotFound
    print(showtimes[date])
    result = []
    for movie_id in showtimes[date]:
        resp = requests.get(movies_service.format(movie_id))
        result.append(resp.json()["title"])
    return nice_json(result)

Step 3: Dockerization of microservices

We now have two microservices running on localhost and talking to each other. If we need to move these services, it is not easy. Your next task is to wrap these microservices as a docker image and make them portable. Essentially, this involves specifying all the dependencies and running parameters explicitly in a Dockerfile. You can have a go at this by copying from this tutorial. Or, switch to the dockerservices branch where you will find the solution for steps 3 and 4.

Step 4: How to make two dockerized microservices talk to each other

credits

The original code is taken from https://github.com/umermansoor/microservices It has been lightly modified for Python3 compatibility, and further simplified to showcase microservice communications. We have also added a demo of dockerization