Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • com3014-2021/microservices
  • ii00088/microservices
  • rk00436/microservices
  • at00976/microservices
  • tt0018/microservices_OLD
  • cu00080/microservices
  • kk00515/microservices
  • kd00450/microservices
  • cl01056/microservices
  • mj00439/microservices
  • ly00240/microservices
  • ak01928/microservices
  • sf00563/microservices
  • is00320/microservices
  • ty00208/microservices
  • eo00684/microservices
  • mt00969/microservices
  • as03382/microservices
  • br00391/microservices
  • mb02046/microservices
  • og00209/microservices
  • mw01180/microservices
  • rg00736/microservices
  • cr00657/microservices
  • th00993/microservices
  • ke00338/microservices
  • jc02788/microservices
  • jb02262/com-3014-microservices-lab-6
  • an01020/microservices
  • uo00079/microservices
  • as05530/microservices
  • sv00653/microservices
  • cc02503/microservices
  • mi00476/microservices
  • dd00830/dd-00830-s-microservices
  • ar01508/microservices
  • lm02027/microservices
  • pratyushkrsahu/microservices-lab
  • shubhamkandpal111/microservices
  • nc01009/microservices
  • bs01368/microservices
  • sm02810/microservices
  • ma03940/microservices
  • oi00110/microservices-myversion
  • ps01603/microservices
  • ut00054/microservices
  • mp01405/microservices
  • vk0012/microservices
  • ag02566/microservices
  • ts00738/microservices-lab
  • ha02133/microservices
  • rm01524/microservicesreuben
  • rs02603/microservices
  • ht00865/microservices
  • mb03523/microservices
  • mm04625/microservices
  • in00270/microservices
  • sn01408/microservices
  • ng00868/microservices
  • hl01638/microservices
  • ap02973/microservices
  • ak03955/nakama
  • db01397/microservices
  • mn01300/microservices
  • ss05670/microservices
  • tt00964/microservices
  • mo01018/microservices
  • sk03140/labmicroservices
  • no00505/microservices
  • ms02916/microservices
  • ss05503/microservices
  • ms04773/microservices-fork
  • mm03317/microservices
