MicroAPI is fully separates API in Async mode based on Microservices
For CRUD operations is used Customer - Product - Price - Order
model
The
Customer
buys aProduct
with a differentPrice
and receives anOrder
ThePrice
is calculated including taxes/discounts depending on the type of customer/
Each part is work like Microservice
The Microservice runs in separate Docker container
Microservice has its own Database
Database can be switch from Postgres to MongoDB or other
Clone the project
git clone [email protected]:zakharb/microapi.git
cd microapi
Start docker-compose
docker-compose up -d
Get, put, update, delete Customers
via API Customers
Get, put, update, delete Products
via API Products
Get Prices
via API Prices
Get Orders
via API Orders
To solve problem with performance each Service run in container
Uvicorn work as ASGI server and connect to one piece with Nginx
Main configuration is docker-compose.yml
- every service located in separate directory
name-service
- use
Dockerfile
to change docker installation settings - folder
app
contain FastAPI application - all services connected to one piece in
docker-compose.yml
- example of service + DB containers (change
--workers XX
to increase multiprocessing)
Customer
service
customer_service:
build: ./customer-service
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4
#command: gunicorn main:app --workers 4 --worker-class --host 0.0.0.0 --port 8000
volumes:
- ./customer-service/:/app/
ports:
- 8001:8000
environment:
- DATABASE_URI=postgresql://customer_db_username:customer_db_password@customer_db/customer_db_dev
depends_on:
- customer_db
logging:
driver: none
customer_db:
image: postgres:latest
volumes:
- postgres_data_customer:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=customer_db_username
- POSTGRES_PASSWORD=customer_db_password
- POSTGRES_DB=customer_db_dev
logging:
driver: none
There is client for work with Microservices
Microapiclient
is used for generating data and testing
It is located in folder client
cd client
python3 -m venv venv
source venv/bin/activate
python -m pip install -e .
cd client
source venv/bin/activate
python -m microapiclient
__ __ _ _____ _____ _______
(__)_(__) (_) _ (_____) (_____)(_______)
(_) (_) (_) _ ___ (_)__ ___ (_)___(_)(_)__(_) (_)
(_) (_) (_)(_) _(___)(____)(___) (_______)(_____) (_)
(_) (_)(_)(_)___ (_) (_)_(_)(_) (_)(_) __(_)__
(_) (_)(_) (____)(_) (___) (_) (_)(_) (_______)
usage: __main__.py [-h]
{getcustomer,postcustomer,getproduct,postproduct,getprice,postorder,postorders,generateorder}
...
positional arguments:
{getcustomer,postcustomer,getproduct,postproduct,getprice,postorder,postorders,generateorder}
getcustomer Get customer from DB
postcustomer Post new customer into DB
getproduct Get product by name from DB
postproduct Post new product into DB
getprice Get price_net and price_gross
postorder Post order into DB
postorders Bulk write orders into DB
generateorder Generate order into CSV file
optional arguments:
-h, --help show this help message and exit
- Generate Customers
python -m microapiclient postcustomer --customer-count 50
- Generate Products
python -m microapiclient postproduct --product-count 50
- Generate Orders to file
python -m microapiclient generateorder --order-count 1000 --task-count 32
- Bulk write Orders from file to DB
python -m microapiclient postorders --order-file orders.csv --task-count 32
- View logs in
client.log
cat client.log | more
Edit Dockerfile
for each Microservice and deploy container
Using SemVer for versioning. For the versions available, see the tags on this repository.
- Zakhar Bengart - Initial work - Ze
See also the list of contributors who participated in this project.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation - see the LICENSE file for details