Coder Social home page Coder Social logo

bhardwajrahul / flask-microservice-demo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michaellitherland/flask-microservice-demo

0.0 0.0 0.0 173 KB

This is a very simple demonstration of a microservice architecture using common Python3 libraries and Docker.

License: Apache License 2.0

Python 100.00%

flask-microservice-demo's Introduction

Flask Microservice Demo

This project is an example of using nothing but python3, flask, requests, and docker to make a complete microservice architecture implementation. Yes, it passes JSON around in between the microservices, which is arguably not a great idea, but it's hard to argue with how easy these are to spin up.

Architecture Diagram

Here's the architecture diagram:

Edit: I added another GET to the backend to test the custSearch functionality and demo a JSON payload in client. Details below.

Architecture Diagram

For testing fun all of the microservices are exposed, but in a real application only the backend (or API) would be.

Description of the containers

There are four different docker images that are build for this demo. Backend is the API layer and it has three GET methods as illustrated in the diagram. For order details you can specify an integer from 1 to 1000. If you specify the count parameter it will show the first # of orders up to the count you specify.

The aggregate which is exposed locally on port 5003 combines information from the orders data service and the items data service. It has a single method which will actually return exactly the same thing as the backend /detail/ method, but of course you haven't exposed the aggregate microservice in the real world, and you'd do other value-add things in the API layer like authentication.

The orders microservice is a data service, and typically you'd be going after a database of some kind. Here a faker is used to create 1000 orders and they're kept in memory for the life of the run. The /custSearch method (which isn't used by the backend currently, but demonstrates how to take a JSON payload, much like you would pass to a microservice via a gRPC request or your preferred equivalent) can be queried through postman or some other REST testing interface. You'd set the "content-type" to "application/json" and provide a JSON payload like:

{
  "name": "Daniel"
}

Lastly the items microservice provides information about 100 randomly created items. This one is never used directly by the backend, but indirectly through the aggregate. Again, this would be a database of some sort in the real world.

Running it

You need docker and docker-compose. Follow your OS instructions for doing that. The docker images start with Alpine and install Python3. The only dependencies in Python are Flask, Faker, and requests.

Testing it

With the docker-compose running and postman, try the following URLS:

A bunch of raw orders:

http://localhost:5000/orders?count=10

[
    {
        "cust": "Mr. James Edwards",
        "id": 1,
        "items": [
            88,
            2,
            38,
            59,
            52,
            95,
            32,
            68,
            17
        ]
    },
    {
        "cust": "Sarah Taylor",
        "id": 2,
        ...and so on...
    }
    ...and so on...
]

An order detail

http://localhost:5000/detail/5

Notice how the "items" element has been replaced with a list of item details.

{
    "cust": "John Martinez",
    "id": 6,
    "items": [
        {
            "desc": "synthesize plug-and-play interfaces",
            "id": 72
        },
        {
            "desc": "target open-source action-items",
            "id": 18
        },
        {
            "desc": "grow dynamic web services",
            "id": 86
        },
        {
            "desc": "synthesize plug-and-play interfaces",
            "id": 72
        },
        {
            "desc": "streamline synergistic e-commerce",
            "id": 37
        }
    ]
}

Customer name search

http://localhost:5000/custSearch/Dan

You can now hit this URL directly with a GET to perform a search by partial customer name. A space is just '%20'. This calls the underlying custSearch endpoint for the orders microservice, but takes the name and forms a JSON payload which is possibly a more familiar way to send data to a microservice for it to operate on rather than doing it through parameters. Again Flask makes this absurdly simple.

Test the search in the data microservice

POST http://localhost:5001/custSearch

Note the port of the orders microservice here! This isn't the backend!

Make sure to use a POST and to set "Content-Type: application/json" in the "headers" and to put something like {"name": "Daniel"} in the "body". You'll get a list of order where the "cust" field contains the search parameter. The customer names are randomly generated, so you might want to try something simple.

flask-microservice-demo's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.