73 results
Show changes
Commits on Source (5)
# 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:
```console
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)
```console
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
```console
python3 showtimes.py
```
You can query its endpoints too:
```console
$ 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.
```console
$ 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:
```console
$ curl 127.0.0.1:5002/showtimes/20151130
[
"The Good Dinosaur",
"The Martian",
"Spectre"
]%
```
>Try your best to solve it. If you need hints, you will find the solution for this in the branch `simpleservices`, which you can obtain by running `git checkout simpleservices`. Note that after checking out this branch, you will have to start both the `movies` and `showtimes` microservices and then issue the curl request. The showtimes microservice will talk to movies microservice to deliver the final result.
> This tutorial is continued now in the `simpleservices` branch
## Step 3: Dockerization of microservices
## 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
import os
import json
from flask import make_response
def root_dir():
""" Returns root director for this project """
return os.path.dirname(os.path.realpath(__file__ + '/..'))
def nice_json(arg):
response = make_response(json.dumps(arg, sort_keys = True, indent=4))
response.headers['Content-type'] = "application/json"
return response
\ No newline at end of file
from services import root_dir, nice_json
from flask import Flask
import json
from werkzeug.exceptions import NotFound
app = Flask(__name__)
with open("{}/database/bookings.json".format(root_dir()), "r") as f:
bookings = json.load(f)
@app.route("/", methods=['GET'])
def hello():
return nice_json({
"uri": "/",
"subresource_uris": {
"bookings": "/bookings",
"booking": "/bookings/<username>"
}
})
@app.route("/bookings", methods=['GET'])
def booking_list():
return nice_json(bookings)
@app.route("/bookings/<username>", methods=['GET'])
def booking_record(username):
if username not in bookings:
raise NotFound
return nice_json(bookings[username])
if __name__ == "__main__":
app.run(port=5003, debug=True)
from services import root_dir, nice_json
from flask import Flask
from werkzeug.exceptions import NotFound
import json
app = Flask(__name__)
with open("{}/database/movies.json".format(root_dir()), "r") as f:
movies = json.load(f)
@app.route("/", methods=['GET'])
def hello():
return nice_json({
"uri": "/",
"subresource_uris": {
"movies": "/movies",
"movie": "/movies/<id>"
}
})
@app.route("/movies/<movieid>", methods=['GET'])
def movie_info(movieid):
if movieid not in movies:
raise NotFound
result = movies[movieid]
result["uri"] = "/movies/{}".format(movieid)
return nice_json(result)
@app.route("/movies", methods=['GET'])
def movie_record():
return nice_json(movies)
if __name__ == "__main__":
app.run(port=5001, debug=True)
from services import root_dir, nice_json
from flask import Flask
from werkzeug.exceptions import NotFound
import json
app = Flask(__name__)
with open("{}/database/showtimes.json".format(root_dir()), "r") as f:
showtimes = json.load(f)
@app.route("/", methods=['GET'])
def hello():
return nice_json({
"uri": "/",
"subresource_uris": {
"showtimes": "/showtimes",
"showtime": "/showtimes/<date>"
}
})
@app.route("/showtimes", methods=['GET'])
def showtimes_list():
return nice_json(showtimes)
@app.route("/showtimes/<date>", methods=['GET'])
def showtimes_record(date):
if date not in showtimes:
raise NotFound
print showtimes[date]
return nice_json(showtimes[date])
if __name__ == "__main__":
app.run(port=5002, debug=True)
from services import root_dir, nice_json
from flask import Flask
from werkzeug.exceptions import NotFound, ServiceUnavailable
import json
import requests
app = Flask(__name__)
with open("{}/database/users.json".format(root_dir()), "r") as f:
users = json.load(f)
@app.route("/", methods=['GET'])
def hello():
return nice_json({
"uri": "/",
"subresource_uris": {
"users": "/users",
"user": "/users/<username>",
"bookings": "/users/<username>/bookings",
"suggested": "/users/<username>/suggested"
}
})
@app.route("/users", methods=['GET'])
def users_list():
return nice_json(users)
@app.route("/users/<username>", methods=['GET'])
def user_record(username):
if username not in users:
raise NotFound
return nice_json(users[username])
@app.route("/users/<username>/bookings", methods=['GET'])
def user_bookings(username):
"""
Gets booking information from the 'Bookings Service' for the user, and
movie ratings etc. from the 'Movie Service' and returns a list.
:param username:
:return: List of Users bookings
"""
if username not in users:
raise NotFound("User '{}' not found.".format(username))
try:
users_bookings = requests.get("http://127.0.0.1:5003/bookings/{}".format(username))
except requests.exceptions.ConnectionError:
raise ServiceUnavailable("The Bookings service is unavailable.")
if users_bookings.status_code == 404:
raise NotFound("No bookings were found for {}".format(username))
users_bookings = users_bookings.json()
# For each booking, get the rating and the movie title
result = {}
for date, movies in users_bookings.iteritems():
result[date] = []
for movieid in movies:
try:
movies_resp = requests.get("http://127.0.0.1:5001/movies/{}".format(movieid))
except requests.exceptions.ConnectionError:
raise ServiceUnavailable("The Movie service is unavailable.")
movies_resp = movies_resp.json()
result[date].append({
"title": movies_resp["title"],
"rating": movies_resp["rating"],
"uri": movies_resp["uri"]
})
return nice_json(result)
@app.route("/users/<username>/suggested", methods=['GET'])
def user_suggested(username):
"""
Returns movie suggestions. The algorithm returns a list of 3 top ranked
movies that the user has not yet booked.
:param username:
:return: Suggested movies
"""
raise NotImplementedError()
if __name__ == "__main__":
app.run(port=5000, debug=True)
......@@ -24,7 +24,7 @@ def hello():
@app.route("/showtimes", methods=['GET'])
def showtimes_list():
nice_json(showtimes)
return nice_json(showtimes)
@app.route("/showtimes/<date>", methods=['GET'])
......